18,Anisble劇本

1,ansible劇本
playbook 翻譯過來就是“劇本”, 那 playbook 組成如下
play: 定義的是主機的角色
task: 定義的是具體執行的任務
playbook: 由一個或多個 play 組成,一個 play 可以包含多個 task 任務
簡單理解爲: 使用不同的模塊完成一件事情

2,ansible劇本的優勢
1,功能比ansible命令更強大
2,能控制先後執行順序和依賴關係
3,語法更加直觀

3,ansible使用yaml語法
1)以縮進代表不同層級之間的關係
2)對縮進有嚴格要求
3)-橫槓,橫槓後面有空格代表列表
4):冒號,冒號後有空格,表示賦值

4,ansible劇本小實例模板

ansible nfs -m group -a "name=www gid=666 state=present"                                        
ansible nfs -m user -a "name=www uid=666 group=666 shell=/sbin/nologin create_home=no"                                      
ansible nfs -m yum -a "name=nfs-utils"                                      
ansible nfs -m file -a "path=/data state=directory"                                     
ansible nfs -m copy -a "src=exports dest=/etc/exports backup=yes"                                       
ansible nfs -m service -a "name=rpcbind state=started enabled=yes"                                      
ansible nfs -m service -a "name=nfs state=started enabled=yes"                                      
ansible nfs -m shell -a "showmount -e"                                      

小試牛刀:


小試牛刀:                   
- hosts: nfs                主機組 
  tasks:                     任務 
  - name: create group              取名字任意,要方便記  
    group:                                      引用的模塊   
      name: www                         參數1 
      gid: 666                                 參數2  
      state: present                        參數3 
  - name: create user                   
    user:                   
      name: www                 
      uid: 666                  
      group: www                    
      shell: /sbin/nologin                  
      create_home: no                   
  - name: install nfs soft                  
    yum:                    
      name: nfs-utils                   
  - name: mkdir directory                   
    file:                   
      path: /data                   
      state: directory                  
  - name: copy file                 
    copy:                   
      src: /root/exports                    
      dest: /etc/exports                    
      backup: yes                   
  - name: start rpcbind                 
    service:                    
      name: rpcbind                 
      state: started                    
      enbaled: yes                  
  - name: start nfs                 
    service:                    
      name: nfs                 
      state: started                    
      enbaled: yes                  
  - name: show mount                    
    shell: showmount -e

5,ansible執行方式
1)ansible-playbook --syntax-check xxx.yaml 語法檢查
2)ansible-playbook -C xxx.yaml 模擬執行
3)ansible-playbook xxx.yaml 執行劇本
6,anisble劇本高級特性-loop
使用場景:在寫ansible劇本中我們經常會寫到重複性命令,比如創建多個用戶,多個組,多個目錄,安裝多個軟件
一個個寫就太麻煩了,也體現不出ansible劇本的優越性。所以我們就要用到它的一些高級特性

- hosts: nfs                    
  tasks:                    
  - name: create directory                  
    file:                   
      path: "{{ item }}"                    
      state: present                    
    loop:                   
    - /data                 
    - /dat2                 

  - name: add group                 
   group:                   
      name: "{{ item.name }}"                   
      gid: "{{ item.gid }}"                 
      state: present                    
   loop:                    
   - { name: group1, gid: 1111 }                    
   - { name: group2, gid: 2222 }                    

  -name: add user                   
    user:                   
      name: "{{ item.name }}"                   
      uid: "{{ item.uid }}"                 
      group: "{{ item.group }}"                 
      shell: /sbin/nologin                  
      create_home: no                   
    loop:                   
    - { name: user1, uid: 7777 group: group1 }                  
    - { name: user2, uid: 8888 group: group2 }  

六,ansible劇本高級特性-變量
使用情景:
1.自定義某個名稱,在任務中會多次引用
2.從主機收集的系統信息中提取某個變量並引用,例如網卡信息

- hosts: nfs        
  var:      
    path: /opt/data     
  tasks:        
  - name: create directory      
    file:       
      path:"{{ path }}"     
      state: present        

也可以寫在/etc/ansible/hosts
18,Anisble劇本

- hsots: nfs                                        
  tasks:                                        
  - name: show ip                                       
    shell: "echo {{ ansible_facts.eth1.ipv4.address }} > /root/ip_eth1.txt"                         
    shell: "echo {{ ansible_facts.eth0.ipv4.address }} > /root/ip_eth0.txt"

(ansible內置變量提取ip地址的變量)

六,ansible劇本高級特性-註冊變量
使用情景:將配置文件的狀態註冊成一個變量,方便其他任務引用
例:

- hosts: nfs                                    
  tasks:                                    
  - name: show ip                                   
    shell: "echo {{ ansible_facts.eth1.ipv4.address }}"                                 
    register: eth1 (register是固定用法,表示註冊一個叫eth1的變量)                                   
  - name: echo eth1                                 
    debug:                                  
      msg: "{{ eth1.stdout }} " (固定用法,加.stdout表示顯示帶stdout的行因爲這行剛好有ip)

六,ansible劇本高級特性-觸發機制
使用場景:通常劇本里定義的配置文件一旦修改後,我們都要重啓服務,但是ansible定義的service模塊只能state: started
所以要有一個觸發機制,當配置文件修改,服務自動重啓

- hosts: nfs            
  tasks:            
- name: copy export         
    copy:           
      src: /root/exports            
      dest: /etc/exports            
      backup: yes           
    notify: restart_nfs_server          
  handlers:         
  - name: restart_nfs_server            
    service:            
      name: "{{ item }}"            
      state: restarted          
    loop:           
    - rpcbind           
    - nfs

六,ansible劇本高級特性-標籤tags
應用場景:給劇本里執行的每個模塊打上tags,在你執行的時候你可以靈活指定執行哪個模塊或者對於報錯的模塊單獨執行,而不需要再從頭執行一遍

- hosts: 172.16.1.41                            
  tasks:                            
  - name: 01-add group                          
    group: name=www gid=666                         
    tags: 01-add-group                          
  - name: 02-add user                           
    user: name=www create_home=no shell=/sbin/nologin group=www uid=666                         
    tags: 02-add-user                           
  - name: 03-install rsync                          
    yum: name=rsync state=installed                         
    tags: 03-install-rsync                          
  - name: 04-copy rsync conf                            
    copy: src=/server/scripts/rsyncd.conf dest=/etc/                            
    tags: 04-copy-conf                          
  - name: 05-create passwd conf                         
    copy: content='rsync_backup:oldboy' dest=/etc/rsync.passwd mode=600                         
    tags: 05-create-passwd                          
  - name: 06-create backup dir                          
    file: path=/backup state=directory owner=www group=www                          
    tags: 06-create-backup                          
  - name: 07-create backup dir                          
    file: path=/data state=directory owner=www group=www                            
    tags: 07-create-data                            
  - name: 08-start rsyncd service                           
    service: name=rsyncd state=started                          
    tags: 08-start-rsyncd                           
  - name: 09-enabled rsyncd service                         
    systemd: name=rsyncd enabled=yes                            
    tags: 09-enable 

1)指定運行某個tags:
ansible-playbook -t 05-create-passwd tags2.yml
2)指定運行多個tags:
ansible-playbook -t 05-create-passwd,06-create-backup tags2.yml
3)指定不運行某個tags:
ansible-playbook --skip-tags=05-create-passwd tags2.yml
4)指定不運行多個tags:
ansible-playbook --skip-tags=05-create-passwd,06-create-backup tags2.yml

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章