iptables.yaml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. - 10.***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.service_mgr }}"
  21. - ansible_facts.distribution "{{ ansible_facts.distribution }}"
  22. - ansible_facts.distribution_major_version "{{ ansible_facts.distribution_major_version }}"
  23. - ansible_facts.distribution_file_variety "{{ ansible_facts.distribution_file_variety }}"
  24. #- "{{ ansible_facts.services_iptables'].state }}"
  25. #- ansible_facts.services "{{ ansible_facts.services }}"
  26. - name: Show MONSRC variables
  27. debug:
  28. msg: "MONSRCs: {{ MONSRC[0-2] }}"
  29. - name: Show MONSRCRANGE variables
  30. debug:
  31. msg: "MONSRCRANGEs: {{ MONSRCRANGE[0-2] }}"
  32. - name: Populate systemd service_facts
  33. service_facts:
  34. - debug:
  35. msg:
  36. - ansible_facts.services['firewalld.service'] "{{ ansible_facts.services['firewalld.service'] }}"
  37. when: "'firewalld.service' in services"
  38. - debug:
  39. msg:
  40. - ansible_facts.services['iptables.service'] "{{ ansible_facts.services['iptables.service'] }}"
  41. when: "'iptables.service' in services"
  42. - debug:
  43. msg:
  44. - ansible_all_ipv4_addresses "{{ ansible_all_ipv4_addresses}}"
  45. - name: Open 10050/tcp from $MONSRC0 ("{{ MONSRC[0] }}") if iptables.service is enabled
  46. iptables:
  47. action: insert
  48. chain: INPUT
  49. source: "{{ MONSRC[0] }}"
  50. protocol: tcp
  51. destination_port: 10050:10051
  52. state: present
  53. jump: ACCEPT
  54. when: ansible_facts.services['iptables.service']['status'] == 'enabled'
  55. - name: Open 10050/tcp from $MONSRC1 ("{{ MONSRC[1] }}") if iptables.service is enabled
  56. iptables:
  57. action: insert
  58. chain: INPUT
  59. source: "{{ MONSRC[1] }}"
  60. protocol: tcp
  61. destination_port: 10050:10051
  62. state: present
  63. jump: ACCEPT
  64. when: ansible_facts.services['iptables.service']['status'] == 'enabled'
  65. - name: Open 10050/tcp from $MONSRC2 ("{{ MONSRC[2] }}") if iptables.service is enabled AND IP contains 10.***CONTENTS REDACTED***
  66. iptables:
  67. action: insert
  68. chain: INPUT
  69. source: "{{ MONSRC[2] }}"
  70. protocol: tcp
  71. destination_port: 10050:10051
  72. state: present
  73. jump: ACCEPT
  74. when:
  75. - ansible_facts.services['iptables.service']['status'] == 'enabled'
  76. - ansible_all_ipv4_addresses is search("10.***CONTENTS REDACTED***")
  77. - name: Open 10050/tcp from $MONSRCRANGE0 ("{{ MONSRCRANGE[0] }}") if iptables.service is enabled
  78. iptables:
  79. action: insert
  80. chain: INPUT
  81. src_range: "{{ MONSRCRANGE[0] }}"
  82. protocol: tcp
  83. destination_port: 10050:10051
  84. state: present
  85. jump: ACCEPT
  86. when: ansible_facts.services['iptables.service']['status'] == 'enabled'
  87. - name: Open 10050/tcp from $MONSRCRANGE1 ("{{ MONSRCRANGE[1] }}") if iptables.service is enabled
  88. iptables:
  89. action: insert
  90. chain: INPUT
  91. src_range: "{{ MONSRCRANGE[1] }}"
  92. protocol: tcp
  93. destination_port: 10050:10051
  94. state: present
  95. jump: ACCEPT
  96. when: ansible_facts.services['iptables.service']['status'] == 'enabled'
  97. - name: Save current state of the firewall in system file if iptables is enabled
  98. iptables_state:
  99. state: saved
  100. path: /etc/sysconfig/iptables
  101. when: ansible_facts.services['iptables.service']['status'] == 'enabled'