2015-09-26 73 views

回答

7

當涉及到這種事情時,Ansible文檔存在限制 - 如果有官方最佳實踐,我還沒有遇到過。

讓您的劇本保持可讀性和可讀性的一個好方法是針對主機運行多個不同的播放,並在每個播放器中使用不同的參數調用角色。

role: foo, var: blah語法顯示了一點點this description的語法是一種傳遞參數的好方法,並且一目瞭然地讓事情一目瞭然。例如:

- name: Run the docker role with docker_container_state=foo 
    hosts: docker-host 
    roles: 
    - { role: docker_container, docker_container_state: foo } 

- name: Run the docker role with docker_container_state=bar 
    hosts: docker-host 
    roles: 
    - { role: docker_container, docker_container_state: bar } 
5

我通常使用includes運行的作用,部分多次,如果我有一個變量的體面的佈局(或整個角色!)。請參閱下面的示例劇本,角色apply_state其中roles/apply_state/tasks文件夾中有print_state.yml。訣竅是通過包含內容的項目,之後它是一塊蛋糕。

playbook.yml

- hosts: localhost 
    roles: 
    - { role: apply_state, states: [ state_one, state_two, state_three ] } 

角色/ apply_state /任務/ main.yml

- name: print all states! 
    include: print_state.yml state="{{ item }}" 
    with_items: "{{ states }}" 

角色/ apply_state /任務/ print_state.yml

- name: echo state 
    debug: msg="{{ state }}" 

請參閱下面的ansible-playbook -i localhost, playbook.yml的輸出:

PLAY [localhost] *************************************************************** 

TASK [setup] ******************************************************************* 
ok: [localhost] 

TASK [apply_state : print all states!] ***************************************** 
included: /home/user/roles/apply_state/tasks/print_state.yml for localhost 
included: /home/user/roles/apply_state/tasks/print_state.yml for localhost 
included: /home/user/roles/apply_state/tasks/print_state.yml for localhost 

TASK [apply_state : echo state] ************************************************ 
ok: [localhost] => { 
    "msg": "state_one"                             
}                                  

TASK [apply_state : echo state] ************************************************ 
ok: [localhost] => { 
    "msg": "state_two"                             
}                                  

TASK [apply_state : echo state] ************************************************ 
ok: [localhost] => { 
    "msg": "state_three"                            
}                                  

PLAY RECAP ********************************************************************* 
localhost     : ok=7 changed=0 unreachable=0 failed=0 
相關問題