75

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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
Damian Moore
  • 1,306
  • 1
  • 11
  • 13

3 Answers3

85

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
kenorb
  • 155,785
  • 88
  • 678
  • 743
elimisteve
  • 1,771
  • 1
  • 18
  • 22
  • 3
    For some reason I still get no output, I even used `-vvvv` on ansible and I get no output at all. Tries with `command: ls` – sorin Feb 07 '15 at 14:12
  • 1
    @sorin I was also seeing no output, the issue in my case was that my `hosts` parameter in the playbook was set incorrectly. Such a configuration is hard to debug, since Ansible doesn't provide an indication of hosts that are attempted. Perhaps that was your issue? – blong Mar 29 '17 at 15:20
  • 1
    great solution. thanks! Why should we add "var=" ? the intuitive way for me would be {{vagrant}}.function() . is there a reason for that? – ALUFTW Dec 26 '18 at 13:38
30

If you pass the -v flag to the ansible-playbook command, then ansible will show the output on your terminal.

For your use case, you may want to try using the fetch module to copy the public key from the server to your local machine. That way, it will only show a "changed" status when the file changes.

Lorin Hochstein
  • 57,372
  • 31
  • 105
  • 141
  • 2
    I don't think I'd want to run the entire playbook in verbose mode to get the output of this. Fetch could be useful in avoiding the "changed" status if I could get the playbook to pause and display the content of the local file. I need to tell the operator to add the public SSH key to GitHub's account authorisation. I guess I could just provide more instructions and tell them to open the fetched file. – Damian Moore Sep 15 '13 at 18:56
  • 2
    @DamianMoore You can use the "pause" module to get the playbook to pause, and you could use the "debug" module with the "lookup" plugin to print the file to screen. – Lorin Hochstein Sep 16 '13 at 01:06
8

Prints pubkey and avoid the changed status by adding changed_when: False to cat task:

- name: Generate SSH keys for vagrant user   
  user: name=vagrant generate_ssh_key=yes ssh_key_bits=2048

- name: Check SSH public key   
  command: /bin/cat $home_directory/.ssh/id_rsa.pub
  register: cat
  changed_when: False

- name: Print SSH public key
  debug: var=cat.stdout

- name: Wait for user to copy SSH public key   
  pause: prompt="Please add the SSH public key above to your GitHub account"
kenorb
  • 155,785
  • 88
  • 678
  • 743
luissquall
  • 1,740
  • 19
  • 14