Add playbook to stop and backup services
This commit is contained in:
parent
36c12a330f
commit
82c25edf8e
4 changed files with 62 additions and 15 deletions
|
@ -3,6 +3,7 @@ class FilterModule(object):
|
||||||
return {
|
return {
|
||||||
'my_service_attributes': self.my_service_attributes,
|
'my_service_attributes': self.my_service_attributes,
|
||||||
'services_to_dnscontrol': self.services_to_dnscontrol,
|
'services_to_dnscontrol': self.services_to_dnscontrol,
|
||||||
|
'services_get_backups': self.services_get_backups,
|
||||||
}
|
}
|
||||||
|
|
||||||
def my_service_attributes(self, services, host, attribute="name"):
|
def my_service_attributes(self, services, host, attribute="name"):
|
||||||
|
@ -23,6 +24,15 @@ class FilterModule(object):
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def services_get_backups(self, all_services, wanted_services):
|
||||||
|
result = []
|
||||||
|
for service in all_services:
|
||||||
|
if service.get("name") in wanted_services:
|
||||||
|
for backup in service.get("backup") or []:
|
||||||
|
result.append(backup["name"])
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def services_to_dnscontrol(self, services):
|
def services_to_dnscontrol(self, services):
|
||||||
result = {}
|
result = {}
|
||||||
for service in services:
|
for service in services:
|
||||||
|
|
32
playbooks/stop-and-backup-unused.yml
Normal file
32
playbooks/stop-and-backup-unused.yml
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
---
|
||||||
|
- name: Stop and backup services
|
||||||
|
hosts: serguzim_net
|
||||||
|
vars:
|
||||||
|
host_services: "{{ all_services | my_service_attributes(inventory_hostname) | union(common_services) }}"
|
||||||
|
tasks:
|
||||||
|
- name: Get unused services
|
||||||
|
ansible.builtin.include_tasks:
|
||||||
|
file: tasks/get-unused.yml
|
||||||
|
|
||||||
|
- name: Set unused services backups
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
unused_services_backups: "{{ all_services | services_get_backups(unused_services) }}"
|
||||||
|
- name: Debug unused services backups
|
||||||
|
ansible.builtin.debug:
|
||||||
|
msg: "{{ unused_services_backups }}"
|
||||||
|
|
||||||
|
- name: Stop unused services
|
||||||
|
ansible.builtin.include_tasks:
|
||||||
|
file: tasks/stop-unused.yml
|
||||||
|
|
||||||
|
- name: Backup unused services
|
||||||
|
ansible.builtin.command:
|
||||||
|
cmd: sudo autorestic -c .autorestic.all.yml backup -l {{ item }}
|
||||||
|
chdir: "{{ (services_path, 'backup') | path_join }}"
|
||||||
|
loop: "{{ unused_services_backups }}"
|
||||||
|
changed_when: true
|
||||||
|
become: true
|
||||||
|
register: cmd_result_backup
|
||||||
|
until: "cmd_result_backup is not failed"
|
||||||
|
retries: 10
|
||||||
|
delay: 10
|
17
playbooks/tasks/get-unused.yml
Normal file
17
playbooks/tasks/get-unused.yml
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
- name: Get running compose projects
|
||||||
|
ansible.builtin.shell:
|
||||||
|
executable: /usr/bin/bash
|
||||||
|
cmd: |
|
||||||
|
set -o pipefail
|
||||||
|
docker container ls --format json \
|
||||||
|
| jq .Labels \
|
||||||
|
| grep -oe 'com.docker.compose.project=[a-zA-Z0-9_-]*' \
|
||||||
|
| sed -e 's/com.docker.compose.project=//' \
|
||||||
|
| sort \
|
||||||
|
| uniq
|
||||||
|
changed_when: false
|
||||||
|
register: docker_compose_projects_result
|
||||||
|
|
||||||
|
- name: Set unused services
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
unused_services: "{{ docker_compose_projects_result.stdout_lines | difference(host_services) }}"
|
|
@ -1,19 +1,7 @@
|
||||||
- name: Get running compose projects
|
|
||||||
ansible.builtin.shell:
|
|
||||||
cmd: |
|
|
||||||
set -o pipefail
|
|
||||||
docker container ls --format json \
|
|
||||||
| jq .Labels \
|
|
||||||
| grep -oe 'com.docker.compose.project=[a-zA-Z0-9_-]*' \
|
|
||||||
| sed -e 's/com.docker.compose.project=//' \
|
|
||||||
| sort \
|
|
||||||
| uniq
|
|
||||||
changed_when: false
|
|
||||||
register: docker_compose_projects_result
|
|
||||||
|
|
||||||
- name: Get unused services
|
- name: Get unused services
|
||||||
ansible.builtin.set_fact:
|
ansible.builtin.include_tasks:
|
||||||
unused_services: "{{ docker_compose_projects_result.stdout_lines | difference(host_services) }}"
|
file: tasks/get-unused.yml
|
||||||
|
when: "unused_services is undefined"
|
||||||
|
|
||||||
- name: Stop unused services
|
- name: Stop unused services
|
||||||
ansible.builtin.command:
|
ansible.builtin.command:
|
||||||
|
|
Loading…
Reference in a new issue