A: playbook conditional statements --- when judgment
1. Depending on the installation of the operating system nginx
#官方示例:
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
- hosts: web_group
tasks:
- name: Install CentOS nginx
yum:
name: nginx
state: present
#官方
when: ansible_facts['os_family'] == "CentOS"
#非官方
when: ansible_distribution == "CentOS"
- name: Install Ubuntu nginx
yum:
name: apache2
state: present
when: ansible_facts['os_family'] == "Ubuntu"
2. Conditions packet parentheses
tasks:
- name: "shut down CentOS 6 and Debian 7 systems"
command: /sbin/shutdown -t now
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "6") or
(ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "7")
3. specify multiple conditions for the list
tasks:
- name: "shut down CentOS 6 systems"
command: /sbin/shutdown -t now
when:
- ansible_facts['distribution'] == "CentOS"
- ansible_facts['distribution_major_version'] == "6"
Case: rsync Push Profile (early learning)
#服务端:
[root@m01 ~]# cat rsyncd/rsyncd.yml
- hosts: rsync_server
tasks:
- name: Install Rsyncd Server
yum:
name: rsync
state: present
- name: Create www Group
group:
name: www
gid: 666
- name: Create www User
user:
name: www
group: www
uid: 666
create_home: false
shell: /sbin/nologin
- name: Scp Rsync Config
copy:
src: ./rsyncd.j2
dest: /etc/rsyncd.conf
owner: root
group: root
mode: 0644
when: ansible_hostname == "backup"
#ansible_fqdn == "backup"
- name: Create Passwd File
copy:
content: 'rsync_backup:123'
dest: /etc/rsync.passwd
owner: root
group: root
mode: 0600
when: ansible_hostname == "backup"
- name: Create backup Directory
file:
path: /backup
state: directory
mode: 0755
owner: www
group: www
recurse: yes
when: ansible_hostname == "backup"
- name: Start Rsyncd Server
systemd:
name: rsyncd
state: started
when: ansible_hostname == "backup"
#客户端
[root@m01 ~]# vim rsync.yml
- hosts: rsync_server
tasks:
- name: SCP Backup Shell
copy:
src: ./backup.sh
dest: /root/backup.sh
when: ansible_hostname is match "web*"
Precautions: 1.When needs and aligned module
2. If using regular need "is match" to match
3. Normally judged "==" if not equal "! ="
4. By the command execution result storage register to a variable, then when statement is determined by
- hosts: web_group
tasks:
- name: Check Httpd Server
command: systemctl is-active httpd
ignore_errors: yes #忽略错误,方便下一步继续执行
register: check_httpd
- name: Httpd Restart
service:
name: httpd
state: started
when: check_httpd.rc != 0
说明:
1.systemctl is-active httpd 查看httpd是否启动,没有启动会报错rc=3(0为正常)
2.register设置变量check_httpd
3.when判断check_httpd.rc是否等于0
4.如果不等于0,将开启nginx服务
Two: playbook loop
Such as: start multiple services or create multiple simultaneous users mysql
1. Define variable cycle
EX1:启动多个服务
- hosts: web_group
tasks:
- name: start service
systemd:
name: "{{ item }}"
state: started
with_items:
- httpd
- php-fpm
- mariadb
EX2:安装多个服务
- hosts: web_group
tasks:
- name: ensure a list of packages installed
yum: name= "{{ item }}" state=present
with_items:
- httpd
- httpd-tools
2. circulating dictionary
EX1:创建多个用户
[root@m01 ~]# cat loop.yml
- hosts: web_group
tasks:
- name: Add Users
user:
name: "{{ item.name }}"
groups: "{{ item.groups }}"
state: present
with_items:
- { name: 'zls', groups: 'linux' }
- { name: 'egon', groups: 'python' }
EX2:拷贝多个文件
- hosts: web_group
tasks:
- name: Configure Nginx Websit PHP Conf
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode }}"
with_items:
- {src: './nginx.j2',dest: '/etc/nginx/nginx.conf',mode: '0644'}
- {src: './php.drz.com.j2',dest: '/etc/nginx/conf.d/php.drz.com.conf',mode: '0755'}
- {src: './php.j2',dest: '/etc/php-fpm.d/www.conf',mode: '0000'}
三:playbook handlers
handler
Used to perform tasks under certain conditions, such as when the configuration file changes, triggered by notify handler to restart the service.
In saltstack also has a similar flip-flops, written Ansible relatively simple, only need to watch, configuration files.
Case
[root@m01 ~]# cat handler.yml
- hosts: web_group
vars:
- http_port: 8080
tasks:
- name: Install Http Server
yum:
name: httpd
state: present
- name: config httpd server
template:
src: ./httpd.j2
dest: /etc/httpd/conf
notify:
- Restart Httpd Server
- Restart PHP Server
- name: start httpd server
service:
name:httpd
state: started
enabled: yes
handlers:
- name: Restart Httpd Server
systemd:
name: httpd
state: restarted
- name: Restart PHP Server
systemd:
name: php-fpm
state: restarted
Note:
1. No matter how many task notification with the same handlers, handlers will run at the end of all the tasks only once.
2.Handlers only when the tasks they are to perform, will be run; if a task is defined notify call Handlers, but due to the condition judgment and other reasons, the task is not performed, the Handlers same will not be executed.
3.Handlers only at the end of each run of a play once; if you want to run Handlers in the middle of a playbook, you need to use meta module. For example: -meta: flush_handlers.
4. If a running play before calling Handlers statement fails, then the Handlers will not be executed. We can use meta module option --force-handlers to enforce Handlers, play midway fail even where Handlers can be performed.
The handlers can not use the alternative tasks