Skip to content

Commit 8ffa681

Browse files
committed
Cluster configuration fixes/cleanup
Replace ring0_addr/ and friends with addr0/addr1 Remove bindnet vars and flags Deduplicate some tasks Check for facts instead of IPv4 address for cluster hosts
1 parent 2ef5c06 commit 8ffa681

8 files changed

+82
-58
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -418,18 +418,18 @@ pve_cluster_enabled: no # Set this to yes to configure hosts to be clustered tog
418418
pve_cluster_clustername: "{{ pve_group }}" # Should be set to the name of the PVE cluster
419419
```
420420

421-
Information about the following can be found in the PVE Documentation in the
422-
[Cluster Manager][pvecm-network] chapter.
421+
The following variables are used to provide networking information to corosync.
422+
These are known as ring0_addr/ring1_addr or link0_addr/link1_addr, depending on
423+
PVE version. They should be IPv4 or IPv6 addresses. For more information, refer
424+
to the [Cluster Manager][pvecm-network] chapter in the PVE Documentation.
423425

424426
```
425-
pve_cluster_ring0_addr: "{{ ansible_default_ipv4.address }}"
426-
pve_cluster_bindnet0_addr: "{{ pve_cluster_ring0_addr }}"
427-
# pve_cluster_ring1_addr: "another interface's IP address or hostname"
428-
# pve_cluster_bindnet1_addr: "{{ pve_cluster_ring1_addr }}"
429-
427+
# pve_cluster_addr0: "{{ ansible_default_ipv4.address }}"
428+
# pve_cluster_addr1: "another interface's IP address or hostname"
430429
```
431430

432431
You can set options in the datacenter.cfg configuration file:
432+
433433
```
434434
pve_datacenter_cfg:
435435
keyboard: en-us

defaults/main.yml

+2-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,8 @@ pve_ceph_crush_rules: []
2929
# pve_ssl_certificate: "contents of certificate"
3030
pve_cluster_enabled: no
3131
pve_cluster_clustername: "{{ pve_group }}"
32-
# PVE 5.x (Debian Stretch) clustering options
33-
pve_cluster_ring0_addr: "{{ ansible_default_ipv4.address }}"
34-
pve_cluster_bindnet0_addr: "{{ pve_cluster_ring0_addr }}"
35-
# pve_cluster_ring1_addr: "another interface's IP address or hostname"
36-
# pve_cluster_bindnet1_addr: "{{ pve_cluster_ring1_addr }}"
37-
# PVE 6.x (Debian Buster) clustering options
38-
pve_cluster_link0_addr: "{{ ansible_default_ipv4.address }}"
39-
# pve_cluster_link1_addr: "another interface's IP address or hostname"
32+
# pve_cluster_addr0: "{{ ansible_default_ipv4.address }}"
33+
# pve_cluster_addr1: "{{ ansible_eth1.ipv4.address }}
4034
pve_datacenter_cfg: {}
4135
pve_ssl_letsencrypt: false
4236
pve_groups: []

tasks/load_variables.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
- name: Gather distribution specific variables
3+
include_vars: "debian-{{ ansible_distribution_release }}.yml"
4+
5+
- block:
6+
# Per Proxmox documentation, bindnet_addr is expected to be an IP address and
7+
# ring_addr can be either hostname or IP, but this role has always used an IP
8+
# address. Thus, we're deprecating them. See below references.
9+
# https://pve.proxmox.com/wiki/Separate_Cluster_Network#Setup_at_Cluster_Creation
10+
# https://git.proxmox.com/?p=pve-cluster.git;a=blob;f=data/PVE/Corosync.pm;h=8b5c91e0da084da4e9ba7423176872a0c16ef5af;hb=refs/heads/stable-5#l209
11+
- name: LEGACY - Define pve_cluster_addr0 from bindnet0_addr/ring0_addr
12+
set_fact:
13+
pve_cluster_addr0: "{{ pve_cluster_bindnet0_addr | default(pve_cluster_ring0_addr) }}"
14+
when: pve_cluster_ring0_addr is defined and ansible_distribution_release == 'stretch'
15+
16+
- name: LEGACY - Define pve_cluster_addr0 from link0_addr
17+
set_fact:
18+
pve_cluster_addr0: "{{ pve_cluster_link0_addr }}"
19+
when: pve_cluster_link0_addr is defined and ansible_distribution_release == 'buster'
20+
when: pve_cluster_addr0 is not defined
21+
22+
- block:
23+
- name: LEGACY - Define pve_cluster_addr1 from bindnet1_addr/ring1_addr
24+
set_fact:
25+
pve_cluster_addr1: "{{ pve_cluster_bindnet1_addr | default(pve_cluster_ring1_addr) }}"
26+
when: pve_cluster_ring1_addr is defined and ansible_distribution_release == 'stretch'
27+
28+
- name: LEGACY - Define pve_cluster_addr1 from link1_addr
29+
set_fact:
30+
pve_cluster_addr1: "{{ pve_cluster_link1_addr }}"
31+
when: pve_cluster_link1_addr is defined and ansible_distribution_release == 'buster'
32+
when: pve_cluster_addr1 is not defined
33+
34+
- name: Define pve_cluster_addr0 if not provided
35+
set_fact:
36+
pve_cluster_addr0: "{{ pve_cluster_addr0 | default(_pve_cluster_addr0) }}"
37+
38+
- name: Calculate list of SSH addresses
39+
set_fact:
40+
pve_cluster_ssh_addrs: >-
41+
["{{ ansible_fqdn }}", "{{ ansible_hostname }}",
42+
"{{ pve_cluster_addr0 }}",
43+
{% if pve_cluster_addr1 is defined %}"{{ pve_cluster_addr1 }}"{% endif %}]

