Ansible交互式响应非预设远程shell输出

inn6fuwd  于 6个月前  发布在  Shell
关注(0)|答案(1)|浏览(67)

我已经尝试了“expect”和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用户和给予他们的选择基于列表.我知道这是恼人的人为干预,而不是在自动化的精神,但这一步,我们愿意放弃的事情,有一个人实际上看这些文件,并作出决定.
目前“expect”只匹配预设的输出和预设的用户响应。我不想这样做,因为我不知道什么文件将被呈现给用户,所以我不能使用任何预设。
我想要的是显示shell命令的所有输出,并提示ansible用户根据输出做出决定。
发出命令并注册输出的简单任务:

- name: Issue XYZ command
  shell: xyz
  register: xyz_output
- debug: var=xyz_output.stdout


问题是shell命令在这种情况下挂起,因为ansible无法:

  • 显示输出&
  • 提示ansible用户是否继续
waxmsbnn

waxmsbnn1#

一个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'

字符串
我使用了“pause”模块来暂停播放和提示输入,并使用“when”条件来比较输入结果和处理动作。

相关问题