ansible-infra/roles/common/tasks/backup_storage_box.yml

109 lines
3.4 KiB
YAML
Raw Normal View History

2020-11-28 20:47:30 +01:00
- name: Install Storage Box mount dependencies
apt:
name: "{{ storage_box_package }}"
state: present
loop: "{{ storage_box_packages }}"
loop_control:
loop_var: storage_box_package
- name: Create SSH directory
file:
path: "{{ ssh_config_dir }}"
state: directory
mode: "700"
2020-11-28 20:47:30 +01:00
- name: Generate SSH key pair for storage box {{ storage_box_host }}
openssh_keypair:
path: "{{ ssh_config_dir }}/{{ storage_box_prefix }}"
type: ed25519
- name: Create SSH config file
file:
path: "{{ ssh_config_dir }}/config"
state: touch
access_time: preserve
modification_time: preserve
mode: "600"
2020-11-28 20:47:30 +01:00
- name: Update SSH config file for storage box {{ storage_box_host }}
blockinfile:
path: "{{ ssh_config_dir }}/config"
block: |
Host {{ storage_box_host }}
{% if storage_box_username is defined %}
User {{ storage_box_username }}
{% endif %}
Port {{ storage_box_port }}
IdentityFile {{ ssh_config_dir }}/{{ storage_box_prefix }}
PreferredAuthentications publickey,password
marker: "# {mark} ANSIBLE MANAGED BLOCK {{ storage_box_host }}"
- name: Copy script to add OpenSSH public key through SFTP
copy:
src: sftp/push_public_key.sh
dest: /usr/local/bin/sftp_push_public_key
owner: root
group: root
mode: "755"
- name: Scan public keys for storage box {{ storage_box_host }}:{{ storage_box_port }}
command: ssh-keyscan -p {{ storage_box_port }} {{ storage_box_host }}
changed_when: no
register: _ssh_known_host
- name: Add backup host {{ storage_box_host }} in known hosts list
known_hosts:
name: |-
{%- if storage_box_port == 22 -%}
{{ storage_box_host }}
{%- else -%}
[{{ storage_box_host }}]:{{ storage_box_port }}
{%- endif -%}
key: "{{ _ssh_known_host.stdout }}"
state: present
- name: Push SSH public key to storage box {{ storage_box_host }}
when: storage_box_password is defined
command: sftp_push_public_key "{{ storage_box_host }}" "{{ ssh_config_dir }}/{{ storage_box_prefix }}.pub"
environment:
SSHPASS: "{{ storage_box_password }}"
changed_when:
- _storage_box_authorized.stdout is defined
- "'Public key added!' in _storage_box_authorized.stdout"
register: _storage_box_authorized
- name: Create backup endpoint {{ storage_box_path }} on {{ storage_box_host }}
shell: |
sftp {{ storage_box_host }} <<-EOF
mkdir "{{ storage_box_path }}"
EOF
changed_when: "'Couldn\\'t create directory' not in _backup_endpoint_created.stderr"
register: _backup_endpoint_created
- name: Create AutoFS config file for storage box {{ storage_box_host }} (SSHFS)
lineinfile:
path: /etc/auto.backup.{{ storage_box_prefix }}
regex: "^{{ storage_box_mount.path }} "
line: |
{{ storage_box_mount.path }} -fstype=fuse,{{ storage_box_mount.options | join(',') }} :sshfs\#{{ storage_box_host }}\:{{ storage_box_path }}
2020-11-28 20:47:30 +01:00
state: present
create: yes
notify: reload autofs
- name: Add AutoFS config file into main AutoFS config
lineinfile:
path: /etc/auto.master
regexp: '^/- /etc/auto.backup'
line: /- /etc/auto.backup.{{ storage_box_prefix }} --timeout=90,--ghost
state: present
notify: reload autofs
- name: Start AutoFS service
service:
name: autofs
state: started
enabled: yes
register: autofs_started
- name: Trigger AutoFS handlers
meta: flush_handlers