tasks/main.yml

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
---
22
# tasks file for ansible-role-proxmox
3-
- name: Gather distribution specific variables
4-
include_vars: "debian-{{ ansible_distribution_release }}.yml"
3+
- import_tasks: load_variables.yml
54

6-
- name: Ensure that we have an IP address for all cluster hosts
5+
- name: Ensure that facts are present for all cluster hosts
76
assert:
87
that:
9-
- "hostvars[item].ansible_default_ipv4.address is defined"
10-
msg: "Missing IP address and other information for {{ item }}. Have you gathered its facts?"
8+
- "hostvars[item].ansible_facts"
9+
msg: "Could not load facts for {{ item }}. Please run your playbook against all hosts in {{ pve_group }}."
1110
with_items: "{{ groups[pve_group] }}"
1211

1312
- name: Ensure this host is in the group specified
@@ -29,7 +28,7 @@
2928
marker: "# {mark} ANSIBLE MANAGED: Proxmox Cluster Hosts"
3029
content: |
3130
{% for host in groups[pve_group] %}
32-
{{ hostvars[host].pve_cluster_ring0_addr }} {{ hostvars[host].ansible_fqdn }} {{ hostvars[host].ansible_hostname }}{% if ansible_fqdn == hostvars[host].ansible_fqdn %} pvelocalhost{% endif %}
31+
{{ hostvars[host].pve_cluster_addr0 }} {{ hostvars[host].ansible_fqdn }} {{ hostvars[host].ansible_hostname }}{% if ansible_fqdn == hostvars[host].ansible_fqdn %} pvelocalhost{% endif %}
3332
3433
{% endfor %}
3534
@@ -42,7 +41,7 @@
4241
# above, then we match against different IPs (e.g. NOT 10.0.3.17) that have
4342
# the hostname/fqdn we inserted a record for previously, taking care also to
4443
# detect word boundaries (\b wasn't working for some reason)
45-
regexp: '^(?!{{ hostvars[item].pve_cluster_ring0_addr | regex_escape() }} {{ hostvars[item].ansible_fqdn | regex_escape() }} {{ hostvars[item].ansible_hostname | regex_escape() }}( pvelocalhost)?)(?!{{ hostvars[item].pve_cluster_ring0_addr | 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*)$'
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*)$'
4645
state: absent
4746
backup: yes
4847
with_items: "{{ groups[pve_group] }}"

tasks/pve_add_node.yml

+9-16
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,19 @@
44
_pve_current_node: "{{ item }}"
55

66
- name: Add node to Proxmox cluster
7-
command: "pvecm add {{ hostvars[groups[pve_group][0]]['ansible_default_ipv4']['address'] }} \
8-
-use_ssh \
9-
-ring0_addr {{ pve_cluster_ring0_addr }}{% if pve_cluster_ring1_addr is defined %} \
10-
-ring1_addr {{ pve_cluster_ring1_addr }}{% endif %}"
7+
command: >-
8+
pvecm add {{ hostvars[groups[pve_group][0]].pve_cluster_addr0 }} -use_ssh
9+
{{ addr0_flag }} {{ pve_cluster_addr0 }}
10+
{% if pve_cluster_addr1 is defined %}
11+
{{ addr1_flag }} {{ pve_cluster_addr1 }}
12+
{% endif %}
1113
args:
1214
creates: "{{ pve_cluster_conf }}"
15+
vars:
16+
addr0_flag: "{{ (ansible_distribution_release == 'buster') | ternary('-link0', '-ring0_addr') }}"
17+
addr1_flag: "{{ (ansible_distribution_release == 'buster') | ternary('-link1', '-ring1_addr') }}"
1318
when:
1419
- "inventory_hostname == _pve_current_node"
15-
- "ansible_distribution_release == 'stretch'"
16-
17-
- name: Add node to Proxmox cluster
18-
command: "pvecm add {{ hostvars[groups[pve_group][0]]['ansible_default_ipv4']['address'] }} \
19-
-use_ssh \
20-
-link0 {{ pve_cluster_link0_addr }}{% if pve_cluster_link1_addr is defined %} \
21-
-link1 {{ pve_cluster_link1_addr }}{% endif %}"
22-
args:
23-
creates: "{{ pve_cluster_conf }}"
24-
when:
25-
- "inventory_hostname == _pve_current_node"
26-
- "ansible_distribution_release == 'buster'"
2720

