![在本地主机上运行暂停](https://linux22.com/image/127727/%E5%9C%A8%E6%9C%AC%E5%9C%B0%E4%B8%BB%E6%9C%BA%E4%B8%8A%E8%BF%90%E8%A1%8C%E6%9A%82%E5%81%9C.png)
如何访问并回答另一台服务器上的提示?我的主人:
[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"
答案1
在本地主机上运行暂停
您可以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['firstprocessedhost']
(有时hostvars['db1']
)。
当您使用时,delegate_to: localhost
任务仍然运行db1
并且答案存储在hostvars['db1']
.
您不应该依赖于主机的顺序,并且暂停模块旨在暂停剧本而不是提示用户数据。
变量提示符
如果可能的话,您应该使用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'