Skip to content

Commit 16ff7a3

Browse files
committed
Refactor values-prep role to align with Ansible best practices
- Use fully qualified module names (ansible.builtin.*) throughout - Convert boolean values from yes/no to true/false - Improve task names with proper capitalization and clarity - Replace shell with command for simple oc commands - Add explicit cmd parameter to all command/shell tasks - Fix typos: 'woker' -> 'worker', 'accros' -> 'across' - Fix grammar: 'run lsblk on each nodes' -> 'Run lsblk on each node' - Add become flag where appropriate (iptables commands) Optimize jq query in find-node-disks.yml: - Combine multiple chained select() statements into a single select() with and conditions for better performance - Validated query produces identical results: * Tested with local lsblk output containing disks with/without /boot partitions * Confirmed correct filtering of disks with /boot mounted children * Verified both original and optimized versions produce same output This ensures consistency, better performance, and alignment with Ansible best practices for module usage and task naming conventions. AI Model: Generated using Auto (agent router designed by Cursor)
1 parent 7d0e0ad commit 16ff7a3

File tree

3 files changed

+56
-54
lines changed

3 files changed

+56
-54
lines changed
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
---
2-
- name: run lsblk on each nodes
3-
vars:
2+
- name: Run lsblk on each node
3+
vars:
44
ip: "{{ lookup('vars', 'node_' ~ item ~ '_ip') }}"
5-
shell: |
6-
ssh core@{{ ip }} \
7-
"lsblk -J -b -o NAME,TYPE,SIZE,MOUNTPOINT | jq -r '
8-
.blockdevices[]
9-
| select(.type == \"disk\")
10-
| select(.size > 0)
11-
| select((.children // []) | all(.mountpoint != \"/boot\"))
12-
| \"/dev/\" + .name'"
5+
ansible.builtin.shell:
6+
cmd: >-
7+
ssh core@{{ ip }} "lsblk -J -b -o NAME,TYPE,SIZE,MOUNTPOINT |
8+
jq -r '.blockdevices[] | select(.size > 0 and .type == \"disk\") |
9+
select((.children // []) | all(.mountpoint != \"/boot\")) | \"/dev/\" + .name'"
1310
register: blk_outputs
1411
loop: "{{ range(0,3) | list }}"
1512

1613
- name: Extract device lists
17-
set_fact:
14+
ansible.builtin.set_fact:
1815
node_disks_list: "{{ blk_outputs.results | map(attribute='stdout_lines') | list }}"
1916

20-
- name: Find common devices accross nodes
21-
set_fact:
17+
- name: Find common devices across nodes
18+
ansible.builtin.set_fact:
2219
common_disks: "{{ node_disks_list[0] | intersect(node_disks_list[1]) | intersect(node_disks_list[2]) }}"
2320

24-
- name: common disks
25-
debug:
21+
- name: Display common disks
22+
ansible.builtin.debug:
2623
msg: "{{ common_disks }}"
Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,85 @@
11
---
2-
- name: get woker node names
3-
shell: oc get nodes -o jsonpath='{.items[*].metadata.name}'
2+
- name: Get worker node names
3+
ansible.builtin.command:
4+
cmd: oc get nodes -o jsonpath='{.items[*].metadata.name}'
45
register: node_names
56

6-
- name: set node facts (node_*)
7-
set_fact:
7+
- name: Set node facts (node_*)
8+
ansible.builtin.set_fact:
89
"{{ item_name }}": "{{ item }}"
910
loop: "{{ node_names.stdout.split() }}"
1011
loop_control:
1112
index_var: node_index
1213
vars:
1314
item_name: "node_{{ node_index }}"
1415

15-
- name: get the second node macs (2,3 and 4th macs)
16-
set_fact:
16+
- name: Get the second node MACs (2nd, 3rd and 4th MACs)
17+
ansible.builtin.set_fact:
1718
macs: "{{ ocp_inventory.json.nodes[1].mac[1:4] }}"
1819

19-
- name: get the ip of the first node
20-
command: >
21-
oc get nodes -o jsonpath="{range .items[*]}{.status.addresses[?(@.type=='InternalIP')].address}{'\n'}{end}"
22-
register:
23-
nodes_ip
20+
- name: Get the IP of the first node
21+
ansible.builtin.command:
22+
cmd: oc get nodes -o jsonpath="{range .items[*]}{.status.addresses[?(@.type=='InternalIP')].address}{'\n'}{end}"
23+
register: nodes_ip
2424

25-
- name: set node ip fact
26-
set_fact:
25+
- name: Set node IP fact
26+
ansible.builtin.set_fact:
2727
"{{ item_name }}": "{{ item }}"
2828
loop: "{{ nodes_ip.stdout.split() }}"
2929
loop_control:
3030
index_var: node_index
3131
vars:
32-
item_name: "node_{{ node_index }}_ip"
32+
item_name: "node_{{ node_index }}_ip"
3333

34-
- name: fix and setup ssh
35-
include_tasks: setup-ssh.yml
34+
- name: Fix and setup SSH
35+
ansible.builtin.include_tasks:
36+
file: setup-ssh.yml
3637
loop: "{{ range(0, 3) | list }}"
3738

38-
- name: get the interface names
39-
include_tasks: get_iface.yml
39+
- name: Get the interface names
40+
ansible.builtin.include_tasks:
41+
file: get_iface.yml
4042
loop: "{{ macs | zip(range(macs | length)) | map('list') | list }}"
4143
loop_control:
4244
loop_var: item
4345

44-
- name: set the ctlplane gateway ip
45-
shell:
46-
ip addr add 172.16.0.1/16 dev {{ iface_0 }}
47-
become: yes
48-
ignore_errors: yes
46+
- name: Set the ctlplane gateway IP
47+
ansible.builtin.command:
48+
cmd: ip addr add 172.16.0.1/16 dev {{ iface_0 }}
49+
become: true
50+
ignore_errors: true
4951

50-
- name: set masquerade for the jump host
51-
shell: |
52-
iptables -t nat -A POSTROUTING -s 172.16.0.0/16 -o {{ iface_0 }} -j MASQUERADE
52+
- name: Set masquerade for the jump host
53+
ansible.builtin.command:
54+
cmd: iptables -t nat -A POSTROUTING -s 172.16.0.0/16 -o {{ iface_0 }} -j MASQUERADE
55+
become: true
5356

54-
- name: get the common disk accros nodes
55-
include_tasks: find-node-disks.yml
57+
- name: Get the common disk across nodes
58+
ansible.builtin.include_tasks:
59+
file: find-node-disks.yml
5660

5761
- name: Render nncp.yaml
58-
template:
62+
ansible.builtin.template:
5963
src: nncp_values.yaml.j2
6064
dest: "{{ dt_path }}/examples/dt/perfscale/scalelab/networking/nncp/values.yaml"
6165

6266
- name: Render service_values.yaml
63-
template:
67+
ansible.builtin.template:
6468
src: service_values.yaml.j2
6569
dest: "{{ dt_path }}/examples/dt/perfscale/scalelab/service-values.yaml"
6670

6771
- name: Render service_values_ceph.yaml
68-
template:
72+
ansible.builtin.template:
6973
src: service_values_ceph.yaml.j2
7074
dest: "{{ dt_path }}/examples/dt/perfscale/scalelab/service-values-ceph.yaml"
7175
when: ceph_backend | default(false) | bool
7276

7377
- name: Render kustomization.yaml for controlplane
74-
template:
78+
ansible.builtin.template:
7579
src: kustomization.yaml.j2
7680
dest: "{{ dt_path }}/examples/dt/perfscale/scalelab/kustomization.yaml"
7781

7882
- name: Render lvms value
79-
template:
83+
ansible.builtin.template:
8084
src: lvms-values.yaml.j2
8185
dest: "{{ dt_path }}/examples/dt/perfscale/scalelab/lvms/lvm-cluster/values.yaml"
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
2-
- name: remove bad known_host entry if any
3-
shell:
4-
ssh-keygen -R {{ lookup('vars', 'node_' ~ item ~ '_ip') }}
5-
ignore_errors: yes
2+
- name: Remove bad known_host entry if any
3+
ansible.builtin.command:
4+
cmd: ssh-keygen -R {{ lookup('vars', 'node_' ~ item ~ '_ip') }}
5+
ignore_errors: true
66

77
- name: Add node to known hosts
8-
shell: ssh-keyscan -H {{ lookup('vars', 'node_' ~ item ~ '_ip') }} >> ~/.ssh/known_hosts
9-
ignore_errors: yes
8+
ansible.builtin.shell:
9+
cmd: ssh-keyscan -H {{ lookup('vars', 'node_' ~ item ~ '_ip') }} >> ~/.ssh/known_hosts
10+
ignore_errors: true

0 commit comments

Comments
 (0)