Bug 2108651

Summary: Using set_fact with delegate_to and delegate_facts; true creates unpredicatable results
Product: Red Hat Enterprise Linux 9 Reporter: Brent Eagles <beagles>
Component: ansible-coreAssignee: Dimitri Savineau <dsavinea>
Status: CLOSED ERRATA QA Contact: Matt Clay <mclay>
Severity: urgent Docs Contact:
Priority: urgent    
Version: 9.0CC: cjeanner, jwboyer
Target Milestone: rcKeywords: Triaged
Target Release: ---Flags: pm-rhel: mirror+
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: ansible-core-2.12.2-2.el9_0 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2022-08-09 12:44:51 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 2107914    

Description Brent Eagles 2022-07-19 16:00:57 UTC
Original description found here: https://bugzilla.redhat.com/show_bug.cgi?id=2107914#c12

ansible-core.x86_64 2.12.2-1.el9

Against an OpenStack deployment we run a simple playbook:

- hosts: localhost
  vars:
    some_data: [ 'hey', 'ho', 'lets go']
    hoststo: ['controller-0', 'controller-1', 'controller-2']
  tasks:
    - name: try this
      set_fact:
        well_this: "{{ item.1 }}"
      delegate_to: "{{ item.0 }}"
      delegate_facts: true
      loop: "{{ hoststo|zip(some_data)|list }}"

    - debug: 
        msg: "{{ hostvars[item].well_this }}"
      loop: "{{ hoststo }}"

    - name: try this again
      set_fact:
        well_this: "{{ item.1 }}"
      delegate_to: "{{ item.0 }}"
      delegate_facts: true
      loop: "{{ hoststo|zip(some_data)|list }}"

    - debug: 
        msg: "{{ hostvars[item].well_this }}"
      loop: "{{ hoststo }}"

The result is:

ok: [localhost -> controller-0(192.168.24.50)] => (item=['controller-0', 'hey']) => {
    "ansible_facts": {
        "well_this": "hey"
    },
    "ansible_loop_var": "item",
    "changed": false,
    "item": [
        "controller-0",
        "hey"
    ]
}
ok: [localhost -> controller-1(192.168.24.35)] => (item=['controller-1', 'ho']) => {
    "ansible_facts": {
        "well_this": "ho"
    },
    "ansible_loop_var": "item",
    "changed": false,
    "item": [
        "controller-1",
        "ho"
    ]
}
ok: [localhost -> controller-2(192.168.24.48)] => (item=['controller-2', 'lets go']) => {
    "ansible_facts": {
        "well_this": "lets go"
    },
    "ansible_loop_var": "item",
    "changed": false,
    "item": [
        "controller-2",
        "lets go"
    ]
}

TASK [debug] **********************************************************************************************************************************
task path: /home/stack/hunh.yaml:13
ok: [localhost] => (item=controller-0) => {
    "msg": "lets go"
}
ok: [localhost] => (item=controller-1) => {
    "msg": "ho"
}
ok: [localhost] => (item=controller-2) => {
    "msg": "lets go"
}


The controller-0 fact has the value 'lets go' instead of 'hey'. As indicated in the above link it seems pretty specific to this version of ansible-core.

Comment 7 errata-xmlrpc 2022-08-09 12:44:51 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (ansible-core bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2022:5963