1 |
在之前写playbook的过程中,我们发现,写多个playbook没有办法,一键执行,这样我们还要单个playbook挨个去执行,很鸡肋。所以在playbook中有一个功能,叫做include用来动态调用task任务列表。 |
只调用tasks
1)编写安装nginx
1 2 3 4 5 |
[root@m01 ~]# cat nginx.yml - name: Install Nginx Server yum: name: nginx state: present |
2)编写启动nginx
1 2 3 4 5 6 |
[root@m01 ~]# cat start.yml - name: Start Nginx Server systemd: name: nginx state: started enabled: yes |
3)编写调用的剧本
1 2 3 4 5 |
[root@m01 ~]# cat main.yml - hosts: nfs tasks: - include_tasks: nginx.yml - include_tasks: start.yml |
4)直接调用写好的playbook
1 2 3 |
[root@m01 ~]# cat main.yml - import_playbook: lnmp1.yml - import_playbook: lnmp2.yml |
playbook忽略错误
1 2 3 |
默认playbook会检测task执行的返回状态,如果遇到错误则会立即终止playbook的后续task执行,然而有些时候playbook即使执行错误了也要让其继续执行。 加入参数:ignore_errors:yes 忽略错误 |
一般使用
1 2 3 4 5 6 7 8 9 10 |
- name: Get PHP Install status shell: "rpm -qa | grep php" ignore_errors: yes register: get_php_install_status - name: Install PHP Server shell: yum localinstall -y /tmp/*.rpm when: - ansible_fqdn is match "web*" - get_php_install_status.rc != 0 |