Reputation: 35
Am currently getting the device count from using debug var stmt. I am trying to get only the value but output does comes along with variable.
- name: get number of device on each rack blades
#set_fact:
#devices_count: "{{ result.ansible_facts.blades.{{item}}.device|length }}"
debug: var=result.ansible_facts.blades.{{item}}.device|length
register: devices_count
with_sequence:
- start=0 end="{{ blades_count|int - 1 }}"
Output currently getting from the above code :
TASK [get number of device on each rack blades] *************************************************************************************************
ok: [localhost] => (item=0) => {
"item": "0",
"result.ansible_facts.blades.0.device|length": "10"
}
ok: [localhost] => (item=1) => {
"item": "1",
"result.ansible_facts.blades.1.device|length": "8"
}
ok: [localhost] => (item=2) => {
"item": "2",
"result.ansible_facts.blades.2.device|length": "4"
}
How can I get only value part IE, 10, 8, 4.
Ss there a way we can filter or get .value part or use some regex ?
Any help please..
Upvotes: 1
Views: 3525
Reputation: 44615
var
option of the debug
module is expecting a string which is the name of a variable.
You are feeding in that option a jinja2 expression (I'm even surprised you get a result at all) that should be passed to a msg
option with jinja2 expression markers (double curly braces). The item
expansion will need to be modified to work.
Moreover, registering the result of a debug task has no interest (debug the registered var in a next task to see for yourself).
Last, I strongly recommend you adopt the modern full yaml syntax for all your tasks rather than using/mixing with the key=value
old shortcut syntax for modules.
The following should get you to the expected result
- name: debug my values
debug:
msg: "{{ result.ansible_facts.blades[item].device | length }}"
with_sequence: start=0 end="{{ blades_count | int - 1 }}"
You can even adopt the new loop
syntax and its with_sequence
replacement
- name: debug my values
debug:
msg: "{{ result.ansible_facts.blades[item].device | length }}"
loop: "{{ range(0, blades_count | int) | list }}"
Edit: Following your comment, I'm not really sure of what you wish to store and loop over. Example below is to store the raw values in a simple list (not tested without a sample data provided). If this is not what you expect, please ask a new question for this specific problem and provide more details about your actual data structure and the expected result.
- name: Create a list with device number length
vars:
current_number: "{{ result.ansible_facts.blades[item].device | length }}"
set_fact:
device_numbers: "{{ device_numbers | default([]) + [current_number] }}"
loop: "{{ range(0, blades_count | int) | list }}"
- name: Show device_numbers list
debug:
var: device_numbers
Upvotes: 1
Reputation: 68004
Q: " ... getting the device count ... how can I get only value part ie, 10, 8, 4?"
A: Given the dictionary below for testing the number of devices in each blade (3, 4, 5)
blades:
1: [devA, devB, devC]
2: [devA, devB, devC, devD]
3: [devA, devB, devC, devD, devE]
the tasks
- set_fact:
devices: "{{ blades|dict2items|
json_query('[].value')|
map('length')|
list }}"
- debug:
var: devices
give
"devices": [
3,
4,
5
]
Details
dict2items converts the dictionary to a list
[
{
"key": 1,
"value": [
"devA",
"devB",
"devC"
]
},
{
"key": 2,
"value": [
"devA",
"devB",
"devC",
"devD"
]
},
{
"key": 3,
"value": [
"devA",
"devB",
"devC",
"devD",
"devE"
]
}
]
json_query selects the list of values
[
[
"devA",
"devB",
"devC"
],
[
"devA",
"devB",
"devC",
"devD"
],
[
"devA",
"devB",
"devC",
"devD",
"devE"
]
]
and map applies the filter length to each element of the list
[
3,
4,
5
]
Upvotes: 0