Remove an item from a list of dict and splitting stdout_lines - ansible

Im trying to eliminate an item from a list of dicts, the one that have "No results were found":
"validar": [
{
"LPARNAME": [
"No results were found."
]
},
{
"LPARNAME": [
"server1",
"server2",
"server4"
]
},
{
"LPARNAME": [
"server3",
"server5",
"server7"
]
}
]
}
Im trying using rejectattr but is not working:
- name: clean
set_fact:
validar_: "{{ validar | rejectattr('LPARNAME', 'match', 'No results were found.') | list }}"
Also im trying to split stdout_lines of:
"output": [
{
"item": {
"Name": "CONT1"
},
"stdout_lines": [
"server1,1.05",
"server2,0.25"
]
},
{
"item": {
"Name": "CONT2"
},
"stdout_lines": [
"server3,0.05",
"server4,0.35"
]
},
{
"item": {
"Name": "PRD1"
},
"stdout_lines": [
"server4,1.15"
]
}
]
}
I got an error: {"msg": "'list object' has no attribute 'stdout_lines'"}
- name: list
set_fact:
lpar_proc: "{{ lpar_proc | default([]) + [{ 'lpar': item.[0] , 'proc': item[1] }]"
with_items: [ "{{ output.stdout_lines.split(',')[0]}}", "{{ output.stdout_lines.split(',')[1]" ]
the output expected:
Name: CONT1
vm: server1
proc: 1.05
Name: CONT1
vm: server2
proc: 0.25
... etc

you have list of list, so you cant use rejectattr as you do:
- name: testplaybook jinja2
hosts: localhost
gather_facts: no
vars:
validar:
- LPARNAME:
- No results were found.
- LPARNAME:
- server1
- server2
- server4
- LPARNAME:
- server3
- server5
- server7
tasks:
- name: clean
set_fact:
result: "{{ result | default([]) + [item] }}"
loop: "{{ validar }}"
when: '"No results were found." not in item.LPARNAME'
- name: set in validar
set_fact:
validar: "{{ result }}"
- name: debug
debug:
var: validar
result:
{
"validar": [
{
"LPARNAME": [
"server1",
"server2",
"server4"
]
},
{
"LPARNAME": [
"server3",
"server5",
"server7"
]
}
]
}
for the other problem:
- name: testplaybook
hosts: localhost
gather_facts: no
vars:
output:
- item:
Name: CONT1
stdout_lines:
- server1,1.05
- server2,0.25
- item:
Name: CONT2
stdout_lines:
- server3,0.05
- server4,0.35
- item:
Name: PRD1
stdout_lines:
- server4,1.15
tasks:
- name: m1
set_fact:
m1: "{{ m1 | d([]) + _p }}"
loop: "{{ output }}"
vars:
_p: "{{ [item['item']]| product(item.stdout_lines) }}"
- name: m2
set_fact:
result: "{{ result | d([]) + [_server] }}"
loop: "{{ m1 }}"
vars:
_vm: "{{ item.1.split(',')|first }}"
_proc: "{{ item.1.split(',')|last }}"
_server: "{{ item.0 | combine({'vm': _vm, 'proc': _proc}) }}"
- name: debug
debug:
var: result
result:
ok: [localhost] => {
"result": [
{
"Name": "CONT1",
"proc": "1.05",
"vm": "server1"
},
{
"Name": "CONT1",
"proc": "0.25",
"vm": "server2"
},
{
"Name": "CONT2",
"proc": "0.05",
"vm": "server3"
},
{
"Name": "CONT2",
"proc": "0.35",
"vm": "server4"
},
{
"Name": "PRD1",
"proc": "1.15",
"vm": "server4"
}
]
}

Given the data
validar:
- LPARNAME:
- No results were found.
- LPARNAME:
- server1
- server2
- server4
- LPARNAME:
- server3
- server5
- server7
The value of the attribute LPARNAME is a list. Use test contains instead of match. See Testing if a list contains a value
validar_: "{{ validar|
rejectattr('LPARNAME', 'contains', 'No results were found.') }}"
expands to
validar_:
- LPARNAME:
- server1
- server2
- server4
- LPARNAME:
- server3
- server5
- server7
Given the data
output:
- item:
Name: CONT1
stdout_lines:
- server1,1.05
- server2,0.25
- item:
Name: CONT2
stdout_lines:
- server3,0.05
- server4,0.35
- item:
Name: PRD1
stdout_lines:
- server4,1.1
Iterate with_subelements
- set_fact:
lpar_proc: "{{ lpar_proc|default([]) +
[item.0.item|
combine(dict(['vm', 'proc']|
zip(item.1.split(','))))] }}"
with_subelements:
- "{{ output }}"
- stdout_lines
gives
lpar_proc:
- {Name: CONT1, proc: '1.05', vm: server1}
- {Name: CONT1, proc: '0.25', vm: server2}
- {Name: CONT2, proc: '0.05', vm: server3}
- {Name: CONT2, proc: '0.35', vm: server4}
- {Name: PRD1, proc: '1.1', vm: server4}

Related

Register return values from tasks_from when looping over include_role

I'd like to build an array of ticket numbers that are created by the create_srq.yaml task in role servicenow. Is it possible to do this when looping over an include_role?
roles/request_signed_certificate/tasks/main.yaml
- name: Create SNOW Records for Certificate Request
include_role:
name: servicenow
tasks_from: create_srq.yaml
register: result
loop: "{{ spreadsheet }}"
loop_control:
loop_var: csr
vars:
short_description: CSR for {{ csr.certname }}-{{ csr.env }}
attachment: "{{ cert_path }}/{{ csr.certname }}-{{ csr.env }}.csr"
- name: debug
debug:
var: result
roles/servicenow/tasks/create_srq.yaml
- name: Create a SRQ
snow_record:
state: present
table: u_request
username: "{{ snow_username }}"
password: "{{ snow_password }}"
instance: "{{ servicenow_instance }}"
data:
short_description: "{{ short_description }}"
attachment: "{{ attachment }}"
register: srq
- name: Attach file to {{ srq.record.number }}
snow_record:
state: present
table: u_request
username: "{{ snow_username }}"
password: "{{ snow_password }}"
instance: "{{ servicenow_instance }}"
number: "{{ srq.record.number }}"
attachment: "{{ attachment }}"
When running the playbook:
---
- hosts: "{{ hosts_list }}"
connection: local
gather_facts: false
vars:
cert_path: "/tmp/certs"
cert_version: "2023"
pre_tasks:
- name: Create facts from csv
csv_to_facts:
src: "file.csv"
delegate_to: localhost
run_once: true
roles:
- role: request_signed_certificate
The result does not include the registered srq variable from create_srq.yaml:
TASK [request_signed_certificate : debug] **************************************************************
ok: [host.example.com] => {
"result": {
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "csr",
"csr": {
"certname": "example_one",
"common_name": "domain_one.example.com",
"dns1": "domain_two.example.com",
"env": "development",
},
"include_args": {
"name": "servicenow",
"tasks_from": "create_srq.yaml"
}
},
{
"ansible_loop_var": "csr",
"csr": {
"certname": "example_two",
"common_name": "domain_123.example.com",
"dns1": "domain_456.example.com",
"env": "test",
},
"include_args": {
"name": "servicenow",
"tasks_from": "create_srq.yaml"
}
}
]
}
}
I was able to do this by appending results to a list. While this works, it wasn't exactly what I was after as I would have preferred to install a reusable role to create the srqs.
Adding this bit to the bottom of create_srq.yaml gave me what I was looking for:
- name: output csv entry + ticket number
set_fact:
csv: "{{ csv | default({}) | combine ( { item.key : item.value } ) | combine( { 'ticket': srq.record.number } ) }}"
loop: "{{ csr | dict2items }}"
# Create list of dictionaries to track
# also, a list is required for the to_csv plugin
- name: create list of dicts
set_fact:
contents: "{{ contents | default([]) + [ csv ] }}"

I want to sum value from dict

I have the following output:
"vmoff2": [
{
"sec_sys": "CONT1",
"vm_na": {
"vm1": "Running",
"vm2": "Running",
"vm3": "Not Activated",
"vm4": "Not Activated",
"vm5": "Running",
"vm6": "Not Activated"
}
},
{
"sec_sys": "CONT2",
"vm_na": {
"vm1": "Not Activated",
"vm2": "Not Activated",
"vm3": "Running",
"vm4": "Running",
"vm5": "Not Activated",
"vm6": "Running"
}
}
]
---------------
"vmprof2": [
{
"proc": {
"vm1": "0.5",
"vm2": "0.7",
"vm3": "1.0",
"vm4": "0.5",
"vm5": "0.5",
"vm6": "0.5"
},
"sec_sys": "CONT1"
},
{
"proc": {
"vm1": "1.0",
"vm2": "0.7",
"vm3": "1.0",
"vm4": "0.7",
"vm5": "0.5",
"vm6": "0.7"
},
"sec_sys": "CONT2"
}
]
And want to sum the proc of the not activated VM, comparing with wmoff2, the out expected will be:
proc_steal:
- server: CONT1
proc: 1.7
- server: CONT2
proc: 2.4
I tried the following:
- name: create proc_steal list
set_fact:
proc_steal: >-
{{ proc_steal | default([])
+ [{
'sec_sys': item.sec_sys,
'proc': vmprof2
| selectattr('sec_sys', '==', item.sec_sys)
| selectattr('proc', '==', item.vm_na)
| sum(attribute='value')
| float
}]
}}
loop: "{{ vmoff2 }}"
loop_control:
label: "{{ item.sec_sys }}"
but i get:
"proc_steal": [
{
"proc": 0.0,
"sec_sys": "CONT1"
},
{
"proc": 0.0,
"sec_sys": "CONT2"
}
]
}
Is not making the sum, maybe because is not float, but when i tried to put float i got an error "float object is not iterable", so how can i sum that values?
For example, to sum all VM
- set_fact:
proc_steal: "{{ dict(_serv|zip(_proc)) }}"
vars:
_serv: "{{ vmprof2|json_query('[].sec_sys') }}"
_proc: "{{ vmprof2|json_query('[].proc.*')|
map('map', 'float')|
map('sum')|
map('round', 2)|list }}"
gives the dictionary
proc_steal:
CONT1: 3.7
CONT2: 4.6
You can convert it to a list if you want to
- set_fact:
proc_steal: "{{ dict(_serv|zip(_proc))|
dict2items(key_name='server', value_name='proc') }}"
vars:
_serv: "{{ vmprof2|json_query('[].sec_sys') }}"
_proc: "{{ vmprof2|json_query('[].proc.*')|
map('map', 'float')|
map('sum')|
map('round', 2)|list }}"
gives
proc_steal:
- proc: 3.7
server: CONT1
- proc: 4.6
server: CONT2
To sum the Not Activated VM only, create a dictionary first
- set_fact:
vm_na: "{{ dict(_serv|zip(_vmna)) }}"
vars:
_serv: "{{ vmoff2|json_query('[].sec_sys') }}"
_vmna: "{{ vmoff2|json_query('[].vm_na')|
map('dict2items')|
map('selectattr', 'value', 'eq', 'Not Activated')|
map('map', attribute='key') }}"
gives
vm_na:
CONT1:
- vm3
- vm4
- vm6
CONT2:
- vm1
- vm2
- vm5
Then use it to select the machines
- set_fact:
proc_steal: "{{ proc_steal|d({})|combine({item.sec_sys: _proc|float}) }}"
loop: "{{ vmprof2 }}"
vars:
_proc: "{{ vm_na[item.sec_sys]|map('extract', item.proc)|
map('float')|
sum|
round(2) }}"
gives the dictionary
proc_steal:
CONT1: 2.0
CONT2: 2.2
, or
- set_fact:
proc_steal: "{{ proc_steal|d([]) + [{'server': item.sec_sys,
'proc': _proc|float}] }}"
loop: "{{ vmprof2 }}"
vars:
_proc: "{{ vm_na[item.sec_sys]|map('extract', item.proc)|
map('float')|
sum|
round(2) }}"
gives the list if you want to
proc_steal:
- proc: 2.0
server: CONT1
- proc: 2.2
server: CONT2

ansible create a new dict and add to list

my playbook always show me just last value , looks like the script is overwrite.
From json file I need extract some value, create dictionary and put it to the list.
My json file .
{
"rade": [
{
"apiRawValues": {
"verificationStatus": "signature-verified"
},
"deviceReference": {
"name": "bigip02"
},
"port": "Ir_HTTP_HTTPs"
},
{
"apiRawValues": {
"verificationStatus": "signature-verified"
},
"deviceReference": {
"name": "bigip01"
},
"port": "Ir_HTTP_HTTPs"
}
]
}
and my playbook look like
---
- hosts: localhost
connection: local
gather_facts: false
vars:
cert1: {}
vars_files:
tasks:
- name : deploy json file AS3 to F5
set_fact:
json_file: "{{ lookup('file', 'parse2.json') }}"
- name: create dic and create list
set_fact:
cert1: "{{ cert1 | d({}) | combine({ 'device': item['deviceReference']['name']}, { 'port': item.port}, recursive=True) }}"
loop: "{{ json_file['rade'] }}"
- name: debug4
debug:
msg: "{{ cert1 }}"
the result is
ok: [localhost] => {
"msg": {
"device": "bigip01",
"port": "Ir_HTTP_HTTPs"
}
}
why it just show me last value ?
I need list of device and port.
thank you for help
The filter json_query makes it simpler e.g.
- debug:
msg: "{{ json_file.rade|
json_query('[].{device: deviceReference.name, port: port}') }}"
gives
msg:
- device: bigip02
port: Ir_HTTP_HTTPs
- device: bigip01
port: Ir_HTTP_HTTPs
If the names of the devices are unique you can create a dictionary, e.g.
- debug:
msg: "{{ dict(_keys|zip(_vals)) }}"
vars:
_keys: "{{ json_file.rade|map(attribute='deviceReference.name')|list }}"
_vals: "{{ json_file.rade|map(attribute='port')|list }}"
gives
msg:
bigip01: Ir_HTTP_HTTPs
bigip02: Ir_HTTP_HTTPs
I have found out solution
- name: create dic and create list
set_fact:
cert1: "{{ cert1 | default([]) + [{ 'device' : item['deviceReference']['name'], 'irule' : item.port }] }}"
loop: "{{ json_file['rade'] }}"
- name: debug4
debug:
msg: "{{ cert1 }}"

Filter debug msg

I am using Ansible 2.9.13 and I have this playbook:
---
- hosts: localhost
connection: local
vars:
ansible_python_interpreter: /usr/bin/env python3
vars_files:
- vars.yml
tasks:
- name: Get Tags from given VM Name
vmware_vm_info:
validate_certs: no
hostname: '{{ vcenter_server }}'
username: '{{ vcenter_user }}'
password: '{{ vcenter_pass }}'
folder: '{{ provision_folder }}'
delegate_to: localhost
register: vm_info
- debug:
msg: "{{ vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='C97A1612171478']"
When I run it I am getting this output:
ok: [localhost] => {
"msg": [
{
"attributes": {},
"cluster": "xxx01",
"esxi_hostname": "xxxx",
"guest_fullname": "Microsoft Windows 10 (64-bit)",
"guest_name": "C97A1612171478",
"ip_address": "10.x.x.x",
"mac_address": [
"0x:x:x:x:xd:x"
],
"power_state": "poweredOn",
"tags": [],
"uuid": "420xxaf-xxx-xe2-9xe-a5xxxxxa3c",
"vm_network": {
"0x:x:x:xa:x:x": {
"ipv4": [
"169.x.x.x"
],
"ipv6": [
"x::x:x:x:xc"
]
},
"x:x:x:x:x0:x1": {
"ipv4": [
"169.x.x.x"
],
"ipv6": [
"x::x7:xf:x:x"
]
},
"0x:5x:x:x:ax:x": {
"ipv4": [
"10.x.x.x"
],
"ipv6": [
"x::1xx:x:8xx:x"
]
}
}
}
]
}
How can I filter the output to make it show only the "ip_address": "10.x.x.x".
In the end only the 10.x.x.x.
I have tried some ways adding the key ip_address in the message code but all of them gave me an error.
I can filter the msg using Python but if there's a way to get it using Ansible I would like to know how.
If you want to get this information without a loop:
If you need an object as a result:
- debug:
msg: "{{ vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='C97A1612171478'] | [0].{ip_address: ip_address}"
will yield
{
"ip_address": "10.x.x.x"
}
If you need a string as a result:
- debug:
msg: "{{ vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='C97A1612171478'] | [0].ip_address"
will yield
"10.x.x.x"
I can't test this properly, but try to fiddle around with the following code:
- debug:
msg: "{{ item.ip_address | json_query(query) }}"
loop: "{{ vm_info.virtual_machines }}"
vars:
query: "[?guest_name=='C97A1612171478']"

Ansible set_fact is overwriting the items

Here is my main.yml
---
- name: Gathering VCenter facts
vmware_vm_info:
hostname: "{{ vcenter_server }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
validate_certs: false
register: vcenter_facts
delegate_to: localhost
- debug:
var: vcenter_facts.virtual_machines
- name: Find all test-vms to run IO
set_fact:
vm_ip: "{{ item.ip_address }}"
loop: "{{ vcenter_facts.virtual_machines }}"
when: item.guest_name is regex("test_vm*")
- name: print vm_ip variable value
debug:
var: vm_ip
- name: Mount 16TB dropbox in each test vm
shell: mount-16tb-dropbox.sh
args:
chdir: /usr/local/bin/
with_items: "{{ vm_ip }}"
And here is the recap:
ok: [localhost] => {
"vcenter_facts.virtual_machines": [
{
"attributes": {},
"cluster": "Compute Cluster",
"esxi_hostname": "100.80.90.179",
"guest_fullname": "CentOS 7 (64-bit)",
"guest_name": "test_vm4",
"ip_address": "192.168.202.13",
"mac_address": [
"00:50:56:9d:d2:99"
],
"power_state": "poweredOn",
"tags": [],
"uuid": "421d7b54-1359-14e8-3ec4-74b568cb96d2",
"vm_network": {
"00:50:56:9d:d2:99": {
"ipv4": [
"192.168.202.13"
],
"ipv6": [
"fe80::44f6:a395:cde3:4dd1",
"fe80::a357:a163:e44f:2086",
"fe80::cd0c:e7d7:1356:2830"
]
}
}
},
{
"attributes": {},
"cluster": "Compute Cluster",
"esxi_hostname": "100.80.90.178",
"guest_fullname": "CentOS 7 (64-bit)",
"guest_name": "test_vm3",
"ip_address": "192.168.202.12",
"mac_address": [
"00:50:56:9d:a9:e8"
],
"power_state": "poweredOn",
"tags": [],
"uuid": "421d9239-0980-80c1-bca4-540efd726452",
"vm_network": {
"00:50:56:9d:a9:e8": {
"ipv4": [
"192.168.202.12"
],
"ipv6": [
"fe80::cd0c:e7d7:1356:2830"
]
}
}
},
{
"attributes": {},
"cluster": "Compute Cluster",
"esxi_hostname": "100.80.90.178",
"guest_fullname": "CentOS 7 (64-bit)",
"guest_name": "Test_Automation_CentOS8_Linux_VM",
"ip_address": "192.168.202.6",
"mac_address": [
"00:50:56:9d:13:14"
],
"power_state": "poweredOn",
"tags": [],
"uuid": "421d53ba-4824-57e4-06fd-fba0f2b1dbea",
"vm_network": {
"00:50:56:9d:13:14": {
"ipv4": [
"192.168.202.6"
],
"ipv6": [
"fe80::cd0c:e7d7:1356:2830",
"fe80::44f6:a395:cde3:4dd1"
]
}
}
},
{
"attributes": {},
"cluster": "Compute Cluster",
"esxi_hostname": "100.80.90.180",
"guest_fullname": "CentOS 7 (64-bit)",
"guest_name": "test_vm5",
"ip_address": "192.168.202.14",
"mac_address": [
"00:50:56:9d:85:b6"
],
"power_state": "poweredOn",
"tags": [],
"uuid": "421d6855-e60e-cd80-f113-39f11927d63b",
"vm_network": {
"00:50:56:9d:85:b6": {
"ipv4": [
"192.168.202.14"
],
"ipv6": [
"fe80::44f6:a395:cde3:4dd1",
"fe80::cd0c:e7d7:1356:2830",
"fe80::a357:a163:e44f:2086"
]
}
}
}
]
}
I am not able to loop through all the ip_address variable (i.e. 192.168.202.12, 192.168.202.13, 192.168.202.14).
It just reads the last item (i.e. 192.168.202.14).
What am I possibly doing wrong with set_fact that it is not reading all the variable and performing the set of tasks that follows?
An alternate solution using json_query
---
- name: Gathering VCenter facts
vmware_vm_info:
hostname: "{{ vcenter_server }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
validate_certs: false
register: vcenter_facts
delegate_to: localhost
- name: Mount 16TB dropbox in each test vm
shell: mount-16tb-dropbox.sh
args:
chdir: /usr/local/bin/
vars:
query: >-
[?contains("guest_name", 'test_vm')].ip_address[]
with_items: "{{ vcenter_facts.virtual_machines | to_json | from_json | json_query(query) | list }}"
Note: to_json | from_json is a workaround for a bug between ansible and jmespath so that all values can be converted to real strings and can be used with the jmespath contains function.
This should give all the IP. You correctly assumed where the code may need correction. In the code, vm_ip variable was overwritten by each loop and the last IP remained. What you need is a list and then append each IP to the list.
- set_fact:
vm_ip: "{{ vm_ip | default([]) + [item.ip_address] }}"
loop: "{{ vcenter_facts.virtual_machines | flatten }}"
when: item.guest_name is regex("test_vm*")
- debug:
var: vm_ip
Alternative solution using Jinja2 filters.
- set_fact:
vm_ip: >-
{{ vcenter_facts.virtual_machines | flatten
| rejectattr('guest_name', 'match', '^(?!test_vm).*')
| map(attribute='ip_address') | list }}

Resources