Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying output of a remote command with Ansible

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" 

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

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

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.

like image 790
Damian Moore Avatar asked Sep 13 '13 15:09

Damian Moore


People also ask

How do you show output in Ansible?

To capture the output, you need to specify your own variable into which the output will be saved. To achieve this, we use the 'register' parameter to record the output to a variable. Then use the 'debug' module to display the variable's content to standard out. To demonstrate this, let's use a few examples.

What is stdout in Ansible?

stdout. Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, and so on). This field contains the normal output of these utilities. "stdout": "foo!"

How do you grep Ansible output?

There is no Ansible grep module, but you can use the grep commands along with shell module or command module. We can store the results of the task and use it in various conditional statements, print them or use them for debugging purposes.


1 Answers

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

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 
like image 95
elimisteve Avatar answered Oct 01 '22 12:10

elimisteve