1 #####################################
2 ### someone's ansible provisioner ###
3 #####################################
4 # Part of: https://git.somenet.org/root/pub/somesible.git
5 # 2017-2025 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"
160 - name: list dirs in /var/ansible/ansible/group_files
161 shell: "ls -1 /var/ansible/ansible/group_files"
167 - name: remove unmanaged dirs in /var/ansible/ansible/group_files
169 path: "/var/ansible/ansible/group_files/{{item}}"
171 with_items: "{{contents.stdout_lines}}"
172 when: item != "all" and item != group_files_group
176 - name: create ansible host_files-dir
178 path: "/var/ansible/ansible/host_files"
186 - name: synchronize host's host_files-dir
188 src: "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/"
189 dest: "/var/ansible/ansible/host_files/{{inventory_hostname}}/"
198 - name: create ansible host_playbooks-dir
200 path: "/var/ansible/ansible/host_playbooks"
208 - name: copy host's playbook
210 src: "{{lookup('env','PWD')}}/host_playbooks/{{inventory_hostname}}.yml"
211 dest: "/var/ansible/ansible/host_playbooks/{{inventory_hostname}}.yml"
218 - name: create ansible host_vars-dir
220 path: "/var/ansible/ansible/host_vars"
228 - name: synchronize host's host_vars dir
230 src: "{{lookup('env','PWD')}}/host_vars/{{inventory_hostname}}"
231 dest: "/var/ansible/ansible/host_vars/{{inventory_hostname}}"
240 # Make the site consist of only the host's playbook
241 - name: ensure ansible.inventory exists
243 content: "#AUTOGENERATED\n---\n- import_playbook: host_playbooks/{{inventory_hostname}}.yml\n"
244 dest: "/var/ansible/ansible/site.yml"
252 # Make the loopback connection run as "local"
253 - name: ensure ansible.inventory exists
255 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 %}"
256 dest: "/var/ansible/ansible/ansible.inventory"
263 - name: fix "ansible_autoselfheal" workdir permissions.
265 path: "/var/ansible/ansible"
274 - name: copy ansible_autoselfheal.service to /etc/systemd/system/
277 dest: "/etc/systemd/system/ansible_autoselfheal.service"
282 - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/ansible_autoselfheal.service"
283 - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/ansible_autoselfheal.service"
284 - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/ansible_autoselfheal.service"
285 - "default/ansible_autoselfheal.service"
288 - name: copy ansible_autoselfheal.timer to /etc/systemd/system/
291 dest: "/etc/systemd/system/ansible_autoselfheal.timer"
296 - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/ansible_autoselfheal.timer"
297 - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/ansible_autoselfheal.timer"
298 - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/ansible_autoselfheal.timer"
299 - "default/ansible_autoselfheal.timer"
302 - name: enable and start ansible_autoselfheal.timer
303 include_role: name="base/systemd/enable-and-start"
305 service_name: ansible_autoselfheal.timer
309 #############################################
310 # update "ansible_autoselfheal" known_hosts #
311 #############################################
312 # TODO: maybe useless
313 - name: copy known_hosts
316 dest: "/var/ansible/.ssh/known_hosts"
321 - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/known_hosts"
322 - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/known_hosts"
323 - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/known_hosts"
324 - "default/known_hosts"
325 when: ansible_setup_autoselfheal_update_known_hosts | bool