set_fact with default value of type string

17 views
Skip to first unread message

John Harmon

unread,
Jan 28, 2019, 4:41:53 PM1/28/19
to Ansible Project
I know I can define the variable previous to this, but I am wondering if I can combine setting a variable with a default value within set_fact.  If possible, I wonder if my syntax is wrong (likely)?  I can't seem to find much documentation on this.  If you have a link to this answer in the docs, I would like to review it, but apparently I am not using the right keywords in my search.

Basically, I wish to have the handler_name set to Networking unless my major version is 8.  If that is the case then I want it set to: Network Manager

- set_fact:
    handler_name
: "{{ 'Network Manager' | default('Networking') }} "
 
when: ansible_distribution_major_version == 8


Kai Stian Olstad

unread,
Jan 28, 2019, 5:02:15 PM1/28/19
to ansible...@googlegroups.com
handler_name: "{{ 'Network Manager' if
ansible_distribution_major_version == 8 else 'Networking' }}"

or

- set_fact:
handler_name: "{{ (ansible_distribution_major_version == 8) |
ternary('Network Manager', ''Networking') }}"


Which one to use comes down to personal preference.


--
Kai Stian Olstad

John Harmon

unread,
Jan 28, 2019, 5:09:28 PM1/28/19
to Ansible Project
Wow, I was really off.  Thanks Kai!
Reply all
Reply to author
Forward
0 new messages