我已经尝试过“期望”和 shell/命令模块的许多迭代,但在这种情况下都没有提供我(和我想象其他人)想要做的事情。向更广泛的群体伸出援手,希望有一个我还没有找到的解决方案。
我们的软件有一个 shell 命令,可以打印出将要修改的文件列表,并提示用户使用标准的 [y/n] 提示符继续。与升级软件时 YUM 所做的类似,它会输出一堆输出并等待用户输入。
The following changes need to be made:
Create /home/XYZ-file
Enable and start the ABC service
Enable and start the DEF service
Allow? [y/N] n
有时,根据它列出的文件,我们不想继续...有时,我们想继续。所以我希望能够提示我的 ansible 用户并根据列表给他们选择。我知道这是令人讨厌的人为干预,而且不符合自动化的精神,但为了这一步,我们愿意放弃一些事情,让人类真正查看这些文件并做出决定。
目前“期望”仅将预设输出与预设用户响应进行匹配。我不想这样做,因为我不知道将向用户呈现哪些文件,所以我不能使用任何预设。
我想要的是显示 shell 命令的所有输出,并提示 ansible 用户根据输出做出决定。
发出命令并注册输出的简单任务:
- name: Issue XYZ command
shell: xyz
register: xyz_output
- debug: var=xyz_output.stdout
问题是 shell 命令在这种情况下挂起,因为 ansible 无法:
显示输出 &
提示ansible用户是否继续
一个示例 yaml,您可以参考,并根据您的要求进行改进。
---
- hosts: all
gather_facts: False
tasks:
- name: print
shell: cat inventory
register: fileout
- debug: var=fileout.stdout
- name: pause
pause: prompt='Confirm action by giving - yes/no:'
register: pause
- name: Ansible create file.
file:
path: "/home/ansible/vops.txt"
state: touch
mode: 0777
when: pause.user_input == 'yes'
- name: Ansible start service
service:
name: httpd
state: started
when: pause.user_input == 'yes'