ansible playbook中when的多種用法和playbook handler

回顧

劇本中可使用判斷的方式,減小hosts(play)的個數

template  jinjia2
劇本中不能使用if判斷,使用when判斷

shutdown -a 取消關機
shutdown -s 關機
shutdown -f 強行關閉應用程序
shutdown -m \\計算機名 控制遠程計算機
shutdown -i 顯示「遠程關機」圖形用戶界面,但必須是Shutdown的第一個參數  
shutdown -l 註銷當前用戶
shutdown -r 關機並重啓
shutdown -s -t 時間 設置關機倒計時
shutdown -h 休眠

centos6啓動httpd		/etc/init.d/httpd start

變量的使用並不能減小代碼量,使用循環就能夠減小代碼量了
還原快照要從新推送m01上的公鑰,才能使用ansible
bool值純數字要加引號,字符串不用加
yum localinstall 在劇本中不會報錯

文件類型:str 
		int	字符串類型
python中文件類型的區分是很嚴格的,
劇本中變量加雙引號

循環通常在啓動服務或者copy的時候使用
yum支持列表,通常不用循環

命令行不支持字典的形式調用變量,playbook支持

根據不一樣的操做系統安裝apachephp

官方示例:html

- hosts: all
  tasks:
    - name: "shut down Debian flavored systems"
      command: /sbin/shutdown -t now
      when: ansible_facts['os_family'] == "Debian"		#不等於表示:!= 0
      # 注意,'全部變量'均可以直接在條件語句中使用,而無需使用雙大括號
  - hosts: web_group
    tasks:
      - name: Install CentOS Httpd
        yum:
          name: httpd
          state: present
      #官方
        when: ansible_['os_family'] == "CentOS"		#判斷系統
        when: ansible.os_family == "CentOS"
      #非官方()
        when: ansible_distribution == "CentOS"
  
      - name: Install Ubuntu Httpd
        yum:
          name: apache2
          state: present
        when: ansible_facts['os_family'] == "Ubuntu"
        
  when後面既能夠是變量,又能夠是指定值,通常後面跟變量,與hosts一塊兒使用     
[root@www ~]# ansible web01 -m setup |grep os_family
        "ansible_os_family": "RedHat",
when的縮進和name註釋同樣  
#facts 指的是 ansible_facts 變量,ansible 中使用 setup 模塊來獲取,包含系統的大部分基礎硬件信息

還可使用括號,and , or對條件進行分組python

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")
          
        #使用ansible_facts['distribution'] 判斷系統  注意大小寫

也能夠指定多條件爲列表(and 而且)linux

tasks:
  - name: "shut down CentOS 6 systems"
    command: /sbin/shutdown -t now
    when:
      - ansible_facts['distribution'] == "CentOS"
      - ansible_facts['distribution_major_version'] == "6"
      
      #列表形式等效於and

條件運算nginx

tasks:
  - shell: echo "only on Red Hat 6, derivatives, and later"
    when: ansible_facts['os_family'] == "RedHat" and ansible_facts['lsb']['major_release']|int >= 6		#

rsync服務端推送配置文件web

[root@m01 ~]# cat rsyncd/rsyncd.yml
- hosts: all						######
  tasks:
    - name: Install Rsyncd Server
      yum:
        name: rsync
        state: present					#可在這裏使用ls -l 判斷rsync是否安裝

    - 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" 		#判斷主機名

    - 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"

rsync客戶端推送腳本shell

