feat: use ansible block
This commit is contained in:
parent
7d762d095d
commit
b5c819fb27
12 changed files with 399 additions and 424 deletions
149
tasks/swap.yaml
149
tasks/swap.yaml
|
|
@ -1,94 +1,81 @@
|
|||
---
|
||||
|
||||
# enable or disable swap
|
||||
- name: "swap - set variable"
|
||||
ansible.builtin.set_fact:
|
||||
__swap: "{{ swap | default('true') }}"
|
||||
tags:
|
||||
- "swap"
|
||||
# manage swap
|
||||
- name: "manage swap"
|
||||
block:
|
||||
|
||||
# verify swapfile
|
||||
- name: "swap - verify swapfile"
|
||||
ansible.builtin.stat:
|
||||
path: "{{ swap_file_location | default('/swapfile') }}"
|
||||
register: "swap_file_check"
|
||||
tags:
|
||||
- "swap"
|
||||
# enable or disable swap
|
||||
- name: "swap - set variable"
|
||||
ansible.builtin.set_fact:
|
||||
__swap: "{{ swap | default('true') }}"
|
||||
|
||||
## create swap
|
||||
# create swap file
|
||||
- name: "swap - create swap file"
|
||||
ansible.builtin.command: "fallocate -l {{ swap_file_size }} {{ swap_file_location }}"
|
||||
when: "not swap_file_check.stat.exists and __swap"
|
||||
tags:
|
||||
- "swap"
|
||||
# verify swapfile
|
||||
- name: "swap - verify swapfile"
|
||||
ansible.builtin.stat:
|
||||
path: "{{ swap_file_location | default('/swapfile') }}"
|
||||
register: "swap_file_check"
|
||||
|
||||
# set swap file permissions
|
||||
- name: "swap - set permissions "
|
||||
ansible.builtin.file:
|
||||
path: "{{ swap_file_location }}"
|
||||
owner: "root"
|
||||
group: "root"
|
||||
mode: "0600"
|
||||
when: "__swap"
|
||||
tags:
|
||||
- "swap"
|
||||
## create swap
|
||||
# create swap file
|
||||
- name: "swap - create swap file"
|
||||
ansible.builtin.command: "fallocate -l {{ swap_file_size }} {{ swap_file_location }}"
|
||||
when: "not swap_file_check.stat.exists and __swap"
|
||||
|
||||
# 'format' swapfile
|
||||
- name: "swap - format swap file"
|
||||
ansible.builtin.command: "mkswap {{ swap_file_location }}"
|
||||
when: "not swap_file_check.stat.exists and __swap"
|
||||
tags:
|
||||
- "swap"
|
||||
# set swap file permissions
|
||||
- name: "swap - set permissions "
|
||||
ansible.builtin.file:
|
||||
path: "{{ swap_file_location }}"
|
||||
owner: "root"
|
||||
group: "root"
|
||||
mode: "0600"
|
||||
when: "__swap"
|
||||
|
||||
# configure fstab
|
||||
- name: "swap - configure fstab"
|
||||
ansible.posix.mount:
|
||||
name: "swapfile"
|
||||
src: "{{ swap_file_location | default('/swapfile') }}"
|
||||
fstype: "swap"
|
||||
opts: "sw"
|
||||
passno: "0"
|
||||
dump: "0"
|
||||
state: "present"
|
||||
when: "__swap"
|
||||
tags:
|
||||
- "swap"
|
||||
# 'format' swapfile
|
||||
- name: "swap - format swap file"
|
||||
ansible.builtin.command: "mkswap {{ swap_file_location }}"
|
||||
when: "not swap_file_check.stat.exists and __swap"
|
||||
|
||||
# enable swap
|
||||
- name: "swap - enable swap"
|
||||
ansible.builtin.command: "swapon -a"
|
||||
when: "not swap_file_check.stat.exists and __swap"
|
||||
tags:
|
||||
- "swap"
|
||||
# configure fstab
|
||||
- name: "swap - configure fstab"
|
||||
ansible.posix.mount:
|
||||
name: "swapfile"
|
||||
src: "{{ swap_file_location | default('/swapfile') }}"
|
||||
fstype: "swap"
|
||||
opts: "sw"
|
||||
passno: "0"
|
||||
dump: "0"
|
||||
state: "present"
|
||||
when: "__swap"
|
||||
|
||||
## delete swap
|
||||
# disable swap
|
||||
- name: "swap - disable swap"
|
||||
ansible.builtin.command: "swapoff -a"
|
||||
when: "swap_file_check.stat.exists and not __swap"
|
||||
tags:
|
||||
- "swap"
|
||||
# enable swap
|
||||
- name: "swap - enable swap"
|
||||
ansible.builtin.command: "swapon -a"
|
||||
when: "not swap_file_check.stat.exists and __swap"
|
||||
|
||||
# delete swap file
|
||||
- name: "swap - delete swap file"
|
||||
ansible.builtin.file:
|
||||
path: "{{ swap_file_location }}"
|
||||
state: "absent"
|
||||
when: "swap_file_check.stat.exists and not __swap"
|
||||
tags:
|
||||
- "swap"
|
||||
## delete swap
|
||||
# disable swap
|
||||
- name: "swap - disable swap"
|
||||
ansible.builtin.command: "swapoff -a"
|
||||
when: "swap_file_check.stat.exists and not __swap"
|
||||
|
||||
# delete swap file
|
||||
- name: "swap - delete swap file"
|
||||
ansible.builtin.file:
|
||||
path: "{{ swap_file_location }}"
|
||||
state: "absent"
|
||||
when: "swap_file_check.stat.exists and not __swap"
|
||||
|
||||
# configure fstab
|
||||
- name: "swap - configure fstab"
|
||||
ansible.posix.mount:
|
||||
name: "swapfile"
|
||||
src: "{{ swap_file_location | default('/swapfile') }}"
|
||||
fstype: "swap"
|
||||
opts: "sw"
|
||||
passno: "0"
|
||||
dump: "0"
|
||||
state: "absent"
|
||||
when: "not __swap"
|
||||
|
||||
# configure fstab
|
||||
- name: "swap - configure fstab"
|
||||
ansible.posix.mount:
|
||||
name: "swapfile"
|
||||
src: "{{ swap_file_location | default('/swapfile') }}"
|
||||
fstype: "swap"
|
||||
opts: "sw"
|
||||
passno: "0"
|
||||
dump: "0"
|
||||
state: "absent"
|
||||
when: "not __swap"
|
||||
tags:
|
||||
- "swap"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue