install ssh public key if user exist with ansible

cat > ssh_pub_key_install.yml <<EOF
---
# install ssh public key if user exist
# ansible-playbook ssh_pub_key_install.yml -e target=srv7

- name: 'set host'
  hosts: "{{ target }}"
  gather_facts: no

  tasks:

  - name: 'execute a command'
    command: bash -c "id lancelot"
    changed_when: false
    register: cmd_output

  - name: 'create file'
    lineinfile:
      path: "$HOME/.ssh/authorized_keys"
      line: "ssh-rsa AAB3NzaC1yc2EAADAQABBAQCeeCZw"
      state: present
      create: yes
    when: cmd_output is succeeded
EOF

Leave a comment