本文介绍了使用Ansible显示远程命令的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ansible角色中,我生成用户的SSH密钥.之后,我想将其打印到屏幕上并暂停,以便用户可以将其复制并粘贴到其他位置.到目前为止,我有这样的事情:

In an Ansible role I generate the user's SSH key. After that I want to print it to the screen and pause so the user can copy and paste it somewhere else. So far I have something like this:

- name: Generate SSH keys for vagrant user
  user: name=vagrant generate_ssh_key=yes ssh_key_bits=2048
- name: Show SSH public key
  command: /bin/cat $home_directory/.ssh/id_rsa.pub
- name: Wait for user to copy SSH public key
  pause: prompt="Please add the SSH public key above to your GitHub account"

显示SSH公钥"任务已完成,但未显示输出.

The 'Show SSH public key' task completes but doesn't show the output.

TASK: [Show SSH public key] *************************************************** 
changed: [default]

可能会有更好的方法来解决此问题.我真的不喜欢它会始终显示已更改"状态的事实.我确实找到了对ansible的请求请求- https://github.com/ansible/ansible/pull/2673 -但不确定是否可以在不编写自己的模块的情况下使用它.

There may be a better way of going about this. I don't really like the fact that it will always show a 'changed' status. I did find this pull request for ansible - https://github.com/ansible/ansible/pull/2673 - but not sure if I can use it without writing my own module.

推荐答案

我不确定您的特定命令(例如流浪汉等)的语法,但总的来说...

I'm not sure about the syntax of your specific commands (e.g., vagrant, etc), but in general...

只需将Ansible(未正常显示)的JSON输出注册到变量中,然后显示每个变量的stdout_lines属性:

Just register Ansible's (not-normally-shown) JSON output to a variable, then display each variable's stdout_lines attribute:

- name: Generate SSH keys for vagrant user
  user: name=vagrant generate_ssh_key=yes ssh_key_bits=2048
  register: vagrant
- debug: var=vagrant.stdout_lines

- name: Show SSH public key
  command: /bin/cat $home_directory/.ssh/id_rsa.pub
  register: cat
- debug: var=cat.stdout_lines

- name: Wait for user to copy SSH public key
  pause: prompt="Please add the SSH public key above to your GitHub account"
  register: pause
- debug: var=pause.stdout_lines

这篇关于使用Ansible显示远程命令的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 11:15