|
| 1 | +- hosts: deploy_node |
| 2 | + become: true |
| 3 | + tasks: |
| 4 | + - name: Validate required variables are defined |
| 5 | + assert: |
| 6 | + that: |
| 7 | + - kubenode1_ip is defined |
| 8 | + - kubenode2_ip is defined |
| 9 | + - kubenode3_ip is defined |
| 10 | + fail_msg: | |
| 11 | + Required variables not defined: |
| 12 | + - kubenode1_ip: {{ kubenode1_ip | default('UNDEFINED') }} |
| 13 | + - kubenode2_ip: {{ kubenode2_ip | default('UNDEFINED') }} |
| 14 | + - kubenode3_ip: {{ kubenode3_ip | default('UNDEFINED') }} |
| 15 | + quiet: yes |
| 16 | + |
| 17 | + - name: set ipv4 forward |
| 18 | + sysctl: |
| 19 | + name: net.ipv4.ip_forward |
| 20 | + value: '1' |
| 21 | + sysctl_set: true |
| 22 | + state: present |
| 23 | + reload: true |
| 24 | + |
| 25 | + - name: Check if /etc/nftables.conf exists |
| 26 | + stat: |
| 27 | + path: /etc/nftables.conf |
| 28 | + register: nftables_conf_check |
| 29 | + |
| 30 | + - name: Generate nftables.conf.new to compare |
| 31 | + template: |
| 32 | + src: files/wiab_server_nftables.conf.j2 |
| 33 | + dest: /tmp/nftables.conf.new |
| 34 | + mode: 0750 |
| 35 | + owner: root |
| 36 | + group: root |
| 37 | + |
| 38 | + - name: Compare existing and new nftables config |
| 39 | + shell: | |
| 40 | + if diff -q /etc/nftables.conf /tmp/nftables.conf.new > /dev/null; then |
| 41 | + echo "NO_CHANGE" |
| 42 | + else |
| 43 | + echo "CHANGE_DETECTED" |
| 44 | + fi |
| 45 | + register: nftables_diff_result |
| 46 | + changed_when: false |
| 47 | + when: nftables_conf_check.stat.exists |
| 48 | + |
| 49 | + - name: Backup existing nftables.conf before update |
| 50 | + shell: | |
| 51 | + TIMESTAMP=$(date +%Y%m%d_%H%M%S) |
| 52 | + cp /etc/nftables.conf /etc/nftables.conf.pre_wire.$TIMESTAMP |
| 53 | + echo "Backed up to /etc/nftables.conf.pre_wire.$TIMESTAMP" |
| 54 | + register: backup_result |
| 55 | + when: |
| 56 | + - nftables_conf_check.stat.exists |
| 57 | + - nftables_diff_result.stdout == "CHANGE_DETECTED" |
| 58 | + |
| 59 | + - name: Copy /etc/nftables.conf |
| 60 | + copy: |
| 61 | + src: /tmp/nftables.conf.new |
| 62 | + dest: /etc/nftables.conf |
| 63 | + mode: 0750 |
| 64 | + owner: root |
| 65 | + group: root |
| 66 | + remote_src: yes |
| 67 | + register: nftables_template |
| 68 | + notify: nftables | restart |
| 69 | + when: | |
| 70 | + not nftables_conf_check.stat.exists or |
| 71 | + nftables_diff_result.stdout == "CHANGE_DETECTED" |
| 72 | +
|
| 73 | + - name: Clean up temporary template file |
| 74 | + file: |
| 75 | + path: /tmp/nftables.conf.new |
| 76 | + state: absent |
| 77 | + |
| 78 | + handlers: |
| 79 | + - name: nftables | restart |
| 80 | + service: |
| 81 | + name: nftables |
| 82 | + enabled: true |
| 83 | + state: restarted |
| 84 | + become: true |
0 commit comments