ansible playbook
ansible-playbook命令格式:
ansible-playbook [option] filename(剧本名字)
ansible-playbook 命令的常用选项:
-C: 模拟运行
--list-hosts: 列出剧本主机清单
--list-tags : 列出剧本标记
--list-tasks: 列出剧本任务
--syntax-check: 检测语法
安装并且启动apache服务的剧本简单举例:
[root@host1 ansible]# mkdir ansible_playbook
[root@host1 ansible]# ll
total 24
-rw-r--r-- 1 root root 19980 Sep 14 04:00 ansible.cfg
drwxr-xr-x 2 root root 6 Oct 24 08:28 ansible_playbook
-rw-r--r-- 1 root root 40 Oct 9 15:04 hosts
drwxr-xr-x 2 root root 6 Sep 14 04:00 roles
[root@host1 ansible_playbook]# pwd
/etc/ansible/ansible_playbook
[root@host1 ansible_playbook]# ll
total 4
-rw-r--r-- 1 root root 210 Oct 24 08:48 apache.yaml
[root@host1 ansible_playbook]# more apache.yaml
- hosts: test1
tasks:
- name: Install Httpd
yum name=httpd state=installed
- name: Configure Httpd.conf
- name: Start Httpd Service
service name=httpd state=started enabled=yes
检测语法举例:
[root@host1 ansible_playbook]# ansible-playbook --syntax-check apache.yaml
playbook: apache.yaml
查看剧本主机清单:
[root@host1 ansible_playbook]# ansible-playbook --list-hosts apache.yaml
playbook: apache.yaml
play #1 (test1): test1 TAGS: []
pattern: [u'test1']
hosts (2):
192.168.132.163
192.168.132.162
查看剧本任务:
[root@host1 ansible_playbook]# ansible-playbook --list-tasks apache.yaml
playbook: apache.yaml
play #1 (test1): test1 TAGS: []
tasks:
Install Httpd TAGS: []
Start Httpd Service TAGS: []
模拟运行剧本:
[root@host1 ansible_playbook]# ansible-playbook -C apache.yaml
PLAY [test1] ************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.132.163]
ok: [192.168.132.162]
TASK [Install Httpd] ****************************************************************************************************************
changed: [192.168.132.163]
changed: [192.168.132.162]
TASK [Start Httpd Service] **********************************************************************************************************
changed: [192.168.132.162]
changed: [192.168.132.163]
PLAY RECAP **************************************************************************************************************************
192.168.132.162 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.132.163 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
出现如上情况说明剧本模拟运行正常,可以去直接执行剧本了!!
下面是批量修改httpd的配置文件后重启httpd服务的ansible 剧本(文件):
注意: playbook核心元素常用的是hosts,tasks,handlers,其他两个有兴趣可以自己去研究!