天天看點

ansible-playbook教程(三)

# --check | -c:隻檢測可能會發生的改變,但不真正執行操作

# --list-hosts:列出運作任務的主機

# --list-tags:列出playbook檔案中定義的所有tags

# --list-tasks:列出playbook檔案中定義的所有任務

# --limit:主機清單 隻針對主機清單中的某個主機或者某個組執行

playbook核心元素

•    hosts 執行的遠端主機清單

•    tasks 任務集

•    varniables 内置變量或自定義變量在playbook中調用

•    templates 模闆,即使用模闆文法的檔案,比如配置檔案等

•    handlers 和notity結合使用,由特定條件觸發的操作,滿足條件方才執行,否則不執行

•    tags 标簽,指定某條任務執行,用于選擇運作playbook中的部分代碼。

準備工作 

主機清單安裝httpd軟體

案例

---

- hosts: all

  remote_user: root

  vars:

   - p1: t1133

   - p2: t1132

   - p3: t1131

  tasks:

  - name: copy file

    copy:

     src: /root/test/3.txt

     dest: /root/

  - name: copy http html

     src: /var/www/html/index.html

     dest: /var/www/html/index.html

    when: ansible_hostname=="localhost"

  - name: touch file2

    file: name=/root/{{ p2 }} state=touch  mode=755

    when: ansible_hostname=="docker2"

  - name: touch file3

    file: name=/root/{{ p3 }} state=touch  mode=755

    when: ansible_hostname=="docker3"

  - name: touch file1

    file: name=/root/{{ p1 }} state=touch  mode=755

  - name: installed lsof  tree

    yum: name={{ item.name }} state=installed

    with_items:

     - { name: 'tree' }

     - { name: 'lsof' }

     - { name: 'httpd '}

  - name: config httpd

    template:

      src: /root/anslibetest/templates/httpd.conf.jinja2

      dest: /etc/httpd/conf/httpd.conf

    tags:

        - conf

    notify:

         - restart httpd

  handlers:

  - name: restart httpd

    service:

       name: httpd

       state: restarted

特殊顔色都是要學習的知識點 可百度案例 

yml檔案内容

繼續閱讀