# set\_fact with default value of type string

**URL:** <https://forum.ansible.com/t/set-fact-with-default-value-of-type-string/29552>\
**Category:** Ansible Project\
**Created:** [January 28, 2019, 9:41pm UTC](https://forum.ansible.com/t/set-fact-with-default-value-of-type-string/29552 "2019-01-28T21:41:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 28, 2019, 9:41pm UTC](https://forum.ansible.com/t/set-fact-with-default-value-of-type-string/29552/1 "2019-01-28T21:41:50Z")

</div>

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

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 28, 2019, 10:02pm UTC](https://forum.ansible.com/t/set-fact-with-default-value-of-type-string/29552/2 "2019-01-28T22:02:05Z")

</div>

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handler\_name: "{{ 'Network Manager' if ansible\_distribution\_major\_version == 8 else 'Networking' }}"

or

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handler\_name: "{{ (ansible\_distribution\_major\_version == 8) | ternary('Network Manager', ''Networking') }}"

Which one to use comes down to personal preference.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 28, 2019, 10:09pm UTC](https://forum.ansible.com/t/set-fact-with-default-value-of-type-string/29552/3 "2019-01-28T22:09:28Z")

</div>

Wow, I was really off. Thanks Kai!
