ansible 流程式控制制 使用when判斷主機名 2.使用when判斷系統 3.使用when判斷系統版本 4.使用註冊變數對返回值進行判斷 ansible迴圈語句 1.with_items 2.變數迴圈 3.字典迴圈 ansible handlers(觸發器) 註意: 1.無論多少個task通知了相 ...
ansible 流程式控制制
使用when判斷主機名
- hosts: rsync_server
tasks:
- name: Install rsyncd Server
yum:
name: rsync
state: present
- name: Config rsyncd Conf
copy:
src: ./rsyncd.j2
dest: /etc/rsyncd.conf
owner: root
group: root
mode: 0644
when: ansible_fqdn == 'backup'
- name: Create dir
file:
path: /backup
state: directory
owner: www
group: www
mode: 0755
recurse: yes
when: ansible_fqdn == 'backup'
- name: Create passwd file
copy:
content: "rsync_backup:123"
dest: /etc/rsync.passwd
owner: root
group: root
mode: 0600
when: ansible_fqdn == 'backup'
#單條件判斷
- name: Start rsyncd
systemd:
name: rsyncd
state: started
enabled: yes
when: ansible_fqdn == 'backup'
#多條件判斷,使用小括弧分組
- name: copy shell
template:
src: ./backup.sh
dest: /root
when: (ansible_fqdn == 'web01') or (ansible_fqdn == 'web02')
#多條件判斷,使用list列表形式
- name: copy shell
template:
src: ./backup.sh
dest: /root
when:
- ansible_fqdn == 'web01'
- ansible_fqdn == 'web02'
#多條件判斷,使用is match 支持通配符
- name: Add Crontab
cron:
name: "backup"
minute: "00"
hour: "01"
job: "/bin/sh /root/backup.sh &>/dev/null"
when: ansible_fqdn is match 'web*'
2.使用when判斷系統
- hosts: webs
tasks:
- name: Install CentOS Apache
yum:
name: httpd
state: present
when: ansible_distribution == 'CentOS'
- name: Install Ubuntu Apache
apt:
name: apache2
state: present
when: ansible_distribution == 'Ubuntu'
3.使用when判斷系統版本
- hosts: webs
tasks:
- name: Start CentOS6 Httpd
shell: "/etc/init.d/httpd start"
when: ansible_distribution_major_version == '6'
- name: Start CentOS7 Httpd
shell: "systemctl start httpd"
when: ansible_distribution_major_version == '7'
4.使用註冊變數對返回值進行判斷
- hosts: web_group
tasks:
- name: Check Httpd Server
command: systemctl is-active httpd
ignore_errors: yes
register: check_httpd
- name: debug outprint
debug: var=check_httpd
- name: Httpd Restart
service:
name: httpd
state: restarted
when: check_httpd.rc == 0
- name: pan duan rpm bao
shell: "rpm -qa|grep php"
register: check_php
- name: Install php
shell: "cd /usr/local/src && rpm -Uvh *rpm"
when: check_php.rc != 0
ansible迴圈語句
1.with_items
- name: start php and nginx
systemd:
name: "{{ item }}"
state: started
enabled: yes
with_items:
- nginx
- php-fpm
2.變數迴圈
- name: ensure a list of packages installed
yum:
name: "{{ packages }}"
vars:
packages:
- httpd
- httpd-tools
3.字典迴圈
- hosts: web_group
tasks:
- name: copy conf and code
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode }}"
with_items:
- { src: "./httpd.conf", dest: "/etc/httpd/conf/", mode: "0644" }
- { src: "./upload_file.php", dest: "/var/www/html/", mode: "0600" }
- name: tar php and nginx and wordpress
unarchive:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: "{{ item.user }}"
group: "{{ item.user }}"
copy: yes
with_items:
- { src: "./php.tgz", dest: "/usr/local/src", user: "root" }
- { src: "./nginx-1.16.0.tar.gz", dest: "/root", user: "root" }
- { src: "./wordpress.tgz", dest: "/code", user: "www" }
ansible handlers(觸發器)
- name: scp nginx shell conf
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
with_items:
- { src: "./nginx.service", dest: "/usr/lib/systemd/system" }
- { src: "./nginx.conf", dest: "/usr/local/nginx/conf/" }
- { src: "./www.drz.com.conf", dest: "/usr/local/nginx/conf/conf.d" }
#添加觸發器
notify:
- reload nginx
- reload php
handlers:
- name: reload nginx
systemd:
name: nginx
state: reloaded
- name: reload php
systemd:
name: php
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
playbook復用include
1.只調用task include_tasks
[root@m01 web]# vim install_nginx.yml
- name: Install nginx
yum:
name: nginx
state: present
[root@m01 web]# vim conf_nginx.yml
- name: conf nginx
copy:
src: ./www.drz.com.conf
dest: /etc/nginx/conf.d
notify: reload nginx
[root@m01 web]# vim main.yml
- hosts:
tasks:
- include_tasks: ./install_nginx.yml
- include_tasks: ./conf_nginx.yml
handlers:
- name: reload nginx
systemd:
name: nginx
state: reloaded
[root@m01 m01]# ansible-playbook tag.yml --list-tags
[root@m01 m01]# ansible-playbook tag.yml -t httpd_server
[root@m01 m01]# ansible-playbook tag.yml -t install_httpd,confiure_httpd
[root@m01 m01]# ansible-playbook tag.yml --skip-tags httpd_server
2.直接復用寫好的yml文件
- 舊版:
include
- 新版:
import_playbook
- 在saltstack中,叫做top file入口文件
- import_playbook: ./lnmp.yml
- import_playbook: ../mariadb/mysql.yml
示例一:
[root@m01 m01]# cat task.yml
- hosts: web_group
vars:
- http_port: 8080
tasks:
- include_tasks: task_install.yml
- include_tasks: task_configure.yml
- include_tasks: task_start.yml
handlers:
- name: Restart Httpd Server
systemd:
name: httpd
state: restarted
[root@m01 m01]# cat task_install.yml
- name: Install Http Server
yum:
name: httpd
state: present
[root@m01 m01]# cat task_configure.yml
- name: configure httpd server
template:
src: ./httpd.j2
dest: /etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
[root@m01 m01]# cat task_start.yml
- name: start httpd server
service:
name: httpd
state: started
enabled: yes
示例二:
- include: httpd.yml
- include: nfs.yml
- include: rsync.yml
示例三:
- include: httpd.yml
- include: nfs.yml
- include: rsync.yml