# Based on Variable defined template should behave

**URL:** <https://forum.ansible.com/t/based-on-variable-defined-template-should-behave/37730>\
**Category:** Ansible Project\
**Created:** [March 6, 2023, 7:28am UTC](https://forum.ansible.com/t/based-on-variable-defined-template-should-behave/37730 "2023-03-06T07:28:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [March 6, 2023, 7:28am UTC](https://forum.ansible.com/t/based-on-variable-defined-template-should-behave/37730/1 "2023-03-06T07:28:55Z")

</div>

Hello experts,

Need one little suggestion on below …  
Below is my current code .

- name: set /etc/motd for abcd servers  
template:  
src: etc-motd.j1  
dest: /etc/motd  
owner: root  
group: root  
mode: 0644  
backup: yes  
when:
- is\_us

**Template:** etc-motd.j1  
REMINDER: This is {{ ansible\_hostname }}, a {{ ansible\_local.function }} server of {{ ansible\_local.project }}, owned by {{ ansible\_local. **bu** }}

Now the problem is when ansible\_local. **lob is** defined then the above ansible\_local. **bu** should be replaced by ansible\_local. **lob** but if not defined then it should use ansible\_local. **bu.**

I know if I write two templates one is for **bu** and one is for **lob** the it might solve. Like below . But it will too much change for this requirement I guess . Any better idea will be appreciated.

**-** name: set /etc/motd for abcd servers  
template:  
src: etc-motd.j1  
dest: /etc/motd  
owner: root  
group: root  
mode: 0644  
backup: yes  
when:

- is\_us
- ansible\_local. **lob** is **defined**

**template1** : etc-motd.j1  
REMINDER: This is {{ ansible\_hostname }}, a {{ ansible\_local.function }} server of {{ ansible\_local.project }}, owned by {{ ansible\_local. **lob** }}

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 6, 2023, 12:40pm UTC](https://forum.ansible.com/t/based-on-variable-defined-template-should-behave/37730/2 "2023-03-06T12:40:46Z")

</div>

**Template:** etc-motd.j1  
REMINDER: This is {{ ansible\_hostname }}, a {{ ansible\_local.function }} server of {{ ansible\_local.project }}, owned by {{ ansible\_local.lob | default(ansible\_local.bu) }}

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [March 6, 2023, 1:14pm UTC](https://forum.ansible.com/t/based-on-variable-defined-template-should-behave/37730/3 "2023-03-06T13:14:56Z")

</div>

Thank you Lewis… I ll try …

Regards

---

<div class="post-metadata">

**Author:** ![HAMIDOU\_OUARO](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@HAMIDOU\_OUARO](https://forum.ansible.com/u/HAMIDOU_OUARO)\
**Post date:** [March 6, 2023, 2:25pm UTC](https://forum.ansible.com/t/based-on-variable-defined-template-should-behave/37730/4 "2023-03-06T14:25:55Z")

</div>

Please, I ask the last version of ansible

---

<div class="post-metadata">

**Author:** ![Antony\_Stone](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Antony\_Stone](https://forum.ansible.com/u/Antony_Stone)\
**Post date:** [March 6, 2023, 2:31pm UTC](https://forum.ansible.com/t/based-on-variable-defined-template-should-behave/37730/5 "2023-03-06T14:31:02Z")

</div>

> Please, I ask the last version of ansible

If you are asking what the latest version of Ansible is (nothing to do with  
the topic you posted this question in) then this may help:

---------- Forwarded Message Starts ----------

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [March 7, 2023, 12:27pm UTC](https://forum.ansible.com/t/based-on-variable-defined-template-should-behave/37730/6 "2023-03-07T12:27:54Z")

</div>

Hi Lewis

Your advice worked like a charm. Thank u again . I replaced this piece of code with yours.

{% if variable is defined %}  
value of variable: {{ variable }}  
{% else %}  
variable is not defined  
{% endif %}

But can we extend you piece of code to take the default value except for Amazon servers.  
Amazon servers = ansible\_local.bu  
!Amazon servers = ansible\_local.lob

Regards  
PD
