Hi Todd. I believe I responded to you but it seems my response was deleted. 
I can post the full playbook and output if required. A lot of those debug 
steps were just to understand why the condition was being ignored when 
"is_leader" is set to false

I'll incorporate the suggestion above to reduce the number of tasks 
required to do this. The goal was simply to add the tasks into an in-memory 
group and carry out some serial action in the order in which they were 
added. 

Thanks

On Tuesday, April 23, 2024 at 11:28:59 PM UTC+1 Todd Lewis wrote:

All that said, I don't see why this one task wouldn't work for your 
purposes:
- name: Add hosts to groups ansible.builtin.add_host: name: "{{ 
inventory_hostname }}" groups: - nodes - "{{ 'nodes_prim' if is_active | 
d(false) else 'nodes_stby' }}" 

On 4/23/24 5:40 PM, Todd Lewis wrote:

Hard to say. The job log you show doesn't match the tasks in the playbook 
you posted. They seem to be the same down to the "Debug is_leader value" 
task, but after that the task names in the log don't match those in the 
playbook.

In the playbook you posted, task "Add standby host" an task "Add primary 
host" both add the hosts to the same group. That may be a source of your 
problem. But as we don't see the output for those tasks …, hmm.
—
Todd

On 4/23/24 4:03 PM, 'deewon' via Ansible Project wrote:

ansible version: 2:11.2 
jinja version: 3.0.3
python version: 3.6.8

Hello all

I have a peculiar problem with a relatively simple playbook I have written. 
It basically checks 2 servers to determine which one has more than 1 IP and 
uses that to set a simp;e fact called "is_leader" for each node. 

This fact is then to be used  to build an in-memory inventory using module 
add_host to iterate through the servers based on the value of that fact. 
See below:

- name: Get Server Recycling Order
  hosts: "{{ my_servers | default ('localhost') }}"
  become: yes
  become_method: sudo
  become_user: root

  tasks:

    - name: Set Patching Sequence
      tags: always
      block:

         - name: Get IP addresses of eth0 using nmcli command
           command: nmcli -g ip4.address conn show "System eth0"
           register: nmcli_output
           changed_when: false


         - name: Get nmcli output
           debug:
             var: nmcli_output


         - name: Set is_leader variable based on number of IPs
           set_fact:
             is_leader: "{{ (nmcli_output.stdout.split('|') | length) > 1 
}}"


         - name: Display if server is ACTIVE (1) or PASSIVE (0)
           debug:
             var: is_leader


         - name: Debug is_leader value
           debug:
              msg: "is_leader value for {{ inventory_hostname }} is {{ 
is_leader }}"


         - name: Add standby host
           add_host:
              name: "{{ inventory_hostname }}"
              groups: nodes
           when: not is_leader 

         - name: Add primary host
           add_host:
              name: "{{ inventory_hostname }}"
              groups: nodes
           when: is_leader

#- name: Execute tasks on servers in a specified order
#  hosts: nodes
#  gather_facts: no
#  serial: 1
#  tasks:
#    - name: Run a command in a specified order
#      command: echo "Running on {{ inventory_hostname }}"
#      tags: always

The problem is that when this runs, the  "is_leader" fact is  ignored for 
when the condition is false and the standby node is  not added to the 
inventory. The relevant extract from the execution is below:

 TASK [Set is_leader variable based on number of IPs] 
**************************************************************************************************************************
Tuesday 23 April 2024  19:46:31 +0000 (0:00:00.079)       0:00:16.815 
*********
ok: [node-1] => {
    "ansible_facts": {
        "is_leader": true
    },
    "changed": false
}
ok: [node-2] => {
    "ansible_facts": {
        "is_leader": false
    },
    "changed": false
}

TASK [Display if server is ACTIVE (1) or PASSIVE (0)] 
**************************************************************************************************************************
Tuesday 23 April 2024  19:46:31 +0000 (0:00:00.083)       0:00:16.898 
*********
ok: [node-1] => {
    "is_leader": true
}
ok: [node-2] => {
    "is_leader": false
}

TASK [Debug is_leader value] 
**************************************************************************************************************************************************
Tuesday 23 April 2024  19:46:31 +0000 (0:00:00.072)       0:00:16.971 
*********
ok: [node-1] => {}

MSG:

is_leader value for node-1 is True
ok: [node-2] => {}

MSG:

is_leader value for node-2 is False

TASK [Re-confirm is_leader before adding to group] 
****************************************************************************************************************************
Tuesday 23 April 2024  19:46:31 +0000 (0:00:00.074)       0:00:17.045 
*********
skipping: [node-1] => {}
ok: [node-2] => {}

MSG:

Preparing to add node-2 to stby group based on is_leader = False

TASK [Add standby proxysql node] 
***********************************************************************************************************************************************
Tuesday 23 April 2024  19:46:31 +0000 (0:00:00.088)       0:00:17.134 
*********
skipping: [node-1] => {
    "changed": false,
    "skip_reason": "Conditional result was False"
}

TASK [Re-confirm is_leader before adding to group] 
****************************************************************************************************************************
Tuesday 23 April 2024  19:46:31 +0000 (0:00:00.036)       0:00:17.170 
*********
ok: [node-1] => {}

MSG:

Preparing to add node-1 to prim group based on is_leader = True
skipping: [node-2] => {}

TASK [Add primary proxysql node] 
***********************************************************************************************************************************************
Tuesday 23 April 2024  19:46:31 +0000 (0:00:00.059)       0:00:17.230 
*********
changed: [node-1] => {
    "add_host": {
        "groups": [
            "nodes_prim"
        ],
        "host_name": "node-1",
        "host_vars": {}
    },
    "changed": true
}

PLAY RECAP 
*********************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0 
   skipped=0    rescued=0    ignored=0
node-1 : ok=8    changed=1    unreachable=0    failed=0    skipped=2   
 rescued=0    ignored=0
node-2 : ok=7    changed=0    unreachable=0    failed=0    skipped=1   
 rescued=0    ignored=0


I'm not sure what I'm missing  regarding this behaviour as it seems like it 
should simply work?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an 
email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/4d6c8e6a-478c-4736-9e10-c12032dd7c3fn%40googlegroups.com
 
<https://groups.google.com/d/msgid/ansible-project/4d6c8e6a-478c-4736-9e10-c12032dd7c3fn%40googlegroups.com?utm_medium=email&utm_source=footer>
.


-- Todd 


-- Todd 

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/15b15cab-0a8a-4adb-b70b-83d054a49cb4n%40googlegroups.com.

Reply via email to