天天看點

ansible劇本編寫擴充功能

ansible劇本編寫擴充功能

1. 劇本變量編寫功能

2. 劇本資訊通知功能

3. 劇本資訊判斷功能

4. 劇本資訊循環功能

5. 劇本編寫忽略錯誤

6. 劇本标簽設定功能

7. 劇本忽略采集功能

8. 劇本資訊觸發功能

  1. 劇本變量編寫功能

    設定變量方法一: 在劇本中設定變量 劇本變量其次優先

- hosts: 172.16.1.41
  vars:
	dir: /etc
	file: rsyncd.conf
	tasks:
	  - name: copy file 
		copy: src={{ dir }}/{{ file }} dest={{ dir }}/
	
	設定變量方法二: 在主機清單中設定變量  主機清單變量最不優先
	[rsync_server]
    172.16.1.41 ansible_user=root ansible_password=123456
    172.16.1.31
    [rsync_server:vars]   --- 給指定主機組統一設定變量
    dir=/etc
    file=rsyncd.conf
	
	設定變量方法三: 在劇本執行指令參數中設定變量      指令行最優先
	ansible-playbook -e dir=/etc -e file=rsyncd.conf test_變量功能.yaml
           
  1. 劇本資訊通知功能
- hosts: 172.16.1.41
  tasks:
    - name: boot server
      service: name=rsyncd state=started
    - name: check server boot
      shell: netstat -lntup|grep 873
      register: oldboy
    - debug: msg={{ oldboy.stdout_lines }}
           
  1. 劇本判斷功能說明
三台主機:
    NFS服務用戶端(nfs服務啟動)          NFS服務端
   centos7  10.0.0.7                centos7 10.0.0.31
   centos6  10.0.0.8 
   centos7  10.0.0.9
- hosts: nfs_client
  tasks:
	  - name: boot centos7 nfs
	    shell: systemctl start nfs 
		 判斷: 如果是centos7 ???
    - name: boot centos6 nfs 
        shell: /etc/init.d/nfs start	
            判斷: 如果是centos6 ???			 
   
- hosts: nfs_client
  tasks:
    - name: create file for 41 host
      file: path=/tmp/172.16.1.41 state=directory
      when: (ansible_hostname == "backup")
    - name: create file for 7 host
      file: path=/tmp/172.16.1.7  state=directory
      when: (ansible_hostname == "web01") 
- hosts: 172.16.1.41
  tasks:
    - name: create file for 41 host
      file: path=/tmp/centos state=directory
      when: (ansible_distribution == "CentOS")
 
   如何對管理主機進行判斷:
   setup --- 收集遠端主機資訊
   
   ansible_all_ipv4_addresses:				    僅顯示ipv4的資訊。
      ansible_devices:							僅顯示磁盤裝置資訊。
      ansible_distribution:						顯示是什麼系統,例:centos,suse等。
      ansible_distribution_major_version:		    顯示是系統主版本。
      ansible_distribution_version:				僅顯示系統版本。
      ansible_machine:							顯示系統類型,例:32位,還是64位。
      ansible_eth0:								僅顯示eth0的資訊。
      ansible_hostname:							僅顯示主機名。
      ansible_kernel:							    僅顯示核心版本。
      ansible_lvm:								顯示lvm相關資訊。
      ansible_memtotal_mb:						顯示系統總記憶體。
      ansible_memfree_mb:						    顯示可用系統記憶體。
      ansible_memory_mb:							詳細顯示記憶體情況。
      ansible_swaptotal_mb:						顯示總的swap記憶體。
      ansible_swapfree_mb:						顯示swap記憶體的可用記憶體。
      ansible_mounts:							    顯示系統磁盤挂載情況。
      ansible_processor:							顯示cpu個數(具體顯示每個cpu的型号)。
      ansible_processor_vcpus:					顯示cpu個數(隻顯示總的個數)。
           
  1. 劇本資訊循環功能
循環建立多個使用者
- hosts: 172.16.1.41
  tasks:
    - name: create user
      user: name={{ item }}
      with_items:
        - oldgirl01
        - oldgirl02
        - oldgirl03
        - oldgirl04
        - oldgirl
   循環建立多個使用者  多個使用者uid數值是不同的
- hosts: 172.16.1.41
  tasks:
    - name: create user
      user: name={{ item.name }} uid={{ item.uid }}
      with_items:
        - {name: "oldgirl06", uid: "3006"}
        - {name: "oldgirl07", uid: "3007"}
        - {name: "oldgirl08", uid: "3008"}
        - {name: "oldgirl09", uid: "3009"}
    - name: check create user info
      shell: grep oldgirl0 /etc/passwd
      register: user_info
    - debug: msg={{ user_info.stdout_lines }}
           
  1. 劇本編寫忽略錯誤
cat test_忽略錯誤.yaml
- hosts: 172.16.1.41
  tasks:
    #- name: install rsync
    #  shell: yum install -y rsync
    - name: create rsync user
      shell: useradd rsync -M -s /sbin/nologin
      ignore_errors: yes
    - name: create backup dir
      shell: mkdir /backup
      ignore_errors: yes
    - name: boot server
      shell: systemctl start rsyncd
      ignore_errors: y
           
  1. 劇本标簽設定功能
cat test_标簽功能.yaml 
- hosts: 172.16.1.41
  tasks:
    - name: 01:安裝軟體
      yum: name=rsync state=installed
      ignore_errors: yes
    - name: 02:建立使用者
      user: name=rsync create_home=no shell=/sbin/nologin
      ignore_errors: yes
      tags: create_user
    - name: 03:建立目錄
      file: path=/backup state=directory
   ansible-playbook -t create_user test_标簽功能.yaml             --- 執行劇本中标簽任務
   ansible-playbook --skip-tags create_user test_标簽功能.yaml    --- 跳過指定标簽任務,執行其他任務
   ansible-playbook -t create_user,create_dir test_标簽功能.yaml  --- 執行多個标簽
           
  1. 劇本忽略采集功能
cat test_忽略采集.yaml 
- hosts: 172.16.1.41
  gather_facts: no
  tasks:
    - name: 01:安裝軟體
      yum: name=rsync state=installed
      ignore_errors: yes
    - name: 02:建立使用者
      user: name=rsync create_home=no shell=/sbin/nologin
      ignore_errors: yes
      tags: create_user
    - name: 03:建立目錄
    file: path=/backup state=directory
      tags: create_dir	
   說明: 可以提升劇本執行效率; 如果劇本中有判斷功能,不能使用此參數
           
  1. 劇本資訊觸發功能
cat test_觸發功能.yaml
- hosts: 172.16.1.41
 tasks:
   - name: 01:傳輸配置檔案
     copy: src=/etc/ansible/ansible_playbook/rsyncd.conf dest=/etc/
     notify: rsync_restart
   - name: 02:啟動服務程式
     service: name=rsyncd state=started
 handlers:
   - name: rsync_restart
     service: name=rsyncd state=restarted
  說明: 整體任務執行完畢,才會執行觸發功能