2016-04-04 37 views
1

的Ansible最佳實踐文檔有示例代碼:如何將標籤應用於Ansible任務文件中的每個命令?

--- 
# file: roles/common/tasks/main.yml 

- name: be sure ntp is installed 
    yum: name=ntp state=installed 
    tags: ntp 

- name: be sure ntp is configured 
    template: src=ntp.conf.j2 dest=/etc/ntp.conf 
    notify: 
    - restart ntpd 
    tags: ntp 

- name: be sure ntpd is running and enabled 
    service: name=ntpd state=running enabled=yes 
    tags: ntp 

我期待避免重複tags: ntp線。每個指令是否可以繼承標籤?

回答

4

你可以一起工作 - 塊:

➜ ~ cat become.yml 
--- 
- hosts: localhost 
    user: vagrant 
    tasks: 
    - block: 
     - shell: whoami 
     register: result 

     - debug: var=result.stdout 

     - name: become_root_user 
     become: true 
     become_user: root 
     shell: whoami 
     register: sudo_test_result 

     - debug: var=sudo_test_result.stdout 
    tags: 
     - block1 
    - block: 
     - name: creating_new_app_user 
     become: true 
     become_user: root 
     become_method: sudo 
     user: name=app_user password=Bzs310di86b6E groups="adm,sudo" system=yes state=present 

     - name: become_app_user 
     become: true 
     become_user: app_user 
     become_method: sudo 
     shell: whoami 
     register: app_user_test_result 

     - debug: var=app_user_test_result.stdout 
    tags: 
     - block2 

〜ansible-劇本-i實時自動化/主機奴隸become.yml --tags 「塊1」

在您的具體情況:

--- 
- block: 
    - name: be sure ntp is installed 
     yum: name=ntp state=installed 

    - name: be sure ntp is configured 
     template: src=ntp.conf.j2 dest=/etc/ntp.conf 
     notify: 
     - restart ntpd 

    - name: be sure ntpd is running and enabled 
     service: name=ntpd state=running enabled=yes 
    tags: ntp 
+0

真棒!看起來這個功能需要Ansible v2 btw。 – Ryre

+0

是的,你需要安全的2.0來使用塊。 –

5

V2在此之前可以實現分配標籤的「包括」

移動這個任務到不同的文件,說ntp.yml

--- 
# file: roles/common/tasks/ntp.yml 

- name: be sure ntp is installed 
    yum: name=ntp state=installed 

- name: be sure ntp is configured 
    template: src=ntp.conf.j2 dest=/etc/ntp.conf 
    notify: 
    - restart ntpd 

- name: be sure ntpd is running and enabled 
    service: name=ntpd state=running enabled=yes 

然後將其包含在main.yml

--- 
# file: roles/common/tasks/main.yml 
- include: ntp.yml 
    tags: ntp 
+0

我發現你仍然在ntp.yml文件中包含'tags:ntp',但是如果我正確理解你的答案,那麼這些標籤可以被刪除? – Ryre

+0

是的,對不起,您不需要ntp.yml中的'tags:ntp' @Ryre – shaps

相關問題