Skip to content

Commit 1bfbb2b

Browse files
UbuntuGavinHeff
authored andcommitted
Add v2 of cloud facts playbook
1 parent 443214e commit 1bfbb2b

File tree

3 files changed

+160
-34
lines changed

3 files changed

+160
-34
lines changed

etc/kayobe/ansible.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ inject_facts_as_vars = False
1010
callbacks_enabled = ansible.posix.profile_tasks
1111
# Silence warning about invalid characters found in group names
1212
force_valid_group_names = ignore
13+
# Default value plus custom filter plugins path
14+
filter_plugins = $ANSIBLE_HOME/plugins/filter:/usr/share/ansible/plugins/filter:$KAYOBE_CONFIG_PATH/ansible/filter_plugins/
1315

1416
[inventory]
1517
# Fail when any inventory source cannot be parsed.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python3
2+
3+
from ansible.errors import AnsibleFilterError
4+
5+
class FilterModule(object):
6+
def filters(self):
7+
return {
8+
'group_hostvars_by_var':
9+
self.group_hostvars_by_var,
10+
'get_hostvars_by_host':
11+
self.get_hostvars_by_host
12+
}
13+
14+
def group_hostvars_by_var(self, hostvars, var, subkey=None):
15+
"""
16+
Returns a dictionary where the keys are values for the
17+
specified var in hostvars, and the values are the hosts
18+
that match that value.
19+
20+
For example, a grouping of hosts by OS release might look like:
21+
distribution_release:
22+
noble:
23+
- node1
24+
- node2
25+
jammy:
26+
- node3
27+
- node4
28+
- node5
29+
30+
Some Ansible commands, such as ansible.builtin.command, return a
31+
dict rather than a single value. So 'subkey' is used for these cases
32+
to access the desired value.
33+
"""
34+
result = {}
35+
36+
for host in hostvars.keys():
37+
try:
38+
indiv_host_var = hostvars[host][var]
39+
if subkey is not None:
40+
indiv_host_var = indiv_host_var[subkey]
41+
result.setdefault(indiv_host_var, []).append(host)
42+
except KeyError as e:
43+
raise AnsibleFilterError(f"Variable {var} not found for host {host} in hostvars: {e}")
44+
45+
return result
46+
47+
def get_hostvars_by_host(self, hostvars, var, subkey=None):
48+
"""
49+
Returns a dictionary where the keys are hosts and the values
50+
are the values for the specified var in hostvars.
51+
52+
For example, the deployed containers by host might look like:
53+
deployed_containers:
54+
node1:
55+
- grafana
56+
- glance
57+
- nova
58+
- prometheus
59+
node2:
60+
- designate
61+
- neutron
62+
- nova
63+
64+
Some Ansible commands, such as ansible.builtin.command, return a
65+
dict rather than a single value. So 'subkey' is used for these cases
66+
to access the desired value.
67+
"""
68+
result = {}
69+
for host in hostvars.keys():
70+
try:
71+
indiv_host_var = hostvars[host][var]
72+
for key in indiv_host_var.keys():
73+
# Check if task to assign value was skipped
74+
if key == "skipped":
75+
result[host] = "No data"
76+
continue
77+
if subkey is not None:
78+
indiv_host_var = indiv_host_var[subkey]
79+
if indiv_host_var:
80+
result[host] = indiv_host_var
81+
else:
82+
result[host] = []
83+
84+
except KeyError as e:
85+
raise AnsibleFilterError(f"Variable {var} not found for host {host} in hostvars: {e}")
86+
87+
return result
Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,78 @@
11
---
2+
- name: Get facts for control host
3+
hosts: localhost
4+
gather_facts: true
5+
tasks:
6+
- name: Check if ngs enabled
7+
ansible.builtin.set_fact:
8+
ngs_enabled: >-
9+
{{ 'genericswitch' in kolla_neutron_ml2_mechanism_drivers | default(false) }}
10+
when: kolla_neutron_ml2_mechanism_drivers
11+
12+
- name: Get facts for seed node
13+
hosts: seed
14+
gather_facts: true
15+
tasks:
16+
- name: Is there a dedicated seed node?
17+
ansible.builtin.set_fact:
18+
seed_node_is_vm: "{{ ansible_facts.virtualization_role == 'guest' }}"
19+
20+
- name: Get facts
21+
hosts: all
22+
gather_facts: true
23+
tasks:
24+
- name: Pinging stackhpc.com to check internet connectivity
25+
ansible.builtin.command: "ping stackhpc.com -c 3"
26+
register: successful_ping
27+
timeout: 5
28+
ignore_errors: true
29+
30+
- name: Set internet connectivity fact
31+
ansible.builtin.set_fact:
32+
internet_connectivity: "{{ not successful_ping.failed }}"
33+
- name: Get OS distribution
34+
ansible.builtin.set_fact:
35+
distribution: "{{ ansible_facts.distribution }}"
36+
37+
38+
- name: Get OS distribution release
39+
ansible.builtin.set_fact:
40+
distribution_release: "{{ ansible_facts.distribution_release }}"
41+
42+
43+
- name: Get kernel version
44+
ansible.builtin.command: "uname -r"
45+
register: kernel_version
46+
47+
- name: Check if docker is installed
48+
ansible.builtin.command: "which docker"
49+
register: docker_check
50+
failed_when: docker_check.rc not in [0]
51+
ignore_errors: true
52+
53+
- name: Get running contianers
54+
ansible.builtin.command: "docker ps --format {% raw %}'{{.Names}}'{% endraw %}"
55+
become: true
56+
register: containers_list
57+
when: not docker_check.failed
58+
59+
- name: Check if hugepages enabled
60+
ansible.builtin.set_fact:
61+
hugepages_enabled: "{{ 'hugepages' in ansible_facts.cmdline }}"
62+
263
- name: Gather Cloud Facts
364
hosts: localhost
465
gather_facts: true
566
tasks:
667
- name: Write facts to file
768
vars:
869
cloud_facts:
9-
ansible_control_host_distribution: "{{ ansible_facts.distribution }}"
10-
ansible_control_host_distribution_release: "{{ ansible_facts.distribution_release }}"
70+
internet_connectivity: "{{ hostvars | group_hostvars_by_var('internet_connectivity') }}"
71+
seed_node_is_vm: "{{ hostvars[groups['seed'][0]]['seed_node_is_vm'] | default('N/A') if groups ['seed'] else 'N/A' }}"
72+
distributions: "{{ hostvars | group_hostvars_by_var('distribution') }}"
73+
dist_releases: "{{ hostvars | group_hostvars_by_var('distribution_release') }}"
74+
kernel_versions: "{{ hostvars | group_hostvars_by_var('kernel_version','stdout') }}"
75+
deployed_containers: "{{ hostvars | get_hostvars_by_host('containers_list', 'stdout_lines') | default({}) }}"
1176
openstack_release: "{{ openstack_release }}"
1277
openstack_release_name: "{{ openstack_release_codename }}"
1378
ansible_control_host_is_vm: "{{ ansible_facts.virtualization_role == 'guest' }}"
@@ -22,41 +87,10 @@
2287
ceph_release: "{{ cephadm_ceph_release }}"
2388
storage_hyperconverged: "{{ groups['controllers'] | intersect(groups['osds']) | length > 0 | bool }}"
2489
wazuh_enabled: "{{ groups['wazuh-agent'] | length > 0 | bool }}"
90+
ngs_enabled: "{{ ngs_enabled | default(false) }}"
2591
kayobe_managed_switches: "{{ groups['switches'] | length > 0 | bool }}"
2692
proxy_configured: "{{ http_proxy | bool or https_proxy | bool }}"
2793
bifrost_version: "{{ kolla_bifrost_source_version }}"
28-
barbican_enabled: "{{ kolla_enable_barbican }}"
29-
nova_enabled: "{{ kolla_enable_nova }}"
30-
neutron_enabled: "{{ kolla_enable_neutron }}"
31-
ovs_enabled: "{{ kolla_enable_openvswitch }}"
32-
ovn_enabled: "{{ kolla_enable_ovn }}"
33-
glance_enabled: "{{ kolla_enable_glance }}"
34-
cinder_enabled: "{{ kolla_enable_cinder }}"
35-
keystone_enabled: "{{ kolla_enable_keystone }}"
36-
horizon_enabled: "{{ kolla_enable_horizon }}"
37-
fluentd_enabled: "{{ kolla_enable_fluentd }}"
38-
rabbitmq_enabled: "{{ kolla_enable_rabbitmq }}"
39-
mariadb_enabled: "{{ kolla_enable_mariadb }}"
40-
mariabackup_enabled: "{{ kolla_enable_mariabackup }}"
41-
memcached_enabled: "{{ kolla_enable_memcached }}"
42-
haproxy_enabled: "{{ kolla_enable_haproxy }}"
43-
keepalived_enabled: "{{ kolla_enable_keepalived }}"
44-
octavia_enabled: "{{ kolla_enable_octavia }}"
45-
designate_enabled: "{{ kolla_enable_designate }}"
46-
manila_enabled: "{{ kolla_enable_manila }}"
47-
magnum_enabled: "{{ kolla_enable_magnum }}"
48-
heat_enabled: "{{ kolla_enable_heat }}"
49-
ironic_enabled: "{{ kolla_enable_ironic }}"
50-
skyline_enabled: "{{ kolla_enable_skyline }}"
51-
blazar_enabled: "{{ kolla_enable_blazar }}"
52-
pulp_enabled: "{{ seed_pulp_container_enabled }}"
53-
opensearch_enabled: "{{ kolla_enable_opensearch }}"
54-
opensearch_dashboards_enabled: "{{ kolla_enable_opensearch_dashboards }}"
55-
influxdb_enabled: "{{ kolla_enable_influxdb }}"
56-
grafana_enabled: "{{ kolla_enable_grafana }}"
57-
prometheus_enabled: "{{ kolla_enable_prometheus }}"
58-
cloudkitty_enabled: "{{ kolla_enable_cloudkitty }}"
59-
telegraf_enabled: "{{ kolla_enable_telegraf }}"
6094
internal_tls_enabled: "{{ kolla_enable_tls_internal }}"
6195
external_tls_enabled: "{{ kolla_enable_tls_external }}"
6296
firewalld_enabled_all: >-
@@ -82,6 +116,9 @@
82116
stackhpc_package_repos_enabled: "{{ stackhpc_repos_enabled }}"
83117
pulp_tls_enabled: "{{ pulp_enable_tls }}"
84118
kolla_image_tags: "{{ kolla_image_tags }}"
119+
gpu_passthrough_enabled: "{{ gpu_group_map | length > 0 | bool }}"
120+
vGPU_enabled: "{{ groups['vgpu'] | length > 0 | bool }}"
121+
hugepages_enabled: "{{ hostvars | group_hostvars_by_var('hugepages_enabled') }}"
85122
ansible.builtin.copy:
86123
content: "{{ cloud_facts | to_nice_json(sort_keys=false) }}"
87124
dest: ~/cloud-facts.json

0 commit comments

Comments
 (0)