Ansible-playbook管理複雜任務

一、 playbook

部署環境、搭建服務、修改配置過程中,對於需反覆執行的、較爲複雜的任務,我們可以用Playbook完成。playbook通過YAML格式進行描述定義,可以實現多臺主機應用的部署。

二、yml文件

# cd /etc/ansible
# vim copy.yml

文件內容:

---                           //文檔標誌符
- hosts: server1     //指定主機
  remote_user: root  //指定在被管理的主機上執行任務的用戶
  tasks:      //任務列表
  - name: create user    //任務名
    user: name=wang_06    //調用user模塊
  - name: create directory
    command: mkdir /wh_k/wang_06     //調用command模塊
  - name: chmod
    command: chown wang_06:wang_06 /wh_k/wang_06  //調用command模塊
  - name: copy file
    copy: src=/wh_k/test1.txt dest=/wh_k/wang_06/  //調用copy模塊

# ansible-playbook --check copy.yml
# ansible-playbook  copy.yml        

備註

 1.第一行中,文件開頭爲 ---;這是YAML將文件解釋爲正確的文檔的要求。YAML一個文件支持多個文檔,每個“文檔”由 --- 符號分割,Ansible只需要一個文件存在一個文檔即可。

 2.YAML對空格非常敏感,空格和縮進要注意

 3.任務列表中的各任務按次序逐個在hosts中指定的所有主機上執行,完成第一個任務後再開始第二個。在自上而下運行某playbook時如果中途發生錯誤,所有該主機上已執行任務都將回滾。

三、示例詳解

# vim copy.yml
---                      //文檔標誌符
- hosts: server1     //定義組和主機
  remote_user: root  // 管理機上執行任務的用戶
  gather_facts: false   // 獲取主機相關信息 true|false

  vars:           
   - user: "wang_06"  //定義變量

  tasks:
  - name: create user
    command: grep {{user}} /etc/passwd
    ignore_errors: True
    register: result      //查看用戶是否存在

  - shell: echo `date +%Y-%m-%d_%H:%M:%S` ":User exit.">>/wh_k/ansible.log
    when: result|succeeded   //用戶存在,輸出信息

  - user: name="{{user}}"
    when: result|failed          //用戶不存在,新建用戶

  - name: create directory
    shell: ls /wh_k/{{user}}
    ignore_errors: True
    register: result            //目錄是否存在

  - command: mkdir /wh_k/{{user}}     //command可換成shell
    when: result|failed     //目錄不存在,新建目錄

  - shell: echo `date +%Y-%m-%d_%H:%M:%S`":File is exit,create file failed." >>/wh_k/ansible.log
    when: result|succeeded  //目錄存在,輸出信息

  - name: chmod
    command: chown {{user}}:{{user}} /wh_k/{{user}}   //賦權

  - name: copy file
    copy: src=/wh_k/test1.txt dest=/wh_k/{{user}}/        //拷貝文件
    notify: insert messages     //當拷貝文件執行正確時,執行調用的操作

  handlers:
  - name: insert messages
    shell: echo `date +%Y-%m-%d_%H:%M:%S`"Ansible  done.">>/wh_k/{{user}}/test1.txt         //定義被調用的操作

說明:

1.gather_facts

gather_facts參數指定了在以下任務部分執行前,是否先執行setup模塊獲取主機相關信息  
gather_facts: no 或者是false是關閉,gather_facts:yes 或者是true都是開啓

如:

...
gather_facts: true       //開啓facts模塊
...
- name: test
  shell: touch /tmp/test.txt
  when: facter_ipaddress == "192.168.61.128"    // 主機地址是xxx時,執行操作
        //  facter_ipaddress調用的是facts模塊的值,當facts模塊開啓時,任務才能執行

2.vars

定義變量需要用引號引起來,調用變量需要用大括號引用

如:

vars:
- user: "wang_06"
....
  shell: ls /wh_k/{{user}}

3.檢測是否存在

執行相關命令,返回succeeded表示存在,返回false表示不存在

如:

 ...
shell: ls /wh_k/{{user}}
ignore_errors: True
register: result

4.notify

當我們執行 tasks 後,服務器發生變化之後我們要執行一些操作。
上面示例中,只有 copy 模塊真正執行後,纔會去調用下面的 handlers 相關的操作。
也就是說如果 src 和 dest 內容是一樣的,並不會去執行 handlers 裏面的 shell 相關命令。
所以這種比較適合配置文件發生更改後,需要重啓服務的操作。

5.handlers

定義追加執行的操作,由notify調用。

6.循環執行

某臺主機上批量執行任務,比如修改text1.txt 和 test2.txt ...文件屬性

如:

 ...
 tasks:
 - name: change mode
    shell: chmod 755 /tmp/{{item}}
    with_items:
     - test1.txt
     - test2.txt
 ...
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章