You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.3 KiB

  1. ---
  2. - name: Hosts | create hosts list from inventory
  3. set_fact:
  4. etc_hosts_inventory_block: |-
  5. {% for item in (groups['k8s_cluster'] + groups['etcd'] | default([]) + groups['calico_rr'] | default([])) | unique -%}
  6. {% if 'access_ip' in hostvars[item] or 'ip' in hostvars[item] or 'ansible_default_ipv4' in hostvars[item] -%}
  7. {{ hostvars[item]['access_ip'] | default(hostvars[item]['ip'] | default(hostvars[item]['ansible_default_ipv4']['address'])) }}
  8. {%- if ('ansible_hostname' in hostvars[item] and item != hostvars[item]['ansible_hostname']) %} {{ hostvars[item]['ansible_hostname'] }}.{{ dns_domain }} {{ hostvars[item]['ansible_hostname'] }} {% else %} {{ item }}.{{ dns_domain }} {{ item }} {% endif %}
  9. {% endif %}
  10. {% endfor %}
  11. delegate_to: localhost
  12. connection: local
  13. delegate_facts: true
  14. run_once: true
  15. - name: Hosts | populate inventory into hosts file
  16. blockinfile:
  17. path: /etc/hosts
  18. block: "{{ hostvars.localhost.etc_hosts_inventory_block }}"
  19. state: "{{ 'present' if populate_inventory_to_hosts_file else 'absent' }}"
  20. create: true
  21. backup: true
  22. unsafe_writes: true
  23. marker: "# Ansible inventory hosts {mark}"
  24. mode: "0644"
  25. - name: Hosts | populate kubernetes loadbalancer address into hosts file
  26. lineinfile:
  27. dest: /etc/hosts
  28. regexp: ".*{{ apiserver_loadbalancer_domain_name }}$"
  29. line: "{{ loadbalancer_apiserver.address }} {{ apiserver_loadbalancer_domain_name }}"
  30. state: present
  31. backup: true
  32. unsafe_writes: true
  33. when:
  34. - populate_loadbalancer_apiserver_to_hosts_file
  35. - loadbalancer_apiserver is defined
  36. - loadbalancer_apiserver.address is defined
  37. - name: Hosts | Update localhost entries in hosts file
  38. when: populate_localhost_entries_to_hosts_file
  39. block:
  40. - name: Hosts | Retrieve hosts file content
  41. slurp:
  42. src: /etc/hosts
  43. register: etc_hosts_content
  44. - name: Hosts | Extract existing entries for localhost from hosts file
  45. set_fact:
  46. etc_hosts_localhosts_dict: >-
  47. {%- set splitted = (item | regex_replace('[ \t]+', ' ') | regex_replace('#.*$') | trim).split(' ') -%}
  48. {{ etc_hosts_localhosts_dict | default({}) | combine({splitted[0]: splitted[1::]}) }}
  49. with_items: "{{ (etc_hosts_content['content'] | b64decode).splitlines() }}"
  50. when:
  51. - etc_hosts_content.content is defined
  52. - (item is match('^::1 .*') or item is match('^127.0.0.1 .*'))
  53. - name: Hosts | Update target hosts file entries dict with required entries
  54. set_fact:
  55. etc_hosts_localhosts_dict_target: >-
  56. {%- set target_entries = (etc_hosts_localhosts_dict | default({})).get(item.key, []) | difference(item.value.get('unexpected', [])) -%}
  57. {{ etc_hosts_localhosts_dict_target | default({}) | combine({item.key: (target_entries + item.value.expected) | unique}) }}
  58. loop: "{{ etc_hosts_localhost_entries | dict2items }}"
  59. - name: Hosts | Update (if necessary) hosts file
  60. lineinfile:
  61. dest: /etc/hosts
  62. line: "{{ item.key }} {{ item.value | join(' ') }}"
  63. regexp: "^{{ item.key }}.*$"
  64. state: present
  65. backup: true
  66. unsafe_writes: true
  67. loop: "{{ etc_hosts_localhosts_dict_target | default({}) | dict2items }}"
  68. # gather facts to update ansible_fqdn
  69. - name: Update facts
  70. setup:
  71. gather_subset: min
  72. when:
  73. - not dns_late