-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaybook.yml
105 lines (94 loc) · 3.45 KB
/
playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
- name: Copy specified SSH keys to all hosts
hosts: all
gather_facts: true
# vars:
# use_yaml: true
# ...
tasks:
- name: Set variables
set_fact:
USE_YAML: "{{ use_yaml | default(false) }}"
USE_GIT: "{{ use_git | default(false) }}"
AUTHORIZED_KEYS_SRC_DIR: "{{ authorized_keys_src_dir | default(use_git|default(false) | ternary(repo_dest|default('/tmp/ansible-authorized-keys'), '.')) }}"
FILE_NAME: "{{ file_name | default(use_yaml|default(false) | ternary('authorized_keys.yml', 'authorized_keys')) }}"
GIT_URL: "{{ git_url | default(omit) }}"
GIT_AUTHENTICATE: "{{ git_authenticate | default(false) }}"
GIT_USERNAME: "{{ git_username | default(omit) }}"
GIT_PASSWORD: "{{ git_password | default(omit) }}"
REPO_DEST: "{{ repo_dest | default('/tmp/ansible-authorized-keys') }}"
- name: Delete repo directory if it already exists
file:
path: "{{ REPO_DEST }}"
state: absent
delegate_to: localhost
run_once: true
when: USE_GIT is true
- name: Create repo directory
file:
path: "{{ REPO_DEST }}"
state: directory
delegate_to: localhost
run_once: true
when: USE_GIT is true
- name: Download authorized key list with authentication
git:
repo: "https://{{ GIT_USERNAME | urlencode }}:{{ GIT_PASSWORD | urlencode }}@{{ GIT_URL }}"
dest: "{{ REPO_DEST }}"
delegate_to: localhost
run_once: true
when: USE_GIT is true and GIT_USERNAME is defined and GIT_PASSWORD is defined
- name: Download authorized key list
git:
repo: "https://{{ GIT_URL }}"
dest: "{{ REPO_DEST }}"
delegate_to: localhost
run_once: true
when: USE_GIT is defined and USE_GIT is true and GIT_USERNAME is not defined
- name: Create temporary file
file:
path: "/tmp/authorized_keys.tmp"
state: touch
mode: 0600
when: USE_YAML is true
- name: Include authorized key list as variable list
include_vars: "{{ AUTHORIZED_KEYS_SRC_DIR }}/{{ FILE_NAME }}"
delegate_to: localhost
run_once: true
when: USE_YAML is true
- name: Copy authorized keys to a temporary file
lineinfile:
path: "/tmp/authorized_keys.tmp"
insertafter: EOF
line: "{{ item }}"
loop: "{{ authorized_keys }}"
no_log: true
when: USE_YAML is true
- name: Copy new authorized_keys file to destination
copy:
src: "{{ AUTHORIZED_KEYS_SRC_DIR }}/{{ FILE_NAME }}"
dest: "{{ ansible_env.HOME + '/' + authorized_keys_dest_dir | default(ansible_env.HOME + '/.ssh/authorized_keys') }}"
mode: 0600
owner: "{{ ansible_env.USER }}"
group: "{{ ansible_env.USER }}"
when: USE_YAML is false
- name: Copy new authorized_keys file to destination
copy:
remote_src: true
src: "/tmp/authorized_keys.tmp"
dest: "{{ ansible_env.HOME + '/' + authorized_keys_dest_dir | default(ansible_env.HOME + '/.ssh/authorized_keys') }}"
mode: 0600
owner: "{{ ansible_env.USER }}"
group: "{{ ansible_env.USER }}"
when: USE_YAML is true
- name: Cleanup
file:
path: "{{ REPO_DEST }}"
state: absent
delegate_to: localhost
run_once: true
when: USE_GIT is true
- name: Cleanup
file:
path: "/tmp/authorized_keys.tmp"
state: absent
when: USE_YAML is true