Ansible 流程控制
ansible条件语句
官方示例:
tasks:
- name: "shut down Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_facts['os_family'] == "Debian"
# note that all variables can be used directly in conditionals without double curly braces
== : 等于
!= : 不等于
>: 大于
>=: 大于等于
<: 小于
<=: 小于等于
is match: 只有web匹配 可以使用通配符
ansible_hostname is match 'web*'
in not match 只有web不匹配
ansible_hostname is not match 'web*'
- hosts: all
tasks:
- name: 安装apache(centos:httpd、ubuntu:apache2)
yum:
name: httpd
state: present
when: ansible_hostname is not match 'web*'
使用小括号进行分组运算
- hosts: all
tasks:
- name: "shut down CentOS 6 and Debian 7 systems"
command: /sbin/shutdown -t now
#小括号分组运算展示
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
(ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
#相似
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "6"
## 指定多条件为列表
- hosts: all
tasks:
- name: 安装apache(centos:httpd、ubuntu:apache2)
yum:
name: httpd
state: present
when:
- ansible_hostname == 'web01'
- ansible_hostname == 'db01'
- hosts: all
tasks:
- name: 安装apache(centos:httpd、ubuntu:apache2)
yum:
name: httpd
state: present
when: ansible_hostname == 'web01' and ansible_hostname == 'db01'
- hosts: all
tasks:
- name: 安装apache(centos:httpd、ubuntu:apache2)
yum:
name: httpd
state: present
when: (ansible_hostname is match 'web*') or ansible_hostname == 'db01'
条件运算
tasks:
- shell: echo "only on Red Hat 6, derivatives, and later"
when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 6
ansible流程控制循环
注意:模块中,只有一个动作是变量时,使用列表循环
## 列表循环
- hosts: web02
tasks:
- name: 创建 zls tls ljy 三个用户
user:
name: "{{ item }}"
state: present
with_items:
- zls
- tls
- ljy
注意:模块中,有一个以上动作是变量时,使用字典循环
## 字典循环
- hosts: web_group
tasks:
- name: 推送nginx配置文件
copy:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
with_items:
- { 'src': "/root/wordpress/web/nginx.conf", 'dest': "/etc/nginx/" }
- { 'src': "/root/wordpress/web/wp.conf", 'dest': "/etc/nginx/conf.d/" }
- { 'src': "/root/wordpress/web/www.conf", 'dest': "/etc/php-fpm.d/" }
ansible的触发器handlers
handler用来执行某些条件下的任务,比如当配置文件发生变化的时候,通过notify触发handler去重启服务。
在saltstack中也有类似的触发器,写法相对Ansible简单,只需要watch,配置文件即可。
- hosts: web_group
tasks:
- name: 推送nginx配置文件
copy:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
with_items:
- { 'src': "/root/wordpress/web/nginx.conf", 'dest': "/etc/nginx/" }
- { 'src': "/root/wordpress/web/wp.conf", 'dest': "/etc/nginx/conf.d/" }
- { 'src': "/root/wordpress/web/www.conf", 'dest': "/etc/php-fpm.d/" }
when: ansible_hostname is match 'web*'
notify: restart nginx
handlers:
- name: restart nginx
service:
name: nginx
state: reloaded
注意:
1.无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次。
2.Handlers只有在其所在的任务被执行时,才会被运行;如果一个任务中定义了notify调用Handlers,但是由于条件判断等原因,该任务未被执行,那么Handlers同样不会被执行。
3.Handlers只会在每一个play的末尾运行一次;如果想在一个playbook中间运行Handlers,则需要使用meta模块来实现。例如: -meta: flush_handlers。
4.如果一个play在运行到调用Handlers的语句之前失败了,那么这个Handlers将不会被执行。我们可以使用meta模块的--force-handlers选项来强制执行Handlers,即使Handlers所在的play中途运行失败也能执行。
5.不能使用handlers替代tasks
ansible任务标签tags
默认情况下,Ansible在执行一个playbook时,会执行playbook中定义的所有任务,Ansible的标签(tag)功能可以给单独任务甚至整个playbook打上标签,然后利用这些标签来指定要运行playbook中的个别任务,或不执行指定的任务。
打标签的方式
一个任务打一个标签
[root@m01 m01]# cat tag.yml
- hosts: web_group
vars:
- http_port: 8080
tasks:
- name: Install Http Server
yum:
name: httpd
state: present
tags:
- install_httpd
[root@m01 m01]# ansible-playbook tag.yml -t install_httpd
一个任务打多个标签
[root@m01 m01]# cat tag.yml
- hosts: web_group
vars:
- http_port: 8080
tasks:
- name: Install Http Server
yum:
name: httpd
state: present
tags:
- install_httpd
- httpd_server
- name: configure httpd server
template:
src: ./httpd.j2
dest: /etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
tags:
- config_httpd
- httpd_server
[root@m01 m01]# ansible-playbook tag.yml -t install_httpd,confiure_httpd
多个任务打一个标签
[root@m01 m01]# cat tag.yml
- hosts: web_group
vars:
- http_port: 8080
tasks:
- name: Install Http Server
yum:
name: httpd
state: present
tags:
- httpd_server
- name: configure httpd server
template:
src: ./httpd.j2
dest: /etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
tags:
- httpd_server
[root@m01 m01]# ansible-playbook tag.yml -t httpd_server