Skip to content

Commit 9c446a7

Browse files
authored
Merge pull request stackhpc#11 from stackhpc/cluster-query
Add support for querying an existing cluster
2 parents 2f0e2e7 + 4331dee commit 9c446a7

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ use.
2525

2626
`cluster_cloud`: Optional name of the OpenStack client config cloud name to use.
2727

28-
`cluster_state`: Desired state of the cluster, either `present` or `absent`.
29-
The default value is `present`.
28+
`cluster_state`: Desired state of the cluster, one of `present`, `absent`, or
29+
`query`. The default value is `present`. If the value is `query`, the cluster
30+
will not be updated, but its configuration will be queried and an Ansible
31+
inventory generated.
3032

3133
`cluster_name`: Name to give the Heat stack
3234
It defaults to `cluster`

tasks/main.yml

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,36 @@
99
ansible_python_interpreter: "{{ cluster_venv }}/bin/python"
1010
when: cluster_venv != None
1111

12-
- name: Orchestrate OpenStack infrastructure
13-
register: cluster_stack
14-
os_stack:
15-
auth_type: "{{ cluster_auth_type or omit }}"
16-
auth: "{{ cluster_auth or omit }}"
17-
cloud: "{{ cluster_cloud or omit }}"
18-
name: "{{ cluster_name }}"
19-
state: "{{ cluster_state }}"
20-
environment: "{{ cluster_environment }}"
21-
template: "{{ cluster_template }}"
22-
parameters: "{{ cluster_params }}"
12+
# Case 1: we are modifying the cluster.
13+
- block:
14+
- name: Orchestrate OpenStack infrastructure
15+
register: cluster_stack
16+
os_stack:
17+
auth_type: "{{ cluster_auth_type or omit }}"
18+
auth: "{{ cluster_auth or omit }}"
19+
cloud: "{{ cluster_cloud or omit }}"
20+
name: "{{ cluster_name }}"
21+
state: "{{ cluster_state }}"
22+
environment: "{{ cluster_environment }}"
23+
template: "{{ cluster_template }}"
24+
parameters: "{{ cluster_params }}"
25+
26+
- name: Extract node groups
27+
set_fact:
28+
cluster_group: "{{ cluster_stack.stack.outputs | selectattr('output_key', 'equalto', 'cluster_group') | first }}"
29+
when: cluster_state != 'query'
30+
31+
# Case 2: we are performing a read-only query of the cluster configuration.
32+
# Read the stack's outputs via the API.
33+
- block:
34+
- name: Gather OpenStack infrastructure information
35+
command: openstack stack output show {{ cluster_name }} cluster_group -f json
36+
register: stack_output
37+
38+
- name: Extract node groups
39+
set_fact:
40+
cluster_group: "{{ stack_output.stdout | from_json }}"
41+
when: cluster_state == 'query'
2342

2443
- name: Reset the python interpreter
2544
set_fact:
@@ -37,10 +56,6 @@
3756
src: cluster_inventory.j2
3857
dest: "{{ cluster_inventory }}"
3958

40-
- name: Extract node groups
41-
set_fact:
42-
cluster_group: "{{ cluster_stack.stack.outputs | selectattr('output_key', 'equalto', 'cluster_group') | first }}"
43-
4459
- name: Extract node objects
4560
set_fact:
4661
cluster_nodes: "{{ cluster_group.output_value | map(attribute='nodes') | list }}"
@@ -54,4 +69,4 @@
5469
timeout: "{{ cluster_ssh_timeout }}"
5570
with_flattened:
5671
- "{{ cluster_nodes }}"
57-
when: cluster_state == 'present'
72+
when: cluster_state != 'absent'

templates/cluster_inventory.j2

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,22 @@ cluster
1111
{% for output in cluster_stack.stack.outputs %}
1212
{% if output.output_key == "cluster_group" %}
1313
[cluster:children]
14-
{% for group_data in output.output_value %}
15-
{{ cluster_stack.stack.stack_name}}_{{ group_data.group }}
14+
{% for group_data in cluster_group.output_value %}
15+
{{ cluster_name }}_{{ group_data.group }}
1616
{% endfor %}
1717

18-
{% for group_data in output.output_value %}
19-
[{{ cluster_stack.stack.stack_name}}_{{ group_data.group }}]
18+
{% for group_data in cluster_group.output_value %}
19+
[{{ cluster_name }}_{{ group_data.group }}]
2020
{% for node_data in group_data.nodes %}
21-
{{node_data.name}} ansible_host={{ node_data.ip }} ansible_user={{ cluster_params.cluster_groups | selectattr("name", "equalto", group_data.group) | map(attribute='user') | join }}
21+
{{ node_data.name }} ansible_host={{ node_data.ip }} ansible_user={{ cluster_params.cluster_groups | selectattr("name", "equalto", group_data.group) | map(attribute='user') | join }}
2222
{% endfor %}
2323

24-
{% endfor %}
25-
{% endif %}
2624
{% endfor %}
2725
# Specific roles for cluster deployment assignments
2826
{% for role in cluster_roles %}
2927
[cluster_{{ role.name }}:children]
3028
{% for group in role.groups %}
31-
{{ cluster_stack.stack.stack_name}}_{{ group.name }}
29+
{{ cluster_name }}_{{ group.name }}
3230
{% endfor %}
3331

3432
{% endfor %}

0 commit comments

Comments
 (0)