TEST-iptables.yaml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ---
  2. - name: "Example of a working iptables playbook"
  3. hosts: all
  4. # - "*IPTables"
  5. gather_facts: True
  6. become: true
  7. # Put the IPs/hostnames of Zabbix servers and proxies here:
  8. vars:
  9. MONSRC:
  10. - ***CONTENTS REDACTED***
  11. - ***CONTENTS REDACTED***
  12. - ***CONTENTS REDACTED***
  13. MONSRCRANGE:
  14. - ***CONTENTS REDACTED***
  15. - ***CONTENTS REDACTED***
  16. tasks:
  17. - name: "msg print to stdout: Debug ansible_facts"
  18. debug:
  19. msg:
  20. - ansible_facts.distribution "{{ ansible_facts.distribution }}"
  21. - ansible_facts.distribution_major_version "{{ ansible_facts.distribution_major_version }}"
  22. - ansible_facts.distribution_file_variety "{{ ansible_facts.distribution_file_variety }}"
  23. - name: Show MONSRC variables
  24. debug:
  25. msg: "MONSRCs: {{ MONSRC[0-2] }}"
  26. - name: Show MONSRCRANGE variables
  27. debug:
  28. msg: "MONSRCRANGEs: {{ MONSRCRANGE[0-2] }}"
  29. - name: Populate systemd service_facts
  30. service_facts:
  31. - debug:
  32. msg:
  33. - ansible_facts.services['firewalld.service'] "{{ ansible_facts.services['firewalld.service'] }}"
  34. when: "'firewalld.service' in services"
  35. - debug:
  36. msg:
  37. - ansible_facts.services['iptables.service'] "{{ ansible_facts.services['iptables.service'] }}"
  38. when: "'iptables.service' in services"
  39. - debug:
  40. msg:
  41. - ansible_all_ipv4_addresses "{{ ansible_all_ipv4_addresses}}"
  42. - name: Open 10050/tcp from $MONSRC0 ("{{ MONSRC[0] }}") if iptables.service is enabled
  43. iptables:
  44. action: insert
  45. chain: INPUT
  46. source: "{{ MONSRC[0] }}"
  47. protocol: tcp
  48. destination_port: 10050:10051
  49. state: present
  50. jump: ACCEPT
  51. when: ansible_facts.services['iptables.service']['status'] == 'enabled'
  52. - name: Open 10050/tcp from $MONSRC1 ("{{ MONSRC[1] }}") if iptables.service is enabled
  53. iptables:
  54. action: insert
  55. chain: INPUT
  56. source: "{{ MONSRC[1] }}"
  57. protocol: tcp
  58. destination_port: 10050:10051
  59. state: present
  60. jump: ACCEPT
  61. when: ansible_facts.services['iptables.service']['status'] == 'enabled'
  62. - name: Open 10050/tcp from $MONSRC2 ("{{ MONSRC[2] }}") if iptables.service is enabled AND IP contains 10.***CONTENTS REDACTED***
  63. iptables:
  64. action: insert
  65. chain: INPUT
  66. source: "{{ MONSRC[2] }}"
  67. protocol: tcp
  68. destination_port: 10050:10051
  69. state: present
  70. jump: ACCEPT
  71. when:
  72. - ansible_facts.services['iptables.service']['status'] == 'enabled'
  73. - ansible_all_ipv4_addresses is search("10.***CONTENTS REDACTED***")
  74. - name: Open 10050/tcp from $MONSRCRANGE0 ("{{ MONSRCRANGE[0] }}") if iptables.service is enabled
  75. iptables:
  76. action: insert
  77. chain: INPUT
  78. src_range: "{{ MONSRCRANGE[0] }}"
  79. protocol: tcp
  80. destination_port: 10050:10051
  81. state: present
  82. jump: ACCEPT
  83. when: ansible_facts.services['iptables.service']['status'] == 'enabled'
  84. - name: Open 10050/tcp from $MONSRCRANGE1 ("{{ MONSRCRANGE[1] }}") if iptables.service is enabled
  85. iptables:
  86. action: insert
  87. chain: INPUT
  88. src_range: "{{ MONSRCRANGE[1] }}"
  89. protocol: tcp
  90. destination_port: 10050:10051
  91. state: present
  92. jump: ACCEPT
  93. when: ansible_facts.services['iptables.service']['status'] == 'enabled'
  94. - name: Save current state of the firewall in system file if iptables is enabled
  95. iptables_state:
  96. state: saved
  97. path: /etc/sysconfig/iptables
  98. when: ansible_facts.services['iptables.service']['status'] == 'enabled'