"search" a string in "when:" conditional statement? - ansible

In the following playbook, I have to use search in a when: statement. Can you please tell me what is wrong with the statement when: item.name is search (domain_list)? domain_list is an array variable defined in files.yml as shown below.
---
- hosts: localhost
gather_facts: False
connection: local
vars_files:
- files.yml
vars:
infra:
- name: ironman.vcloud-lab.com
connection_state: CONNECTED
- name: hulk.vcloud-lab.com
connection_state: CONNECTED
- name: captain.vcloud-lab.com
connection_state: DISCONNECTED
- name: hawkeye.vcloud-lab.com
connection_state: DISCONNECTED
tasks:
- name: Filter list of only connected esxi
set_fact:
esxilist: "{{esxilist | default([]) + [item]}}"
with_items: "{{infra}}"
when: item.name is search (domain_list) ## <= please correct me here, it doesn't work
- name: Show connected esxi list information
debug:
var: esxilist
files.yml
---
domain_list:
- ironman
- captain

Select the short name and test the presence in the list. For example, use regex_replace
- name: Filter list of only connected esxi
set_fact:
esxilist: "{{ esxilist|default([]) + [item] }}"
loop: "{{ infra }}"
when: _short in domain_list
vars:
_short: "{{ item.name|regex_replace('^(.*?)\\.(.*)$', '\\1') }}"
gives
esxilist:
- connection_state: CONNECTED
name: ironman.vcloud-lab.com
- connection_state: DISCONNECTED
name: captain.vcloud-lab.com
The next option is to split the string and select the first item. For example
vars:
_short: "{{ item.name.split('.')|first }}"
gives the same result.
See How to create a Minimal, Reproducible Example. For example
- hosts: localhost
vars:
domain_list: [a, b]
infra:
- {name: a.vcloud-lab.com, state: CONNECTED}
- {name: b.vcloud-lab.com, state: DISCONNECTED}
- {name: c.vcloud-lab.com, state: CONNECTED}
tasks:
- set_fact:
l1: "{{ l1|default([]) + [item] }}"
with_items: "{{ infra }}"
when: _short in domain_list
vars:
_short: "{{ item.name.split('.')|first }}"
- debug:
var: l1

Related

Ansible filter dict based on key presence and list values

I have this variable :
components:
comp_one:
name: first_component
remote_server_path: "/tmp/path/one"
binaries:
- test
- test_two
comp_two:
name: second_component
remote_server_path: "/tmp/path/two"
binaries:
- ed_test
- ed_test_2
comp_three:
name: third_component
remote_server_path: "/tmp/path/three"
What I'm trying to achieve, is to only keep sub-dictionaries of components that has key binaries, and the filter those binaries using my group_names. Also, D'd like to only keep dict keys binaries and remote_server_path.
For example, if i have this hosts.yaml :
---
all:
children:
kubernetes_dev:
children:
ed_test:
hosts:
host_one:
ansible_host: XXX.XXX.XXX.XXX
ansible_ssh_user: ansible
test_two:
hosts:
host_one:
ansible_host: XXX.XXX.XXX.XXX
ansible_ssh_user: ansible
test:
hosts:
host_one:
ansible_host: XXX.XXX.XXX.XXX
ansible_ssh_user: ansible
The expected output would be :
comp_one:
remote_server_path: "/tmp/path/one"
binaries:
- test
- test_two
comp_two:
remote_server_path: "/tmp/path/two"
binaries:
- ed_test
I've successfully filtered the comp_three dict using this :
- name: Debug
ansible.builtin.debug:
msg: "{{ item }}"
with_items: "{{ components | dict2items | selectattr('value.binaries', 'defined') }}"
But, I can't figure out a way to only select binaries and remote_server_path keys while filtering with my `group_names.
Does anyone can't help me find a clean way to do this ?
Regards,
Use group_names. This is: "List of groups the current host is part of.". For the host host_one in the inventory hosts.yaml this is
group_names: [ed_test, test, test_two]
Convert the dictionary components to list and test intersect of binaries and group_names. Default to empty list if the attribute binaries is missing
- set_fact:
selected_list: "{{ selected_list|d([]) + [_item] }}"
loop: "{{ components|dict2items }}"
when: my_groups|length > 0
vars:
_item: "{{ item|combine(my_value) }}"
my_value: "{{ {'value': {'binaries': my_groups,
'remote_server_path': item.value.remote_server_path}} }}"
my_groups: "{{ item.value.binaries|d([])|intersect(group_names) }}"
gives
selected_list:
- key: comp_one
value:
binaries:
- test
- test_two
remote_server_path: /tmp/path/one
- key: comp_two
value:
binaries:
- ed_test
remote_server_path: /tmp/path/two
Conver the list into a dictionary
- set_fact:
selected_dict: "{{ selected_list|items2dict }}"
gives the expected result
selected_dict:
comp_one:
binaries:
- test
- test_two
remote_server_path: /tmp/path/one
comp_two:
binaries:
- ed_test
remote_server_path: /tmp/path/two
Example of a complete playbook
- hosts: all
vars:
components:
comp_one:
name: first_component
remote_server_path: /tmp/path/one
binaries:
- test
- test_two
comp_two:
name: second_component
remote_server_path: /tmp/path/two
binaries:
- ed_test
- ed_test_2
comp_three:
name: third_component
remote_server_path: /tmp/path/three
tasks:
- set_fact:
selected_list: "{{ selected_list|d([]) + [_item] }}"
loop: "{{ components|dict2items }}"
when: my_groups|length > 0
vars:
_item: "{{ item|combine(my_value) }}"
my_value: "{{ {'value': {'binaries': my_groups,
'remote_server_path': item.value.remote_server_path}} }}"
my_groups: "{{ item.value.binaries|d([])|intersect(group_names) }}"
- set_fact:
selected_dict: "{{ selected_list|items2dict }}"
- debug:
var: selected_dict

