1.制作清单模板
[root@afei weixin]# tree
.
├── files
│ └── httpd.conf
├── group_vars
│ └── wbservers
├── host_vars
│ └── 192.168.240.134
├── inventory
├── password
└── playbook.yml
2.使用forks在ansible中配置并行
[root@afei weixin]# vim /etc/ansible/ansible.cfg
#inventory = /etc/ansible/hosts
inventory = /etc/ansible/inventory
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp
#local_tmp = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks = 5
#poll_interval = 15
#sudo_user = root
#ask_sudo_pass = True
#ask_pass = True
#transport = smart
#remote_port = 22
#module_lang = C
#module_set_locale = False
注:forks默认参数为5,同时控制5台主机
3.查看配置文件中forks参数
[root@afei weixin]# ansible-config dump|grep -i fork
DEFAULT_FORKS(default) = 5
4.在受控主机上查看负载情况,以便于更合理的设置fork参数
[root@localhost ~]# top
top - 11:11:07 up 19:43, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 334 total, 2 running, 332 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1800.6 total, 248.1 free, 948.4 used, 604.1 buff/cache
MiB Swap: 2048.0 total, 1717.7 free, 330.2 used. 668.7 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1000 root 20 0 291792 6224 4300 S 0.3 0.3 1:38.88 vmtoolsd
2848 fei 20 0 531724 15304 8424 S 0.3 0.8 1:35.63 vmtoolsd
29499 root 20 0 64772 5232 4176 R 0.3 0.3 0:00.11 top
1 root 20 0 245972 9324 5004 S 0.0 0.5 0:17.75 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.08 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
5.在多台主机中滚动更新playbook,使用serial关键字来确保指定的主机数单独完成整个playbook,这样更安全
[root@afei weixin]# vim playbook.yml
---
- name: 滚动更新
- hosts: wbservers
serial: 2
tasks:
- name: 安装阿帕奇
yum:
name: httpd
state: present
notify: restart apache
handlers:
- name: 重启阿帕奇
service:
name: httpd
state: restarted
5.包含和导入文件
[root@afei playbook]# vim install.yml
---
- hosts: all
tasks:
- name: 安装阿帕奇
yum:
name: httpd
state: present
[root@afei playbook]# vim config.yml
---
- hosts: all
tasks:
- name: 配置阿帕奇
template:
src: .../files/vhosts.conf.j2
dest: /etc/httpd/conf.d/vhosts.conf
notify:
- name: 重启阿帕奇
handlers:
- name: 重启阿帕奇
service:
name: httpd
state: restarted
[root@afei playbook]# vim main.yml
[root@afei playbook]# vim main.yml
---
- name: 导入安装
import_playbook: install.yml
- name: 导入配置
import_playbook: config.yml
~
[root@afei playbook]# ansible-playbook playbook/main.yml