last day (15 days later) » 

13:14
0
A: Is it possible remove host from Ansible group?

Kevin CIt's still a bit vague what is expected, and how your current execution looks like. However, you could create a list of servers which are able to connect from the jumphost to the dest_nodes. This task would take time, but you could use run_once: true to limit execution to one host for this task. ...

I may have not understood completely but, do you mean build a new third list success_nodes of successful task performed IPs inside performtask.yml and then mention something like this with_items: "{{ groups['dest_nodes'] | difference(success_nodes) }}" ?
Almost. You should use when: groups['dest_nodes' | difference(success_nodes)]
I tried the following with debug but i get syntax error and not the difference. ` groups['dest_nodes' | difference(success_nodes | default([]) )] groups['dest_nodes' | difference('success_nodes' | default([]) )] groups[dest_nodes | difference(success_nodes | default([]) )]`
The output of your comment is not understandable and not readable for me. You could use pastebin. Please, take your time to write concise responses.
@ Kevin C i updated my reply in the original post for readability.
13:14
Your syntax is incorrect. It should be groups['dest_nodes'] | difference(success_nodes).
yo
@Kevin C when dest_nodes and success_nodes both become identical as evident from the debug ....`-debug: msg="{{ groups['dest_nodes'] | difference(success_nodes | default([])) }}" prints all ips when the difference printed should be Nil/ Nothing.
I don't quite get what you're saying.
Show me the whole playbook, and output
i'm trying to print the difference between dest_nodes and success_node when they are identical i.e having the same set of IPs
What is the output of both?
sure sharing ... one minute please
---

- name: Check if connectivity already works
block:
- local_action: "raw ssh -o BatchMode=yes {{ TARGET_USER }}@{{ item }} echo success"
register: connworknow
ignore_errors: yes
- add_host:
hostname: "{{ item }}"
groups: success_nodes
when: connworknow.rc == 0
ignore_errors: yes


- debug:
msg: "SUCCESSNODE:{{ groups['success_nodes'] }}"

- debug:
msg: "DIFFERENCENODE:{{ groups['dest_nodes'] | difference(success_nodes | default([])) }}"

Output:

TASK [debug] *********************************************************************************************************************************
dest_nodes is also same as success_nodes i.e [u'10.9.22.111', u'10.9.55.22', u'10.9.66.22']"
13:24
Right, give me a couple of minutes
The above playbook is include_tasks: performtask.yml from the Original Post
Show me the output when you run:

- debug:
msg: "SUCCESSNODE:{{ groups['success_nodes'] }}"

- debug:
msg: "DIFFERENCENODE:{{ groups['dest_nodes'] }}"
Please fix indenting yourself, as this chat fucks it up
sure
With this
- debug:
msg: "DIFFERENCENODE:{{ groups['dest_nodes'] }}"
The output is:
task path: /app/axmw/Ansible/playbook/injectsshkeys/checkandaddkey.yml:37
ok: [10.9.146.220] => {
"msg": "DIFFERENCENODE:[u'10.9.22.111', u'10.9.55.22', u'10.9.66.22']"
}
ok: [192.168.88.13] => {
"msg": "DIFFERENCENODE:[u'10.9.22.111', u'10.9.55.22', u'10.9.66.22']"
}
i.e same as it was for DIFFERENCENODE:{{ groups['dest_nodes'] | difference(success_nodes | default([])) }}
So, the two lists contain the same values
Yes, thats what i m trying to convey
and i m expecting the difference to be Nil / Empty
13:30
Oh, when they are equal, you want the list to be empty?
If an equal item is found, remove it...
thats what i understood with the difference filter / function. I will run the loop on the difference rather than with_items: "{{ groups['dest_nodes'] }}" for each host play.
No, it will do like this:

list_1: 1, 2, 3, 4
list_2: 1, 2, 5, 6

it will make

1, 2, 3, 4, 5, 6

To get the difference of 2 lists (items in 1 that don’t exist in 2):
13:51
dest_nodes: 1, 2, 3, 4
success_nodes: 1, 2

my expected output is : 3,4

In my case success_nodes will always be a subset or equal to dest_nodes.
To get the difference of 2 lists (items in 1 that don’t exist in 2):

{{ list1 | difference(list2) }}
Ah, it works
As per the documentation the difference should work as i expect it to work
---
- hosts: test-multi-01
vars:
list_1:
- 1
- 2
- 3
- 4
list_2:
- 1
- 2
tasks:
- debug:
msg: "{{ list_1 | difference(list_2) }}"

Output
TASK [debug] *******************************************************************************
Tuesday 15 September 2020 15:54:56 +0200 (0:00:00.955) 0:00:00.999 *****
ok: [test-multi-01] => {
"msg": [
3,
4
]
}
why does it not work in my case ?
between the two add_host
ansible version?
Yeah but you're lists are equal right?
13:56
2.9.4
yes they are equal for sure
- debug:
msg: "DIFFERENCENODE:{{ groups['dest_nodes'] | difference(success_nodes) }}"
what does that give
still the list?
yes it give the list with all the entries and not the difference
It could be because of the groups variable. Try:

set_fact:
list_1: "{{ groups['dest_nodes'] }}"

Then do

debug:
debug:
msg: "{{ list_1 | difference(success_nodes) }}"
success_nodes is also calculated using add_host
success_nodes is also constructed using add_host
then also do
set_fact:
list_2: "{{ success_nodes }}"
14:01
Let me try
14:12
With set_facts it works!! difference is kicking in
alright, sounds good
but i wanted this to work with add_host as well
Ah.. I don't know how your data exactly looks like. Perhaps something with | to_json
14:39
Anyway, I've hope I've helped you out a bit
I'm gonna go. I'll edit my answer a bit, thanks!

last day (15 days later) »