Ansible - tower_host bulk delete host from inventories

I would like to remove a host from all the inventories it is part of.
I tried something like this, but that does not work:
---
- hosts: [MYHOST]
tasks:
- name: "Test deletion"
tower_host:
name: "{{ vm_hostname }}"
inventory:
- "RedHat"
- "RedHat-Hors-Prod"
- "RedHat-Prod"
state: absent
...
Is this possible without create 3 differents tasks?
Thanks
The way to do this is by using a loop:
---
- hosts: pransibleapp
tasks:
- name: "Test deletion"
tower_host:
name: "{{ vm_hostname }}"
inventory: "{{ item }}"
state: absent
.
.
loop:
- "RedHat"
- "RedHat-Hors-Prod"
- "RedHat-Prod"
Doc : https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#iterating-over-a-simple-list

ansible get result from role and append to a list

I have a play like this
---
- name: List images in ACRs
any_errors_fatal: true
hosts:
- localhost
gather_facts: false
vars:
acrs: ["registry1", "registry2"]
tasks:
- name: list repos
with_items: "{{ acrs }}"
include_role:
name: list_docker_image_repos
vars:
registry_name: "{{ item }}"
list_docker_image_repos will do set_fact which a list of dicts.
How can I append all the facts (from every iteration) to a list?
Or is there are different way to do this?
thanks
In each iteration put the list into a dictionary. For example, given the role
shell> cat roles/list_docker_image_repos/tasks/main.yml
- set_fact:
docker_image_repos: "{{ ['repo1', 'repo2', 'repo3']|
product([registry_name])|
map('join', '-')|
list }}"
- set_fact:
my_lists: "{{ my_lists|
combine({registry_name: docker_image_repos}) }}"
the playbook
- hosts: localhost
vars:
acrs: [reg1, reg2]
my_lists: {}
tasks:
- name: list repos
include_role:
name: list_docker_image_repos
loop: "{{ acrs }}"
vars:
registry_name: "{{ item }}"
- debug:
var: my_lists
gives the dictionary
my_lists:
reg1:
- repo1-reg1
- repo2-reg1
- repo3-reg1
reg2:
- repo1-reg2
- repo2-reg2
- repo3-reg2
A dictionary is a more suitable structure for this purpose compared with a list. There are many ways how to use it. For example, you can
extract the lists
- debug:
msg: "{{ acrs|map('extract', my_lists)|list }}"
gives
msg:
- - repo1-reg1
- repo2-reg1
- repo3-reg1
- - repo1-reg2
- repo2-reg2
- repo3-reg2
Use the filter flatten to put all items into a single list
- debug:
msg: "{{ acrs|map('extract', my_lists)|flatten }}"
gives
msg:
- repo1-reg1
- repo2-reg1
- repo3-reg1
- repo1-reg2
- repo2-reg2
- repo3-reg2
Use the filter dict2items to iterate items
- debug:
var: item
loop: "{{ my_lists|dict2items }}"
gives (abridged)
item:
key: reg1
value:
- repo1-reg1
- repo2-reg1
- repo3-reg1
item:
key: reg2
value:
- repo1-reg2
- repo2-reg2
- repo3-reg2
Or, use the lookup plugin subelements to iterate the items of the lists as well
- debug:
var: item
with_subelements:
- "{{ my_lists|dict2items }}"
- value
gives (abridged)
item:
- key: reg1
- repo1-reg1
item:
- key: reg1
- repo2-reg1
item:
- key: reg1
- repo3-reg1
item:
- key: reg2
- repo1-reg2
item:
- key: reg2
- repo2-reg2
item:
- key: reg2
- repo3-reg2
Example of a complete playbook for testing
- hosts: localhost
vars:
acrs: [reg1, reg2]
my_lists: {}
tasks:
- name: list repos
include_role:
name: list_docker_image_repos
loop: "{{ acrs }}"
vars:
registry_name: "{{ item }}"
- debug:
var: my_lists
- debug:
msg: "{{ acrs|map('extract', my_lists)|list }}"
- debug:
msg: "{{ acrs|map('extract', my_lists)|flatten }}"
- debug:
var: item
loop: "{{ my_lists|dict2items }}"
- debug:
var: item
with_subelements:
- "{{ my_lists|dict2items }}"
- value

