Skip to content

Commit 9d48307

Browse files
UbuntuGavinHeff
authored andcommitted
Edits from review
1 parent 5f5680a commit 9d48307

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

etc/kayobe/ansible/filter_plugins/filters.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!usr/bin/env python3
1+
#!/usr/bin/env python3
22

33
from ansible.errors import AnsibleFilterError
44

@@ -12,6 +12,11 @@ def filters(self):
1212
}
1313

1414
def group_hostvars_by_var(self, hostvars, var, stdout=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+
"""
1520
result = {}
1621

1722
for host in hostvars.keys():
@@ -20,36 +25,34 @@ def group_hostvars_by_var(self, hostvars, var, stdout=None):
2025
indiv_host_var = indiv_host_all[var]
2126
if stdout is not None:
2227
indiv_host_var = indiv_host_var[stdout]
23-
if indiv_host_var not in result.keys():
24-
result[indiv_host_var] = [host]
25-
else:
26-
result[indiv_host_var].append(host)
28+
result.setdefault(indiv_host_var, []).append(host)
2729
except KeyError as e:
28-
raise AnsibleFilterError(f"Variable {var} not found in hostvars: {e}")
30+
raise AnsibleFilterError(f"Variable {var} not found for host {host} in hostvars: {e}")
2931

3032
return result
3133

32-
def group_hostvars_by_host(self, hostvars, var, stdout=None, hosts=None):
34+
def group_hostvars_by_host(self, hostvars, var, stdout=None, hostgroup=None):
3335
result = {}
34-
35-
### Currently returns blank dict, try block doesn't appear to be working
36-
37-
if not hosts:
38-
hosts = hostvars.keys()
39-
40-
for host in hosts:
36+
"""
37+
Returns a dictionary where the keys are hosts and the values
38+
are the values for the specified var in hostvars. If hosts is
39+
specified, only those hosts are included.
40+
"""
41+
for host in hostvars.keys():
4142
try:
4243
indiv_host_vars = hostvars[host][var]
44+
for key in indiv_host_vars.keys():
45+
if key == "skipped":
46+
return {}
4347
if stdout is not None:
44-
indiv_host_vars = indiv_host_vars['stdout_lines']
48+
indiv_host_vars = indiv_host_vars[stdout]
4549
if indiv_host_vars:
4650
result[host] = indiv_host_vars
4751
else:
4852
result[host] = []
4953

5054
except KeyError as e:
51-
AnsibleFilterError(f"Host {host} not found in hostvars.keys(): {e}")
52-
55+
raise AnsibleFilterError(f"Variable {var} not found for host {host} in hostvars: {e}")
5356

5457
return result
5558

etc/kayobe/ansible/get-cloud-facts.yml

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,11 @@
33
hosts: localhost
44
gather_facts: true
55
tasks:
6-
- name: Check if kolla_neutron_ml2_mechanism_drivers is defined
7-
ansible.builtin.set_fact:
8-
kolla_neutron_ml2_mechanism_drivers_defined: "{{ kolla_neutron_ml2_mechanism_drivers is defined }}"
96
- name: Check if ngs enabled
107
ansible.builtin.set_fact:
118
ngs_enabled: >-
12-
{{ 'genericswitch' in kolla_neutron_ml2_mechanism_drivers }}
13-
when: kolla_neutron_ml2_mechanism_drivers_defined
14-
- name: Can the control host reach the internet?
15-
hosts: localhost
16-
gather_facts: true
17-
tasks:
18-
- name: Check internet connectivity
19-
ansible.builtin.command: "ping stackhpc.com -c 3"
20-
register: internet_connectivity
21-
timeout: 5
22-
ignore_errors: true
23-
24-
- name: Set internet connectivity fact
25-
ansible.builtin.set_fact:
26-
internet_connectivity: "{{ not internet_connectivity.failed }}"
9+
{{ 'genericswitch' in kolla_neutron_ml2_mechanism_drivers | default(false) }}
10+
when: kolla_neutron_ml2_mechanism_drivers
2711

2812
- name: Get facts for seed node
2913
hosts: seed
@@ -37,6 +21,16 @@
3721
hosts: all
3822
gather_facts: true
3923
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+
4034
- name: Get OS distribution
4135
ansible.builtin.set_fact:
4236
distribution: "{{ ansible_facts.distribution }}"
@@ -49,18 +43,22 @@
4943
ansible.builtin.command: "uname -r"
5044
register: kernel_version
5145

46+
- name: Check if docker is installed
47+
ansible.builtin.command: "which docker"
48+
register: docker_check
49+
failed_when: docker_check.rc not in [0]
50+
ignore_errors: true
51+
5252
- name: Get running contianers
5353
ansible.builtin.command: "docker ps --format {% raw %}'{{.Names}}'{% endraw %}"
5454
become: true
5555
register: containers_list
56+
when: not docker_check.failed
5657

57-
- name: Get facts for computes
58-
hosts: compute
59-
gather_facts: true
60-
tasks:
6158
- name: Check if hugepages enabled
6259
ansible.builtin.set_fact:
63-
hugepages_enabled: "{{ ansible_facts.cmdline }}"
60+
hugepages_enabled: "{{ 'hugepages' in ansible_facts.cmdline }}"
61+
6462

6563
- name: Gather Cloud Facts
6664
hosts: localhost
@@ -69,12 +67,12 @@
6967
- name: Write facts to file
7068
vars:
7169
cloud_facts:
72-
internet_connectivity: "{{ internet_connectivity }}"
73-
seed_node_is_vm: "{{ hostvars[groups['seed'][0]]['seed_node_is_vm'] | default('N/A') }}"
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' }}"
7472
distributions: "{{ hostvars | group_hostvars_by_var('distribution') }}"
7573
dist_releases: "{{ hostvars | group_hostvars_by_var('distribution_release') }}"
7674
kernel_versions: "{{ hostvars | group_hostvars_by_var('kernel_version','stdout') }}"
77-
deployed_containers: "{{ hostvars | group_hostvars_by_host('containers_list', 'stdout') }}"
75+
deployed_containers: "{{ hostvars | group_hostvars_by_host('containers_list', 'stdout_lines') | default({}) }}"
7876
openstack_release: "{{ openstack_release }}"
7977
openstack_release_name: "{{ openstack_release_codename }}"
8078
ansible_control_host_is_vm: "{{ ansible_facts.virtualization_role == 'guest' }}"
@@ -89,8 +87,6 @@
8987
ceph_release: "{{ cephadm_ceph_release }}"
9088
storage_hyperconverged: "{{ groups['controllers'] | intersect(groups['osds']) | length > 0 | bool }}"
9189
wazuh_enabled: "{{ groups['wazuh-agent'] | length > 0 | bool }}"
92-
# ngs_enabled_: "{{ 'genericswitch' in (kolla_neutron_ml2_mechanism_drivers | default([])) | bool }}"
93-
# # ABOVE LEADS TO ERROR: argument of type ''NoneType'' is not iterable. argument of type ''NoneType'' is not iterable'
9490
ngs_enabled: "{{ ngs_enabled | default(false) }}"
9591
kayobe_managed_switches: "{{ groups['switches'] | length > 0 | bool }}"
9692
proxy_configured: "{{ http_proxy | bool or https_proxy | bool }}"
@@ -122,7 +118,7 @@
122118
kolla_image_tags: "{{ kolla_image_tags }}"
123119
gpu_passthrough_enabled: "{{ gpu_group_map | length > 0 | bool }}"
124120
vGPU_enabled: "{{ groups['vgpu'] | length > 0 | bool }}"
125-
hugepages_enabled: "{{ hostvars | group_hostvars_by_host('hugepages_enabled', hosts=['compute']) }}"
121+
hugepages_enabled: "{{ hostvars | group_hostvars_by_var('hugepages_enabled') }}"
126122
ansible.builtin.copy:
127123
content: "{{ cloud_facts | to_nice_json(sort_keys=false) }}"
128-
dest: ~/cloud-facts_2.json
124+
dest: ~/cloud-facts.json

0 commit comments

Comments
 (0)