Displaying output of a remote command with Ansible

后端 未结 3 965
[愿得一人]
[愿得一人] 2020-12-24 00:23

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 some

相关标签:
3条回答
  • 2020-12-24 00:40

    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
    
    0 讨论(0)
  • 2020-12-24 00:42

    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"
    
    0 讨论(0)
  • 2020-12-24 00:47

    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.

    0 讨论(0)
提交回复
热议问题