Ansible set_fact sets fact even when another fact is undefined?

694 views
Skip to first unread message

Ford FasteRR

unread,
Aug 21, 2018, 11:05:15 AM8/21/18
to Ansible Project

I'm seeing strange behavior in ansible 2.5.5 (on centos 6.10)

`facts_help.yml
---
- debug:
    var: fake_fact
- debug:
    var: new_fact

- set_fact:
    new_fact: fake_fact
    when: fake_fact is defined

- debug:
    var: fake_fact
- debug:
    var: new_fact`

I see this output:

PLAY [localhost] 
*********************************************************************

TASK [Gathering Facts] 
***************************************************************
ok: [localhost]

TASK [include_tasks] 
*****************************************************************
included: /etc/ansible/facts_help.yml for localhost

TASK [debug] 
*************************************************************************
ok: [localhost] => {
"fake_fact": "VARIABLE IS NOT DEFINED!"
}

TASK [debug] 
*************************************************************************
ok: [localhost] => {
    "new_fact": "VARIABLE IS NOT DEFINED!"
}

TASK [set_fact] 
**********************************************************************
ok: [localhost]

TASK [debug] 
*************************************************************************
ok: [localhost] => {
"fake_fact": "VARIABLE IS NOT DEFINED!"
}

TASK [debug] 
*************************************************************************
ok: [localhost] => {
    "new_fact": "fake_fact"
}

PLAY RECAP 
***************************************************************************
localhost                  : ok=7    changed=0    unreachable=0    failed=0

As you can see, fake_fact is undefined, and debug validates that. The when: clause evaluates to true, and allows new_fact to be set (that's my problem)..

Afterwards, a repeat of the debug on fake_fact still appears undefined.

So I'm not sure if this really a feature that I don't understand, a bug, or just a user error?

Karl Auer

unread,
Aug 21, 2018, 11:40:03 AM8/21/18
to ansible...@googlegroups.com
The when clause should line up under set_fact. When I indent it properly, that play is skipped as expected. Not sure why the wrongly indented version is not picked up as a syntax error.

Here is the playbook I ran:

---
- hosts: localhost
  gather_facts: false
  become: false

  tasks:

     - debug:
         var: fake_fact
     - debug:
         var: new_fact

     - set_fact:
         new_fact: fake_fact
       when: fake_fact is defined
      
     - debug:
         var: fake_fact
     - debug:
         var: new_fact


And here are the results:


PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] => {
    "fake_fact": "VARIABLE IS NOT DEFINED!: 'fake_fact' is undefined"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "new_fact": "VARIABLE IS NOT DEFINED!: 'new_fact' is undefined"
}

TASK [set_fact] ****************************************************************
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional result was False"}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "fake_fact": "VARIABLE IS NOT DEFINED!: 'fake_fact' is undefined"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "new_fact": "VARIABLE IS NOT DEFINED!: 'new_fact' is undefined"
}

PLAY RECAP *********************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0  

You are setting new_fact to the literal string "fake_fact", not to the value of a variable called fake_fact. Not sure that's relevant at this point.

Regards, K.



--
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+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d0f5ff3f-e5f9-4645-83e4-c564cc7f378b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Karl Auer

Email  : ka...@2pisoftware.com
Website: 
http://2pisoftware.com

GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

Kai Stian Olstad

unread,
Aug 21, 2018, 11:43:38 AM8/21/18
to ansible...@googlegroups.com
On 21.08.2018 17:05, Ford FasteRR wrote:
> I'm seeing strange behavior in ansible 2.5.5 (on centos 6.10)
>
> `facts_help.yml
> ---
> - debug:
> var: fake_fact
> - debug:
> var: new_fact
>
> - set_fact:
> new_fact: fake_fact
> when: fake_fact is defined

Indentation error, when: is a property for the task.
Here you set a variable called when to the string "fake_fact is defined"


> As you can see, fake_fact is undefined, and debug validates that. The
> when:
> clause evaluates to true, and allows new_fact to be set (that's my
> problem)..
>
> Afterwards, a repeat of the debug on fake_fact still appears undefined.
>
> So I'm not sure if this really a feature that I don't understand, a
> bug, or
> just a user error?

User error.

--
Kai Stian Olstad

Ford FasteRR

unread,
Aug 21, 2018, 12:07:15 PM8/21/18
to Ansible Project
Thanks for the update and clarification on this !!! 
Reply all
Reply to author
Forward
0 new messages