--- # https://justinmontgomery.com/speed-up-stat-command-in-ansible - name: "Enable | create the swap file" block: - name: "Enable | stat the swap file" register: swapfile_this failed_when: not swapfile_this.stat.exists ansible.builtin.stat: path: "{{ swapfile_path }}" get_checksum: false get_mime: false get_attributes: false rescue: - name: "Enable | ensure swap file exists" register: swapfile_create ansible.builtin.command: cmd: "{{ swapfile_create_command }}" creates: "{{ swapfile_path }}" - name: "Enable | set permissions on swap file" ansible.builtin.file: path: "{{ swapfile_path }}" owner: root group: root mode: "0600" - name: "Enable | make swap" # noqa: no-handler no-changed-when register: swapfile_mkswap_result when: swapfile_create is changed ansible.builtin.command: cmd: mkswap {{ swapfile_path }} - name: "Enable | run swapon on the swap file => '{{ swapfile_path }}'" when: - swapfile_mkswap_result is changed - not swapfile_test_mode changed_when: false ansible.builtin.command: cmd: swapon {{ swapfile_path }} - name: "Set swappiness to '{{ swapfile_swappiness }}'" ansible.posix.sysctl: name: vm.swappiness value: "{{ swapfile_swappiness }}" state: present