2821
- name: Remove stale corosync lock file due to lack of quorum during initialization
2922
file:

tasks/pve_cluster_config.yml

+9-17
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,20 @@
3535
when: "(_pve_found_clusters | default([]) | length) == 1"
3636

3737
- name: Initialize a Proxmox cluster
38-
command: "pvecm create {{ pve_cluster_clustername }} -bindnet0_addr {{ pve_cluster_bindnet0_addr }} \
39-
-ring0_addr {{ pve_cluster_ring0_addr }}\
40-
{% if pve_cluster_bindnet1_addr is defined and pve_cluster_ring1_addr is defined %} \
41-
-bindnet1_addr {{ pve_cluster_bindnet1_addr }} -ring1_addr {{ pve_cluster_ring1_addr }}{% endif %}"
42-
args:
43-
creates: "{{ pve_cluster_conf }}"
44-
when:
45-
- "_pve_found_clusters is not defined"
46-
- "inventory_hostname == groups[pve_group][0]"
47-
- "ansible_distribution_release == 'stretch'"
48-
49-
- name: Initialize a Proxmox cluster
50-
command: "pvecm create {{ pve_cluster_clustername }} \
51-
-link0 {{ pve_cluster_link0_addr }}\
52-
{% if pve_cluster_link1_addr is defined %} \
53-
-link1 {{ pve_cluster_link1_addr }}{% endif %}"
38+
command: >-
39+
pvecm create {{ pve_cluster_clustername }}
40+
{{ addr0_flag }} {{ pve_cluster_addr0 }}
41+
{% if pve_cluster_addr1 is defined %}
42+
{{ addr1_flag }} {{ pve_cluster_addr1 }}
43+
{% endif %}
5444
args:
5545
creates: "{{ pve_cluster_conf }}"
46+
vars:
47+
addr0_flag: "{{ (ansible_distribution_release == 'buster') | ternary('-link0', '-ring0_addr') }}"
48+
addr1_flag: "{{ (ansible_distribution_release == 'buster') | ternary('-link1', '-ring1_addr') }}"
5649
when:
5750
- "_pve_found_clusters is not defined"
5851
- "inventory_hostname == groups[pve_group][0]"
59-
- "ansible_distribution_release == 'buster'"
6052

6153
- name: Wait for quorum on initialization node
6254
proxmox_query:

tasks/ssh_cluster_config.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
marker: "# {mark}: PVE host configuration options (managed by ansible)."
3434
content: |
3535
{% for host in groups[pve_group] %}
36-
Host {{ hostvars[host].ansible_fqdn }} {{ hostvars[host].ansible_hostname }} {{ hostvars[host].ansible_default_ipv4.address }}
36+
Host {{ hostvars[host].pve_cluster_ssh_addrs | join(" ") }}
3737
IdentityFile /root/.ssh/id_rsa
3838
Port {{ pve_ssh_port }}
3939
{% endfor %}
@@ -44,7 +44,7 @@
4444
marker: "# {mark}: Allow root logins from PVE hosts (managed by ansible)."
4545
content: |
4646
{% for host in groups[pve_group] %}
47-
Match Address {{ hostvars[host].pve_cluster_ring0_addr }}
47+
Match Address {{ hostvars[host].pve_cluster_ssh_addrs | join(",") }}
4848
PermitRootLogin prohibit-password
4949
{% endfor %}
5050
validate: "/usr/sbin/sshd -t -f %s"
@@ -74,7 +74,7 @@
7474
content: |
7575
{% for host in groups[pve_group] %}
7676
{% for keytype in ['rsa', 'ed25519', 'ecdsa'] %}
77-
{{ hostvars[host].ansible_fqdn }},{{ hostvars[host].ansible_hostname }},{{ hostvars[host].ansible_default_ipv4.address }} {{ ' '.join(lookup('file', pve_fetch_directory + '/' + host + '/ssh_host_' + keytype + '_key.pub').split()[:-1]) }}
77+
{{ hostvars[host].pve_cluster_ssh_addrs | join(",") }} {{ ' '.join(lookup('file', pve_fetch_directory + '/' + host + '/ssh_host_' + keytype + '_key.pub').split()[:-1]) }}
7878
{% endfor %}
7979
{% endfor %}
8080
when:

vars/main.yml

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
# vars file for ansible-role-proxmox
33
pve_base_dir: "/etc/pve"
44
pve_cluster_conf: "{{ pve_base_dir }}/corosync.conf"
5+
6+
# defaults that need to be host facts
7+
_pve_cluster_addr0: "{{ ansible_default_ipv4.address }}"

0 commit comments

Comments
 (0)