[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*"		#when支持通配符
      when: ansible_hostname ~= "web*"		
      #when: ansible_hostname == "backup" or ansible_hostname == "nfs"
      #這三種方式相似模糊匹配,均可以匹配多臺web
      #模糊匹配和and or不能一塊兒使用

經過register將命令執行結果保存至變量,而後經過when語句進行判斷apache

- 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
#經過變量註冊的方式能夠進行非系統變量的調用,與'register: check_httpd'對應

#htpd
[root@lb01 ~]# systemctl is-active httpd
active
[root@lb01 ~]# systemctl stop httpd
[root@lb01 ~]# systemctl is-active httpd
unknown

#nginx
[root@lb01 ~]# systemctl is-active nginx
active
[root@lb01 ~]# systemctl stop nginx
[root@lb01 ~]# systemctl is-active nginx
failed

playbook循環語句

在以前的學習過程當中,咱們常常會有傳送文件,建立目錄之類的操做,建立2個目錄就要寫兩個file模塊來建立,若是要建立100個目錄,咱們須要寫100個file模塊???媽耶~~~~ 固然不是,只要有循環便可,減小重複性代碼。vim


啓動多個服務centos

- hosts: web_group
  tasks:
    - name: start service
      systemd:
        name: "{{ item }}"
        state: started
      with_items:
        - httpd
        - php-fpm
        - mariadb

定義變量循環

- name: ensure a list of packages installed
  yum:
    name: "{{ packages }}"
  vars:									#模塊內定義變量
    packages:
    - httpd
    - httpd-tools
- hosts: web_group
  tasks:
    - name: ensure a list of packages installed
      yum: name= "{{ item }}" state=present			#可使用多個'='
      with_items:
        - httpd
        - httpd-tools
        
#with_items通常放到模塊的末尾,與模塊同一縮進級別

字典循環

1.建立用戶

[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' }

2.拷貝文件

- 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" }
        
        #同一模塊在一個劇本中屢次出現,便可考慮使用循環
        #同一模塊在一個劇本中屢次出現,對同一主機屢次操做,便可考慮字典循環

playbook handler

handler用來執行某些條件下的任務,好比當配置文件發生變化的時候,經過notify觸發handler去重啓服務。

實踐案例

[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

練習:多個nginx配置文件的推送及觸發器

注意:
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任務標籤

默認狀況下,Ansible在執行一個playbook時,會執行playbook中定義的全部任務,Ansible的標籤(tag)功能能夠給單獨任務甚至整個playbook打上標籤,而後利用這些標籤來指定要運行playbook中的個別任務,或不執行指定的任務。


打標籤的方式

1.對一個task打一個標籤
2.對一個task打多個標籤
3.對多個task打一個標籤


打完標籤如何使用

-t:執行指定的tag標籤任務
--skip-tags:執行--skip-tags以外的標籤任務


使用-t指定tag

[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

    - name: start httpd server
      service:
        name: httpd
        state: started
        enabled: yes
      tags: service_httpd

  handlers:
    - name: Restart Httpd Server
      systemd:
        name: httpd
        state: restarted 

[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

playbook文件複用

在以前寫playbook的過程當中,咱們發現,寫多個playbook沒有辦法,一鍵執行,這樣咱們還要單個playbook挨個去執行,很雞肋。因此在playbook中有一個功能,叫作include用來動態調用task任務列表。

img

只調用task:include_tasks
調用整個task文件:include (新版本:import_playbook)

在saltstack中,叫作top file入口文件。

示例一:

[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

示例三

- import_playbook: httpd.yml
- import_playbook: nfs.yml
- import_playbook: rsync.yml

playbook忽略錯誤

默認playbook會檢測task執行的返回狀態,若是遇到錯誤則會當即終止playbook的後續task執行,然鵝有些時候playbook即便執行錯誤了也要讓其繼續執行。

加入參數:ignore_errors:yes 忽略錯誤

[root@m01 ~]# cat ignore.yml
---
- hosts: web_group
  tasks:
    - name: Ignore False
      command: /bin/false
      ignore_errors: yes
      
    - name: touch new file
      file:
        path: /tmp/zls.txt
        state: touch

playbook錯誤處理

如上所述,當task執行失敗時,playbook將再也不繼續執行,包括若是在task中設置了handler也不會被執行。

可是咱們能夠採起強制措施...


強制調用handler

[root@m01 ~]# cat handler.yml 
- hosts: web_group
  vars:
    - http_port: 8080
  force_handlers: yes
  tasks:

    - name: config httpd server
      template:
        src: ./httpd.j2
        dest: /etc/httpd/conf
      notify: 
        - Restart Httpd Server
        - Restart PHP Server

    - name: Install Http Server
      yum:
        name: htttpd
        state: present

    - 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

抑制changed

被管理主機沒有發生變化,可使用參數將change狀態改成ok

[root@m01 ~]# cat handler.yml 
- hosts: web_group
  vars:
    - http_port: 8080
  force_handlers: yes
  tasks:
    - name: shell
      shell: netstat -lntup|grep httpd
      register: check_httpd
      changed_when: false

    - name: debug
      debug: msg={{ check_httpd.stdout.lines }}
[root@m01 project2]# cat changed_when.yml 
- hosts: webservers
  vars:
    - http_port: 8080
  tasks:
    - name: configure httpd server
      template:
        src: ./httpd.j2
        dest: /etc/httpd/conf/httpd.conf
      notify: Restart Httpd Server

    - name: Check HTTPD
      shell: /usr/sbin/httpd -t
      register: httpd_check
      changed_when: 
        - httpd_check.stdout.find('OK')
        - false

    - name: start httpd server
      service:
        name: httpd
        state: started
        enabled: yes

  handlers:
    - name: Restart Httpd Server
      systemd:
        name: httpd
        state: restarted
相關文章
相關標籤/搜索