和zabbix同理 在使用一个服务之前得保证我们的系统中有这个包 进行安装并启动服务
首先说一下是yum的三种形式
yum仓库:
本地的仓库:base_url = file:///media
本地网络仓库:base_url = http://10.15.200.8/c7
互联网的仓库base_url = http://mirrors.ailiyun.com/centos7/x64(这是阿里云的仓库,网络yum源)
这里我们配置好了仓库 刷新一下仓库 然后检查一下仓库
yum clean all
yum repolist all(注:不刷新仓库,可能装包不成功)
在ansible中:
管理端contoller
被管理端 managed
ansible是基于tcp传输模式(可靠 安全)
ssh通信是基于tcp 加密系统自带 所以不需要安装客户端
首先先了解ansible 的语法
ansible 你要操作的主机 组名-m 模块的名字 -a 参数(注:shell模块加引号)
示例:ansible group1 -m ping 对group1所有主机进行ping
-m shell 你要调用的模块 -a 你要执行的命令
shell支持管道 command不支持管道
每次的命令结果:
红色肯定是报错
紫色警告
绿色成功
浅黄色是第一次成功
如何查看帮助:ansible-doc -l 可以看到服务自带的模块有3387种
[root@ansible ~]# ansible-doc -l |wc -l
3387
3387模块的数量 如:ping yum service
content=你要写的内容
安装包:
state:
name=php为例
present:如果没有安装,则进行安装;如果安装过,不会安装
absent:删除对应的包(离席)
latest:不论安没安装 都去仓库找最新的
service:服务 (类似systemctl)
state:
started:保证服务是开启的状态 (如是已经启动就不再启动)
restart:每次都要重启 服务
stopped:保证开机自启
restarted 重启服务
name=服务的名字 systemctl start 后面的名字
state=started保证状态为active enabled=yes
开机启动相当于sysyemctl enable 前面哪个 name=服务的名字
一般部署一个服务一般就是分为三步
1.安装包
2.修改配置文件
3.设置服务的启动开机自启
这里只会是在第二步的时候变量比较多
ansible-inventory --graph(图形化查看你的inventory内主机)
@all: (所有主机等于/etc/ansible/inventory)
|--@group1:
| |--10.15.200.102
| |--node01
| |--node03.example.cn
|--@ungrouped:
这也是查询的一种方式:
ansible-inventory --list
如何查看帮助
这是查看yum的帮助
ansible-doc yum
vi这个文件 然后找EXAMPLES开头的
/usr/lib/python2.7/site-packages/ansible/modules/packaging/os/yum.py
ansible-doc yum |grep -A 100 EXAMPLES
简单的操作
ansible all -m copy -a 'content="自动化很简单" dest=/var/www/html/0516.html'
content=你要写的内容 dest=你要把东西放哪 写到哪个文件里
'content="自动化很简单" dest=/var/www/html/0516.html'
这外面是单引号 里面就是双引号 或者反向一样 外面双引号里面就单引号
dest 前面有空格
ansible all -m shell -a 'cat /var/www/html/0516.html'
node01 | CHANGED | rc=0 >>
自动化很简单
node03.example.cn | CHANGED | rc=0 >>
自动化很简单
10.15.200.102 | CHANGED | rc=0 >>
自动化很简单
ansible all -m shell -a 'curl http://127.0.0.1/0516.html'
这是用网页的方式去看
[WARNING]: Consider using the get_url or uri module rather than running 'curl'.
If you need to use command because get_url or uri is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
node03.example.cn | CHANGED | rc=0 >>
自动化很简单 % Total % Received % Xferd Average Speed Time Time Time Current
command_warnings=False(上面这一长段里写了 意思不要你用shell 有更好的方式 如果不想看到这个提示 就ansible.cfg配置文件加这一行
command_warnings=False vi加这一行加了就没有提示了
curl http://10.15.200.101/0516.html
自动化很简单[root@ansible ~]#
这个自动化很简单应该在下一行的 加个换行
这里重新执行一下这条命令
ansible all -m copy -a 'content="我要换行啦\n" dest=/var/www/html/0516.html'
curl http://10.15.200.101/0516.html
我要换行啦
copy:复制一个文件
ansible group1 -m copy -a 'src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf'(这里是做了一个修改http端口的号的修改 改成0518了)
curl httpd://10.15.200.101:5018/0516.html
curl: (1) Protocol httpd not supported or disabled in libcurl
这样出错了 修改了配置文件要重启服务
调用service 重启所有节点的httpd服务
ansible all -m service -a 'name=httpd state=restarted'
netstat-tualnp
上面讲的是copy:直接生成内容 和本地文件复制到对端
copy 不可以复制或创建 没有文件或目录
要自己先创建好
file:
ansible all -m file -a 'path=/data/web/2020/0518 state=directory recurse=yes'
path=路径 state=directory 说明他是一个目录
recurse=yes递归创建 相当于mkdir -p
剧本:playbook
相同的功能 缩进必须要一致
比如- name: 安装httpd
冒号的后面必须有一个空格
空格与tab 不能混用
- name: 搭建wordpress的基础环境 包含httpd php mariadb
hosts: group1
tasks:
必须对齐 手动按两空格太费事
# 自动补全 将一个 tab 设置为 默认两个 空格
echo "autocmd FileType yaml setlocal ai ts=2 sw=2 et" > $HOME/.vimrc
模板 这是里系统的欢迎界面 /etc/motd
vim motd.j2
Welcome to
FQDN: {{ ansible_fqdn }}
主机名是: {{ ansible_hostname }}
IP地址是: {{ ansible_default_ipv4.address }}
网卡的名字是: {{ ansible_default_ipv4.alias }}
~ 先写好对应的变量
然后
vim sync_motd.yml
- name: sync motd
hosts: group1
tasks:
- name: welcomte to motd
template:
src: motd.j2
dest: /etc/motd
模板 替换文件 内容 - name: change port
replace:
path: /etc/httpd/conf/htppd.conf
regexp: "Listen 80"
replace: "Listen 5020"
这是把 /etc/httpd/conf/htppd.conf
文件中 查找Listen 80 换成 Listen 5020
files:放copy模块需要的文件
templates:templates模板需要的东西
tasks/main.yml 这是程序的入口 (程序就是从这执行)