Answer the question
In order to leave comments, you need to log in
ANSIBLE How to correlate dictionary elements and task results in a loop?
(Code and debug under spoiler)
It is necessary to register the results from the loop ( task № 1
) and check if ( ) is not command.results
stderr
empty task № 2
relative to the elements of the loop loop: "{{ lookup('dict', openvz_instances) }}"
so that I can use this condition in new tasks.
The problem is that: when
the condition from task № 2
doesn't work because I can't figure out how to split command.results with respect to dictionary elements.
Is there a solution to get around this issue?
---
- name: "CHECK IF EXIST OPENVZ CONTAINER"
hosts: "projectname-OPENVZHOSTS"
gather_facts: yes
remote_user: root
vars:
openvz_check: "yes"
openvz_instances:
opwnvz_instance_1:
vmid: "119"
opwnvz_instance_2:
vmid: "118"
tasks:
- name: "Ping my hosts"
ping:
- name: "1. Register exist containers"
shell: echo $(vzlist -a {{ item.value.vmid }})
register: command
when: openvz_check == "yes"
loop: "{{ lookup('dict', openvz_instances) }}"
ignore_errors: yes
- name: "2. Create variable from command"
debug: msg="{{ command.results | map(attribute='stderr_lines') | list }}"
when:
- openvz_check == "yes"
- command.results | map(attribute='stderr') | list != ""
loop: "{{ lookup('dict', openvz_instances) }}"
ignore_errors: yes
- debug: msg="{{ command.results | map(attribute='stderr_lines') |list }}"
command.results | map(attribute='stderr') | list
:ok: [projectname-OPENVZHOST-S1] => {
"msg": [
[],
[]
]
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question