update_hosts_file.yaml 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. ---
  2. - name: "Update /etc/hosts with most recent info; for Prod and dev systems"
  3. hosts: all
  4. gather_facts: True
  5. become: true
  6. tasks:
  7. - name: "msg print to stdout: Debug ansible_facts"
  8. debug:
  9. msg:
  10. # - "{{ ansible_facts.service_mgr }}"
  11. - ansible_facts.distribution "{{ ansible_facts.distribution }}"
  12. - ansible_facts.distribution_major_version "{{ ansible_facts.distribution_major_version }}"
  13. - ansible_facts.distribution_file_variety "{{ ansible_facts.distribution_file_variety }}"
  14. - ansible_system "{{ ansible_system }}"
  15. - name: "Modify an already modified hosts file in a DEV environment"
  16. lineinfile:
  17. path: /etc/hosts
  18. search_string: 'monitoring'
  19. line: "10.***CONTENTS REDACTED*** monitoring.***CONTENTS REDACTED***"
  20. when:
  21. - ansible_system == "Linux"
  22. - ansible_ansible_all_ipv4_addresses is search("***CONTENTS REDACTED***") or ansible_all_ipv4_addresses is search("***CONTENTS REDACTED***") or ansible_all_ipv4_addresses is search("***CONTENTS REDACTED***")
  23. - name: "Modify an already modified hosts file in PRODUCTION"
  24. lineinfile:
  25. path: /etc/hosts
  26. search_string: 'monitoring'
  27. line: "***CONTENTS REDACTED*** monitoring.***CONTENTS REDACTED***"
  28. state: absent
  29. when:
  30. - ansible_system == "Linux"
  31. - ansible_ansible_all_ipv4_addresses is not search("10.***CONTENTS REDACTED***")
  32. - ansible_all_ipv4_addresses is not search("10.***CONTENTS REDACTED***")
  33. - ansible_all_ipv4_addresses is not search("10.***CONTENTS REDACTED***")
  34. # Restart systemd service
  35. - name: restart zabbix-agent2.service; systemd
  36. systemd:
  37. name: zabbix-agent
  38. enabled: True
  39. masked: no
  40. state: restarted
  41. ignore_errors: true
  42. when: ansible_facts.service_mgr == "systemd"
  43. - name: restart zabbix-agent2.service; systemd
  44. systemd:
  45. name: zabbix-agent2
  46. enabled: True
  47. masked: no
  48. state: restarted
  49. ignore_errors: true
  50. when: ansible_facts.service_mgr == "systemd"