Based on Variable defined template should behave

23 views
Skip to first unread message

Prady A

unread,
Mar 6, 2023, 2:29:18 AM3/6/23
to ansible...@googlegroups.com
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 
 
template1etc-motd.j1
REMINDER: This is {{ ansible_hostname }}, a {{ ansible_local.function }} server of {{ ansible_local.project }}, owned by {{ ansible_local.lob }}


===
- name: set /etc/motd for abcd servers
  template:
    src: etc-motd.j2
    dest: /etc/motd
    owner: root
    group: root
    mode: 0644
    backup: yes
  when: 
       - is_us
       - ansible_local.lob is not defined 
 
Template: etc-motd.j2
REMINDER: This is {{ ansible_hostname }}, a {{ ansible_local.function }} server of {{ ansible_local.project }}, owned by {{ ansible_local.bu }}

Regards 

Todd Lewis

unread,
Mar 6, 2023, 7:40:46 AM3/6/23
to Ansible Project
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) }}

Prady A

unread,
Mar 6, 2023, 8:15:20 AM3/6/23
to ansible...@googlegroups.com
Thank you Lewis.. I ll try .. 

Regards 

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f0b3e3b0-89c7-4033-b07d-14243352ffd1n%40googlegroups.com.

HAMIDOU OUARO

unread,
Mar 6, 2023, 9:26:16 AM3/6/23
to ansible...@googlegroups.com
Please, I ask the last version of ansible

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Antony Stone

unread,
Mar 6, 2023, 9:31:26 AM3/6/23
to ansible...@googlegroups.com
On Monday 06 March 2023 at 15:25:55, HAMIDOU OUARO wrote:

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

Subject: [ansible-project] Ansible 7.3.0 has been Released!
Date: Tuesday 28 February 2023 16:51:29
From: Anwesha Das
To: ansibl...@googlegroups.com, ansible-...@googlegroups.com,
ansible...@googlegroups.com

Hello everyone,

We're happy to announce that the Ansible 7.3.0 package has been released!

Ansible 7.3.0 will include ansible-core 2.14 as well as a curated set of
Ansible collections to provide a vast number of modules and plugins.

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

If that was not your question then please re-phrase it so we may be able to
help further.


Antony.

--
We all get the same amount of time - twenty-four hours per day.
How you use it is up to you.

Please reply to the list;
please *don't* CC me.

Prady A

unread,
Mar 7, 2023, 7:28:15 AM3/7/23
to ansible...@googlegroups.com
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

On Mon, Mar 6, 2023 at 21:40, Todd Lewis <uto...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages