Filter debug msg - ansible

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']"

Related

Remove an item from a list of dict and splitting stdout_lines

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}

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 }}"

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 }}

How parse a list of dictionary and out the values in Ansible?

I've the following json output. It is a list of dictionary. I just want to parse it and output its value.
I've tried the following play but without success...
"output1": {
"changed": false,
"msg": "All items completed",
"results": [
{
"item": [
{
"Device_Name": "SW1"
},
{
"Interface": "GigabitEthernet1/0/7"
}
]
}
]
}
- name: Display output1...2
debug:
msg: "{{ item.1.Device_Name }};{{ item.1.Interface }}"
with_subelements:
- "{{ output1.results }}"
- item
The error was ""msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'int_name'\n\n"
Try below - I have not tested it though.
- name: Display output1...2
debug:
msg: "{{ item|first }} : {{ item[item|first] }}"
with_items:
- "{{ output1.results[0].item }}"
Quick and dirty
The tasks below give quickly what is requested
- set_fact:
Device_Name: "{{ output1.results[0]['item']|
map(attribute='Device_Name')|
select('defined')|
list|first }}"
Interface: "{{ output1.results[0]['item']|
map(attribute='Interface')|
select('defined')|
list|first }}"
Details
This is a typical example of bad data structure. The task below
- debug:
msg: "{{ output1.results|
map('dict2items')|list|flatten|
json_query('[?key==`item`].value')|flatten }}"
gives item, which is a list and must be iterated again,
"msg": [
{
"Device_Name": "SW1"
},
{
"Interface": "GigabitEthernet1/0/7"
}
]
instead of a dictionary, which could be easily referenced.
"msg": [
{
"Device_Name": "SW1",
"Interface": "GigabitEthernet1/0/7"
}
]
As a result of such filtering the tasks below
- set_fact:
Device_Name: "{{ output1.results|
map('dict2items')|list|flatten|
json_query('[?key==`item`].value')|flatten|
map('dict2items')|list|
json_query('[*][?key==`Device_Name`].value')|flatten }}"
- set_fact:
Interface: "{{ output1.results|
map('dict2items')|list|flatten|
json_query('[?key==`item`].value')|flatten|
map('dict2items')|list|
json_query('[*][?key==`Interface`].value')|flatten }}"
- debug:
var: Device_Name
- debug:
var: Interface
give
"Device_Name": [
"SW1"
]
"Interface": [
"GigabitEthernet1/0/7"
]

Ansible parse JSON output

I am trying to parse the Ansible output the print a value
- name: Creating a new instance
os_server:
state: present
auth:
auth_url: "{{ auth_url }}"
username: "{{ username }}"
password: "{{ password }}"
project_name: "{{ project_name }}"
name: "{{ item.hostname }}"
image: "{{ item.image }}"
nics: "{{ nics }}"
with_items: "{{ servers }}"
register: "os"
Output:
"server": {
"OS-DCF:diskConfig": "MANUAL",
"OS-EXT-AZ:availability_zone": "zoneA",
"OS-EXT-STS:power_state": 1,
"OS-EXT-STS:task_state": null,
"OS-EXT-STS:vm_state": "active",
"OS-SRV-USG:launched_at": "2018-04-01T18:53:16.000000",
"OS-SRV-USG:terminated_at": null,
"accessIPv4": "10.190.230.23",
"accessIPv6": "",
"addresses": {
"provider_corenet_bif_757": [
{
"OS-EXT-IPS-MAC:mac_addr": "fa:1:3:3:5e:6a",
"OS-EXT-IPS:type": "fixed",
"addr": "10.19.23.23",
"version": 4
}
],
"provider_nmnet_bif_912": [
{
"OS-EXT-IPS-MAC:mac_addr": "fa:1:3:39:b:57",
"OS-EXT-IPS:type": "fixed",
"addr": "10.25.13.64",
"version": 4
}
]
server.addresses.provider_nmnet_bif_912.addr
},
I want to parse addr "10.25.13.64".
I tried {{ item.server.addresses.provider_nmnet_bif_912.addr }} and {{os.server.addresses.provider_nmnet_bif_912.addr}} both didnot work.
Need Help!!!
Finally figured it out:
"{{ item.server.addresses.provider_nmnet_bif_912[0].addr }}"

Resources