Based on Variable defined template should behave

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 }}

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) }}

Thank you Lewis… I ll try …

Regards

Please, I ask the last version of ansible

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 ----------

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