Ansible---Playbook基本案例

PlayBook介紹

  • playbook是由一個或者多個play組成的列表,主要功能是將task定義好的角色歸併爲一組進行統一管理。
  • playbooks自己組成部分有以下幾份:
一、tasks:任務,即調用模塊完成的操做
二、variables:變量
三、templates:模板
四、handlers:處理器,當條件知足時執行操做,一般前面使用notify聲明。
五、roles:角色,分門別類管理playbook的一種方式,後面將詳細介紹roles的用法。

playbook--yaml文件格式內容

  • 編寫yaml文件,注意不一樣層級之間嚴格的格式要求。

vim /opt/test.yamlphp

- hosts: webserver              //定義的主機組,即應用的主機
  vars:                        //定義變量
    http_port: 80              //此處自定義變量中變量寫在yaml文件,也可寫在ansible/hosts文件中應用主機後
    max_clients: 200
  remote_user: root      //遠程用戶爲root
  tasks:                               //執行的任務
  - name: ensure apache is at the latest version   //下面即將執行的步驟名稱
    yum: pkg=httpd state=latest
  - name: write the apache config file   //下面即將執行的步驟名稱
    template: src=/srv/httpd.j2 dest=/etc/httpd.conf    //模板文件,src爲路徑爲控制端,dest爲被控制端
    notify:          //聲明須要執行的操做
    - restart apache  //操做名稱
  - name: ensure apache is running
    service: name=httpd state=started
  handlers:                       //處理器,處理名稱須要對應notify的名稱,才能自動執行
    - name: restart apache
      service: name=httpd state=restarted

playbook基本命令格式

ansible-playbook test.yaml --syntax-check    #檢查yaml文件的語法是否正確
ansible-playbook test.yaml --list-task       #檢查tasks任務
ansible-playbook test.yaml --list-hosts      #檢查生效的主機
ansible-playbook test.yaml --start-at-task='Copy Nginx.conf'     #指定從某個task開始運行

hosts和users介紹

---                             #表示該文件是YAML文件,非必須
- hosts: webserver               #指定主機組,能夠是一個或多個組。
  remote_user: root                #指定遠程主機執行的用戶名

還能夠爲每一個任務定義遠程執行用戶:
---
- hosts: mysql
  remote_user: root             
  tasks:
    - name: test connection
      ping:
      remote_user: mysql          #指定遠程主機執行tasks的運行用戶爲mysql
  • 執行playbook:

ansible-playbook ping.yaml mysql

  • 指定遠程主機sudo切換用戶:
---
- hosts: mysql
  remote_user: root            
  become: yes                  #2.6版本之後的參數,以前是sudo,意思爲切換用戶運行
  become_user: mysql          #指定sudo用戶爲mysql

tasks列表和action

  • 1.Play的主體部分是task列表,task列表中的各任務按次序逐個在hosts中指定的主機上執行,即在全部主機上完成第一個任務後再開始第二個任務。
    在運行playbook時(從上到下執行),若是一個host執行task失敗,整個tasks都會回滾,請修正playbook 中的錯誤,而後從新執行便可。
    Task的目的是使用指定的參數執行模塊,而在模塊參數中可使用變量,模塊執行時冪等的,這意味着屢次執行是安全的,由於其結果一致。
  • 2.每個task必須有一個名稱name,這樣在運行playbook時,從其輸出的任務執行信息中能夠很好的辨別出是屬於哪個task的。若是沒有定義name,‘action’的值將會用做輸出信息中標記特定的task,也就是會報錯。
  • 3.定義一個task,常見的格式:」module: options」 例如:yum: name=httpd
  • 4.ansible的自帶模塊中,command模塊和shell模塊無需使用key=value格式

Handslers介紹

  • Handlers也是一些task的列表,和通常的task並無什麼區別。是由通知者進行的notify,若是沒有被notify,則Handlers不會執行,假如被notify了,則Handlers被執行,無論有多少個通知者進行了notify,等到play中的全部task執行完成以後,handlers也只會被執行一次。web

  • 示例:
- hosts: webserver
  remote_user: root
  tasks:
   - name: install httpd package
     yum: name=httpd state=latest
   - name: install configuration file for httpd
     copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
     notify:            //聲明後面Handlers的名稱
      -restart httpd    //注意該名稱爲notify的子集,須要靠空格隔開
   - name: start httpd service
     service: enabled=true name=httpd state=started
  handlers:
   - name: restart httpd
     service: name=httpd state=restarted

也能夠引入變量
    ---
- hosts: webserver
  remote_user: root
  vars:               //引入變量
  - package: httpd    //變量名稱
  - service: httpd
  tasks:
   - name: install httpd package
     yum: name={{package}} state=latest    //引入變量方式
   - name: install configuration file for httpd
     copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
     notify:
      -restart httpd
   - name: start httpd service
     service: enabled=true name={{service}} state=started
  handlers:
   - name: restart httpd     //handlers執行的操做必須是notify中聲明的操做名稱,若此處執行的操做名稱notify中沒有聲明,那麼handlers不會執行
     service: name={{service}} state=restarted
  • 在引入變量過程當中,若針對一個組裏不一樣主機須要引入的變量值不相同時,好比IP地址等,則須要在hosts文件中,在不一樣主機後面跟上變量名稱與值,yaml文件中執行操做時引入變量名稱便可。sql

  • 引用主機變量

