ansible-playbook ssh-provision.yml -i hosts -e "target=testing" --limit "testing" --user=$USER --ask-pass -vv
Created
March 9, 2020 16:27
-
-
Save emilorol/9747bd94e2490b65007efc3b0d6151ae to your computer and use it in GitHub Desktop.
Playbook provision your SSH key on remote servers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[defaults] | |
timeout=30 | |
inventory = inventory | |
host_key_checking = False | |
remote_tmp = /tmp/ansible |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[localhost] | |
127.0.0.1 | |
[testing] | |
127.0.0.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: "Provision SSH key on remote servers" | |
hosts: "{{ target }}" | |
become: yes | |
tasks: | |
- name: Check if user directory exists. | |
stat: | |
path: /home/{{ lookup('env','USER') }} | |
register: remote_home_directory | |
- name: Make user directory if missing | |
file: | |
path: /home/{{ lookup('env','USER') }}/.ssh | |
state: directory | |
when: remote_home_directory.stat.exists == false | |
- name: Check if ssh directory exists. | |
stat: | |
path: /home/{{ lookup('env','USER') }}/.ssh | |
register: remote_ssh_directory | |
- name: Recursively change ownership of a /home/USER | |
file: | |
path: /home/{{ lookup('env','USER') }} | |
state: directory | |
recurse: yes | |
owner: "{{ lookup('env','USER') }}" | |
group: "{{ lookup('env','USER') }}" | |
- name: Create empty authorized_keys file | |
file: | |
path: /home/{{ lookup('env','USER') }}/.ssh/authorized_keys | |
state: touch | |
when: remote_ssh_directory.stat.exists == false | |
- name: Add key to host | |
authorized_key: | |
user: "{{ lookup('env','USER') }}" | |
state: present | |
key: "{{ lookup('file', lookup('env','HOME') + '/.ssh/id_rsa.pub') }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment