Skip to content

Commit 397aa9f

Browse files
committed
Break down hosts enumeration strings
yaml is such a pain sometimes
1 parent 8ffa681 commit 397aa9f

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

tasks/main.yml

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,57 @@
2626
blockinfile:
2727
dest: /etc/hosts
2828
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 %}
3235
33-
{% endfor %}
36+
37+
{% endfor %}"
3438

3539
- name: Remove conflicting lines in hosts files
3640
lineinfile:
3741
dest: /etc/hosts
3842
# 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*)$'
4044
# basically first we ignore lines that match from the host enumeration task
4145
# above, then we match against different IPs (e.g. NOT 10.0.3.17) that have
4246
# the hostname/fqdn we inserted a record for previously, taking care also to
4347
# 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*)$"
4562
state: absent
4663
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+
]
4880
4981
- name: Trust Proxmox' packaging key
5082
apt_key:

0 commit comments

Comments
 (0)