Ansible
在ansible中跳過一些提示的任務
如何訪問以在另一台伺服器上回答一個提示?我的主人:
[dbs] db1 db2 db3
我有一個角色,我的角色
tasks/main.yml
是:- pause: prompt: "Do you want to install mysql (yes/no)?" register: my_pause - include_tasks: mysql.yml when: my_pause.user_input | bool
當我執行這個角色時,只有第一台伺服器跳過
MySQL.yml
並到另一台伺服器執行MySQL.yml
。我想如果使用者輸入 no 作為回答提示,
MySQL.yml
不會對任何伺服器執行。但是當我輸入 no 時會再次安裝!![root@anisble ansible]# ansible-playbook playbooks/test.yml PLAY [dbs] ************************************************************** TASK [Gathering Facts] ******************* ok: [db1] ok: [db3] ok: [db2] TASK [ssh : pause] ******************************************************************************** [ssh : pause] Do you want to install mysql (yes/no)?: no ok: [db1] TASK [ssh : include_tasks] ******************************************************************************** skipping: [db1] included: /etc/ansible/roles/ssh/tasks/mysql.yml for db2, db3 TASK [ssh : install mysql] ******************************************************************************** ok: [db3] ok: [db2] PLAY RECAP ******************************************************************************** db1 : ok=2 changed=0 unreachable=0 failed=0 db2 : ok=3 changed=0 unreachable=0 failed=0 db3
編輯-1
我使用第一個您的解決方案,但是當我輸入 no 時,它將再次安裝!
task/main.yml
:--- - pause: prompt: "Do you want to install mysql (yes/no)?" register: install_mysql delegate_to: localhost - include_tasks: mysql.yml when: hostvars['localhost']['install_mysql']['user_input'] == 'yes'
輸出是:
[root@anisble ansible]# ansible-playbook playbooks/test.yml PLAY [dbs] ******************************************************************************** TASK [Gathering Facts] ******************************************************************************** ok: [db2] ok: [db1] ok: [db3] TASK [ssh : pause] ******************************************************************************** [ssh : pause] Do you want to install mysql (yes/no)?: no ok: [db1 -> localhost] TASK [ssh : include_tasks] ******************************************************************************** included: /etc/ansible/roles/ssh/tasks/mysql.yml for db1, db2, db3 TASK [ssh : install mysql] ******************************************************************************** changed: [db1] changed: [db3] changed: [db2] PLAY RECAP ******************************************************************************** db1 : ok=4 changed=1 unreachable=0 failed=0 db2 : ok=3 changed=1 unreachable=0 failed=0 db3 : ok=3 changed=1 unreachable=0 failed=0
編輯-2
這種方法有效。
- name: confirm pause: prompt: "Do you want to install mysql (yes/no)?" register: install_mysql - include_tasks: mysql.yml when: hostvars[groups['dbs'][0]]['install_mysql']['user_input'] == "yes"
在本地主機上執行暫停
您可以
pause
在特定主機上執行任務,例如localhost
並從 hostvars 數組hostvars['localhost']
(從此處竊取)獲取答案。- name: Pause prompt on localhost hosts: localhost tasks: - name: Prompt pause: prompt: "Do you want to install mysql (yes/no)?" register: install_mysql - name: Get the answer from the hostvars array hosts: all tasks: - name: Include role when you answered 'yes' include_tasks: mysql.yml when: hostvars['localhost']['install_mysql']['user_input'] == 'yes'
您必須在該主機上使用專用播放才能訪問 hostvars 數組中的答案。如果您在主機組上執行一次(例如在您的原始請求中),您必須猜測哪個主機將首先被處理並使用
hostvars['firstprocessedhost']
(有時hostvars['db1']
)。當您使用時
delegate_to: localhost
,任務仍在執行,db1
並且答案儲存在hostvars['db1']
.您不應該依賴主機的順序,並且暫停模組旨在暫停劇本而不提示使用者數據。
vars_prompt
如果可能的話,你應該使用
vars_prompt
你的遊戲部分:- name: A play with a vars_prompt section hosts: all vars_prompt: - name: install_mysql prompt: "Do yo want to install mysql (yes/no)?" tasks: - name: Include role when you answered 'yes' include_tasks: mysql.yml when: install_mysql == 'yes'