1 #####################################
2 ### someone's ansible provisioner ###
3 #####################################
4 # Part of: https://git.somenet.org/root/pub/somesible.git
5 # 2017-2024 by someone <someone@somenet.org>
7 # automatic/periodic self healing.
10 - name: install ansible
17 ignore_errors: "{{ignore_online_errors | bool}}"
20 - name: copy ssh config
22 src: "{{ansible_setup_autoselfheal_ssh_dir_location}}/.ssh/config"
23 dest: "/var/ansible/.ssh/config"
29 - name: copy somesibleupdater private key
31 src: "{{ansible_setup_autoselfheal_ssh_dir_location}}/.ssh/key"
32 dest: "/var/ansible/.ssh/somesible_autoupdater_key"
38 - name: ensure ansible_autoselfheal.log exists
41 dest: "/var/log/ansible_autoselfheal.log"
48 - name: create logrotate entry for ansible_autoselfheal.log
51 dest: "/etc/logrotate.d/ansible_autoselfheal"
56 - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/ansible_autoselfheal.logrotate"
57 - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/ansible_autoselfheal.logrotate"
58 - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/ansible_autoselfheal.logrotate"
59 - "default/ansible_autoselfheal.logrotate"
62 - name: remove "ansible_autoselfheal" workdir to force re-creation.
64 path: "/var/ansible/ansible"
66 when: ansible_setup_reset | bool
69 - name: create "ansible_autoselfheal" workdir and set permissions
71 path: "/var/ansible/ansible"
78 - name: copy run_somesible.sh script from "{{lookup('env','PWD')}}"
80 src: "{{lookup('env','PWD')}}/run_somesible.sh"
81 dest: "/var/ansible/ansible/run_somesible.sh"
87 - name: create group_vars-dir
89 path: "/var/ansible/ansible/group_vars"
97 - name: copy the group vars of groups the host is in
99 src: "{{lookup('env','PWD')}}/group_vars/{{item}}.yml"
100 dest: "/var/ansible/ansible/group_vars/{{item}}.yml"
104 with_items: "{{group_names + ['all']}}"
109 - name: list files in /var/ansible/ansible/group_vars
110 shell: "ls -1 /var/ansible/ansible/group_vars"
116 - name: remove unmanaged files in /var/ansible/ansible/group_vars
118 path: "/var/ansible/ansible/group_vars/{{item}}"
120 with_items: "{{contents.stdout_lines}}"
121 when: item|regex_replace('^(.*)\\.yml$', '\\1') not in (group_names+['all'])
125 - name: create group_files/all-dir
127 path: "/var/ansible/ansible/group_files/all"
135 - name: synchronize group_files/all-dir
137 src: "{{lookup('env','PWD')}}/group_files/all/"
138 dest: "/var/ansible/ansible/group_files/all/"
147 - name: synchronize group_files/{{group_files_group}}-dir
149 src: "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/"
150 dest: "/var/ansible/ansible/group_files/{{group_files_group}}/"
155 when: group_files_group != "all"
159 - name: list dirs in /var/ansible/ansible/group_files
160 shell: "ls -1 /var/ansible/ansible/group_files"
166 - name: remove unmanaged dirs in /var/ansible/ansible/group_files
168 path: "/var/ansible/ansible/group_files/{{item}}"
170 with_items: "{{contents.stdout_lines}}"
171 when: item != "all" and item != group_files_group
175 - name: create ansible host_files-dir
177 path: "/var/ansible/ansible/host_files"
185 - name: synchronize host's host_files-dir
187 src: "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/"
188 dest: "/var/ansible/ansible/host_files/{{inventory_hostname}}/"
197 - name: create ansible host_playbooks-dir
199 path: "/var/ansible/ansible/host_playbooks"
207 - name: copy host's playbook
209 src: "{{lookup('env','PWD')}}/host_playbooks/{{inventory_hostname}}.yml"
210 dest: "/var/ansible/ansible/host_playbooks/{{inventory_hostname}}.yml"
217 - name: create ansible host_vars-dir
219 path: "/var/ansible/ansible/host_vars"
227 - name: synchronize host's host_vars dir
229 src: "{{lookup('env','PWD')}}/host_vars/{{inventory_hostname}}"
230 dest: "/var/ansible/ansible/host_vars/{{inventory_hostname}}"
239 # Make the site consist of only the host's playbook
240 - name: ensure ansible.inventory exists
242 content: "#AUTOGENERATED\n---\n- import_playbook: host_playbooks/{{inventory_hostname}}.yml\n"
243 dest: "/var/ansible/ansible/site.yml"
251 # Make the loopback connection run as "local"
252 - name: ensure ansible.inventory exists
254 content: "#AUTOGENERATED\n\n{% for grp in group_names %}[{{grp}}]\n{{inventory_hostname}} ansible_connection=\"local\" ansible_setup=\"False\" run_is_ansible_autoselfheal=\"True\" group_files_group=\"{{group_files_group}}\"\n\n{% endfor %}"
255 dest: "/var/ansible/ansible/ansible.inventory"
262 - name: fix "ansible_autoselfheal" workdir permissions.
264 path: "/var/ansible/ansible"
273 - name: copy ansible_autoselfheal.service to /etc/systemd/system/
276 dest: "/etc/systemd/system/ansible_autoselfheal.service"
281 - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/ansible_autoselfheal.service"
282 - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/ansible_autoselfheal.service"
283 - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/ansible_autoselfheal.service"
284 - "default/ansible_autoselfheal.service"
287 - name: copy ansible_autoselfheal.timer to /etc/systemd/system/
290 dest: "/etc/systemd/system/ansible_autoselfheal.timer"
295 - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/ansible_autoselfheal.timer"
296 - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/ansible_autoselfheal.timer"
297 - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/ansible_autoselfheal.timer"
298 - "default/ansible_autoselfheal.timer"
301 - name: enable and start ansible_autoselfheal.timer
302 include_role: name="base/systemd/enable-and-start"
304 service_name: ansible_autoselfheal.timer
308 #############################################
309 # update "ansible_autoselfheal" known_hosts #
310 #############################################
311 # TODO: maybe useless
312 - name: copy known_hosts
315 dest: "/var/ansible/.ssh/known_hosts"
320 - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/known_hosts"
321 - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/known_hosts"
322 - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/known_hosts"
323 - "default/known_hosts"
324 when: ansible_setup_autoselfheal_update_known_hosts | bool