vim /etc/ansible/hostsshell

[webserver]
192.168.144.110 httpd_port="192.168.144.110:80" server_name="www.yun01.com:80"
192.168.144.111 httpd_port="192.168.144.111:80" server_name="www.yun02.com:80"

Templates用法

  • Jinja是基於Python的模板引擎。template類是jinja的另外一個重要組件,能夠看作是編譯過的模板文件,用來生產目標文件,傳遞Python的變量給模板去替換模板中的標記。apache

  • 製做模板文件

cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2 複製模板,注意文件格式爲j2結尾
vim /opt/httpd.conf.j2vim

  • 引入變量至模板文件中,後面爲自定義變量複製,涉及到不一樣主機不一樣變量值得須要在hosts文件中主機後設定,相同變量值可直接在yaml文件中設定。
Listen {{httpd_port}}          //爲須要修改的部分設定變量
...

ServerName {{server_name}}
  • 在Apache配置文件中,如上修改部分爲每臺主機獨立變量值,須要在hosts文件中指定。

vim /etc/ansible/hosts安全

[webserver]
192.168.144.110 httpd_port="192.168.144.110:80" server_name="www.yun01.com:80"
192.168.144.111 httpd_port="192.168.144.111:80" server_name="www.yun02.com:80"
  • 編寫playbook執行yaml文件

vim /opt/httpd.yaml服務器

- hosts: webserver    //針對webserver組執行
  vars:
    package: httpd   //變量名稱
    server: httpd
  tasks:
    - name: install httpd
      yum: name={{package}} state=latest
    - name: write the apache config file
      template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf      //注意模板文件位置
      notify:
        restart httpd
    - name: start httpd
      service: name={{server}} enabled=true state=started
  handlers:
    - name: restart httpd
      service: name={{server}} state=restarted

條件測試

  • 若是須要根據變量、facts(setup)或此前任務的執行結果來做爲某task執行與否的前提時要用到條件測試,在Playbook中條件測試使用when子句。
  • 在task後添加when子句便可使用條件測試:when子句支持jinjia2表達式或語法,例如:
vi when.yml
---
- hosts: mysql
  remote_user: root
  tasks:
    - name: "shutdown CentOS"
      command: /sbin/shutdown -h now
      when: ansible_distribution == "CentOS"
  • 多條件判斷
vi when.yml
---
- hosts: mysql
  remote_user: root
  tasks:
    - name: "shut down CentOS 7 systems"
      command: /sbin/shutdown -r now
      when:
        - ansible_distribution == "CentOS"
        - ansible_distribution_major_version == "7"
  • 組條件判斷
vi when.yml
---
- hosts: mysql
  remote_user: root
  tasks:
    - name: "shut down CentOS 6 and Debian 7 systems"
      command: /sbin/shutdown -t now
      when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
            (ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
  • 自定義變量進行條件測試
vi when.yml
---
- hosts: all
  vars:
    exist: "True"
  tasks:
  - name: creaet file
    command:  touch /tmp/test.txt
    when: exist | match("True")

  - name: delete file
    command:  rm -rf /tmp/test.txt
    when: exist | match("False")

迭代

  • 當有須要重複性執行的任務時,可使用迭代機制。其使用格式爲將須要迭代的內容定義爲item變量引用,並經過with_items語句指明迭代的元素列表便可。例如:
---
- hosts: webserver
  remote_user: root
  tasks:
    - name: "Install Packages"
      yum: name={{ item }} state=latest
      with_items:
        - httpd
        - mysql-server
        - php
  • 也能夠本身定義
---
- hosts: webserver
  remote_user: root
  tasks:
    - name: "Add users"
      user: name={{ item.name }} state=present groups={{ item.groups }}
      with_items:
        - { name:'test1', groups:'wheel'}
        - { name:'test2', groups:'root'}

TAG模塊

  • 在一個playbook中,咱們通常會定義不少個task,若是咱們只想執行其中的某一個task或多個task時就可使用tags標籤功能了,格式以下:
vi hosts.yml
---
- hosts: webserver
  remote_user: root
  tasks:
    - name: Copy hosts file
      copy: src=/etc/hosts dest=/etc/hosts
      tags:
      - only
    - name: touch file
      file: path=/opt/hosts state=touch

執行命令:ide

ansible-playbook hosts.yml --tags="only"
ansible-playbook hosts.yml

  • 事實上,不光能夠爲單個或多個task指定同一個tags。playbook還提供了一個特殊的tags爲always。做用就是當使用always當tags的task時,不管執行哪個tags時,定義有always的tags都會執行。
vi hosts.yml
---
- hosts: webserver
  remote_user: root
  tasks:
    - name: Copy hosts file
      copy: src=/etc/hosts dest=/etc/hosts
      tags:
      - only
    - name: copy test file
      copy: src=/etc/test.txt dest=/opt/test.txt
    - name: touch file
      file: path=/opt/hosts state=touch
      tags:
      - always

執行命令:

ansible-playbook hosts.yml --tags="only"

  • 注:此處我執行標記only,那麼只會執行copy hosts file與touch file,而不會執行沒有標記的test file
  • 如果執行:ansible-playbook hosts.yml,則全部tasks所有會執行。
  • 分別去兩臺被管理服務器上去查看文件建立狀況
相關文章
相關標籤/搜索