|
26 | 26 | blockinfile:
|
27 | 27 | dest: /etc/hosts
|
28 | 28 | marker: "# {mark} ANSIBLE MANAGED: Proxmox Cluster Hosts"
|
29 |
| - content: | |
30 |
| - {% for host in groups[pve_group] %} |
31 |
| - {{ hostvars[host].pve_cluster_addr0 }} {{ hostvars[host].ansible_fqdn }} {{ hostvars[host].ansible_hostname }}{% if ansible_fqdn == hostvars[host].ansible_fqdn %} pvelocalhost{% endif %} |
| 29 | + content: "\ |
| 30 | + {% for host in groups[pve_group] %}\ |
| 31 | + {{ hostvars[host].pve_cluster_addr0 }} |
| 32 | + {{ hostvars[host].ansible_fqdn }} |
| 33 | + {{ hostvars[host].ansible_hostname }}\ |
| 34 | + {% if ansible_fqdn == hostvars[host].ansible_fqdn %} pvelocalhost{% endif %} |
32 | 35 |
|
33 |
| - {% endfor %} |
| 36 | +
|
| 37 | + {% endfor %}" |
34 | 38 |
|
35 | 39 | - name: Remove conflicting lines in hosts files
|
36 | 40 | lineinfile:
|
37 | 41 | dest: /etc/hosts
|
38 | 42 | # expanded, this turns out to be, for example:
|
39 |
| - # regexp: '^(?!10\.0\.3\.17 test01\.lxc test01( pvelocalhost)?)(?!10\.0\.3\.17)[\w:.]+(\s+.*)?\s(test01\.lxc|test01|pvelocalhost)(\s+.*|\s*)$' |
| 43 | + # regexp: "^(?!10\.0\.3\.17\\ test01\.lxc\\ test01\\ pvelocalhost)(?!10\.0\.3\.17)[0-9a-f:.]+(\s+.*)?\s(test01\.lxc|test01|pvelocalhost)(\s+.*|\s*)$' |
40 | 44 | # basically first we ignore lines that match from the host enumeration task
|
41 | 45 | # above, then we match against different IPs (e.g. NOT 10.0.3.17) that have
|
42 | 46 | # the hostname/fqdn we inserted a record for previously, taking care also to
|
43 | 47 | # detect word boundaries (\b wasn't working for some reason)
|
44 |
| - regexp: '^(?!{{ hostvars[item].pve_cluster_addr0 | regex_escape() }} {{ hostvars[item].ansible_fqdn | regex_escape() }} {{ hostvars[item].ansible_hostname | regex_escape() }}( pvelocalhost)?)(?!{{ hostvars[item].pve_cluster_addr0 | regex_escape() }})[\w:.]+(\s+.*)?\s({{ hostvars[item].ansible_fqdn | regex_escape() }}|{{ hostvars[item].ansible_hostname | regex_escape() }}{% if ansible_fqdn == hostvars[item].ansible_fqdn %}|pvelocalhost{% endif %})(\s+.*|\s*)$' |
| 48 | + regexp: "\ |
| 49 | + ^(?!\ |
| 50 | + {{ _correct_line | regex_escape() }}\ |
| 51 | + )\ |
| 52 | + {# Ignore lines starting with the current cluster host #}\ |
| 53 | + (?!{{ _correct_ip | regex_escape() }})\ |
| 54 | + {# Match an IPv4/v6 address at the start #}\ |
| 55 | + [0-9a-f:.]\ |
| 56 | + {# Match any hostnames, surrounded by whitespace #}\ |
| 57 | + +(\\s+.*)?\\s\ |
| 58 | + (\ |
| 59 | + {{ _match_hosts | map('regex_escape') | join('|') }}\ |
| 60 | + )\ |
| 61 | + (\\s+.*|\\s*)$" |
45 | 62 | state: absent
|
46 | 63 | backup: yes
|
47 |
| - with_items: "{{ groups[pve_group] }}" |
| 64 | + loop: "{{ groups[pve_group] }}" |
| 65 | + vars: |
| 66 | + _correct_line: "\ |
| 67 | + {{ hostvars[item].pve_cluster_addr0 }} |
| 68 | + {{ hostvars[item].ansible_fqdn }} |
| 69 | + {{ hostvars[item].ansible_hostname }}\ |
| 70 | + {% if ansible_fqdn == hostvars[item].ansible_fqdn %} pvelocalhost{% endif %}" |
| 71 | + _correct_ip: "{{ hostvars[item].pve_cluster_addr0 }}" |
| 72 | + _match_hosts: >- |
| 73 | + [ |
| 74 | + "{{ hostvars[item].ansible_fqdn }}", |
| 75 | + "{{ hostvars[item].ansible_hostname }}", |
| 76 | + {% if ansible_fqdn == hostvars[item].ansible_fqdn %} |
| 77 | + "pvelocalhost" |
| 78 | + {% endif %} |
| 79 | + ] |
48 | 80 |
|
49 | 81 | - name: Trust Proxmox' packaging key
|
50 | 82 | apt_key:
|
|
0 commit comments