Simplify and fix the condition. Use the default value. This will cover both tests, e.g.
shell> cat pb.yml
- hosts: localhost
gather_facts: "{{ (migrated|default('no') == 'yes')|ternary(false, true) }}"
tasks:
- meta: noop
will gather facts without the variable migrated defined
shell> ansible-playbook pb.yml
PLAY [localhost] ********************************************************
TASK [Gathering Facts] **************************************************
ok: [localhost]
, or when the variable is set to other value than 'yes'
shell> ansible-playbook pb.yml -e migrated=no
PLAY [localhost] ********************************************************
TASK [Gathering Facts] **************************************************
ok: [localhost]
When the variable is set to 'yes' no facts will be gathered
shell> ansible-playbook pb.yml -e migrated=yes
PLAY [localhost] ********************************************************
PLAY RECAP **************************************************************
Jinja
If you insist on Jinja the playbook below gives the same results
shell> cat pb.yml
- hosts: localhost
gather_facts: "{% if migrated|default('no') == 'yes' %}
false
{% else %}
true
{% endif %}"
tasks:
- meta: noop
Boolean
You can further simplify the test by explicit conversion to Boolean, e.g.
- hosts: localhost
gather_facts: "{{ (migrated|default('no')|bool)|ternary(false, true) }}"
tasks:
- meta: noop
Truthy/Falsy
Make sure you understand how Boolean conversion and testing work. See results of the tasks
- debug:
msg: "True"
loop: [yes, Yes, true, True, xxx]
when: item|bool
- debug:
msg: "False"
loop: [no, No, false, False, xxx]
when: not item|bool
- debug:
msg: "{{ item|bool|ternary(True, False) }}"
loop: [yes, Yes, true, True, xxx,
no, No, false, False, xxx]
- debug:
msg: "{{ item|ternary(True, False) }}"
loop: [yes, Yes, true, True, xxx,
no, No, false, False, xxx]
Q: "Passing the variable 'migrated' from within the inventory does not work."
A: You're right. It seems that the inventory variables are not available at the time gather_facts is running. Use setup as a workaround. For example
- hosts: localhost
gather_facts: false
tasks:
- setup:
when: (migrated|default('no')|bool)|ternary(false, true)