VARIABLE IS NOT DEFINED when trying to register output in playbook

I'm trying to register a variable with the output to a query of a F5 pool and I'm getting this error:
"<type 'list'>": "VARIABLE IS NOT DEFINED!",
What is that I'm doing wrong?
Any help appreciated.
Thanks!
---
- name: GRAB F5 FACTS
hosts: f5
connection: local
gather_facts: no
tasks:
- name: Collect BIG-IP facts
bigip_device_facts:
gather_subset: ltm-pools
provider: "{{ prov }}"
register: bigip_device_facts
- name: FACTS OUTPUT
debug:
var: "{{ item.members | rejectattr('state', 'match', '^present$') | map(attribute='name') | list }}"
register: jkout
with_items: "{{ bigip_device_facts.ltm_pools }}"
when: item.full_path == "/Common/mypool"
- name: Set a variable
debug:
msg: "jkvar={{ jkout }}"
You are using the debug: module with the option var: and this expects a variable, not a jinja2 template.
So either change it to:
debug:
var: item.members
or
debug:
msg: "{{ item.members }}"
Like said by #dgw, the problem is with the var option of debug module.
https://docs.ansible.com/ansible/latest/modules/debug_module.html#parameters
This playbooks works:
- name: test rejectattr
hosts: localhost
gather_facts: no
vars:
members:
- { name: "one", state: "present" }
- { name: "two", state: "absent" }
- { name: "three", state: "present" }
tasks:
- name: FACTS OUTPUT
debug:
msg: "{{ members | rejectattr('state', 'match', '^present$') | map(attribute='name') | list }}"
Thanks for your responses. I'll investigate it further.
Apart from that, I think I've been able to solve it another way.
- name: FACTS OUTPUT
set_fact:
listado: "{{ item.members | rejectattr('state', 'match', '^present$') | map(attribute='name') | list }}"
with_items: "{{ bigip_device_facts.ltm_pools }}"
when: item.full_path == "/Common/mypool"
- debug: msg={{ listado }}
register: jkout
- name: Set a variable
debug:
msg: "jkvar={{ jkout }}"
Is that a right way to do it?
Thanks!!

How to pass results back from include_tasks

Started to learn ansible yesterday, so I believe I may risk XY problem here, but still…
The main yml:
- hosts: localhost
vars_files:
[ "users.yml" ]
tasks:
- name: manage instances
#include_tasks: create_instance.yml
include_tasks: inhabit_instance.yml
with_dict: "{{users}}"
register: res
- name: print
debug: msg="{{res.results}}"
inhabit_instance.yml:
- name: Get instance info for {{ item.key }}
ec2_instance_facts:
profile: henryaws
filters:
"tag:name": "{{item.key}}"
instance-state-name: running
register: ec2
- name: print
debug:
msg: "IP: {{ec2.instances.0.public_ip_address}}"
So that's that IP that I'd like to have on the top level. Haven't found anything right away about return values of the include block…
Well, I've found some way that suits me, maybe it's even canonical?
main.yml:
- hosts: localhost
vars_files:
[ "users.yml" ]
vars:
ec_results: {}
tasks:
- name: manage instances
#include_tasks: create_instance.yml
include_tasks: inhabit_instance.yml
with_dict: "{{users}}"
register: res
inhabit_instance.yml:
- name: Get instance info for {{ item.key }}
ec2_instance_facts:
profile: henryaws
filters:
"tag:name": "{{item.key}}"
instance-state-name: running
register: ec2
- name: update
set_fact:
ec_results: "{{ ec_results|combine({ item.key: ec2.instances.0.public_ip_address }) }}"
Thanks for you answer - this helped me to find an answer to my issue when my task was called in a loop - my solution was just to use lists so for your example above, the solution would be:
The main yml:
- hosts: localhost
vars_files:
[ "users.yml" ]
vars:
ec_results: []
tasks:
- name: manage instances
#include_tasks: create_instance.yml
include_tasks: inhabit_instance.yml
with_dict: "{{users}}"
- name: print
debug: msg="{{ec_results}}"
inhabit_instance.yml:
- name: Get instance info for {{ item.key }}
ec2_instance_facts:
profile: henryaws
filters:
"tag:name": "{{item.key}}"
instance-state-name: running
register: ec2
- name: Add IP to results
set_fact:
ec_results: "{{ ec_results + [ec2.instances.0.public_ip_address] }}"
In my code, include_tasks was in a loop so then ec_results contained a list of the results from each iteration of the loop

Resources