Condition in Templates

69 views
Skip to first unread message

jpall...@gmail.com

unread,
Sep 18, 2015, 10:16:50 AM9/18/15
to Ansible Project
Hi everyone,

one simple question and sry for my english skills :-)

I have a playbook with some templates, in one template I added a condition to write a specific IP Address in my template.
(My servers have several network ports and unfortunately they have sometimes different names. Ok, i could use different templates but i wanted to use a single file)

The code:

{% if ansible_mgt.ipv4.address is defined %}
server_address={{ansible_mgt.ipv4.address}}
{% else %}
server_address={{ansible_eth0.ipv4.address}}
{% endif %}

If I run the playbook against a server which has not the network port ansible_mgt the playbook fails because the variable ansible_mgt.ipv4.address is not defined.

What I am doing wrong? Or it is not possible to check in a template if a variable is not defined?

Thanks for your help!

Regards,
Jost

David Karban

unread,
Sep 18, 2015, 10:20:47 AM9/18/15
to ansible...@googlegroups.com
Hi, 

I guess, in that case not even ansible_mgt will be defined? What is exact error message (try ansible-playbook with -vvv) ?

David
Linux server specialist/Specialista na správu linuxových serverů
www.karban.eu

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d3b7c341-c7bc-4750-b081-0c66f2de3c8e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jpall...@gmail.com

unread,
Sep 18, 2015, 10:34:31 AM9/18/15
to Ansible Project
Hi David,

the error message is:
fatal: [testserver.xxx] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'ansible_mgt' is undefined", 'failed': True}
fatal: [testserver.xxx] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'ansible_mgt' is undefined", 'failed': True}
FATAL: all hosts have already failed -- aborting

Well, that is true. On this machine I have only ansible_eth0:
        "ansible_eth0": {
            "active": true,
            "device": "eth0",
            "ipv4": {
                "address": "10.101.10.133",
                "netmask": "255.255.255.0",
                "network": "10.101.10.0"
            },

My idea was to use the condition, if ansible_mgt is defined -> use ansible_mgt and if not -> ansible_eth0

Regards,
Jost

David Karban

unread,
Sep 18, 2015, 10:39:37 AM9/18/15
to ansible...@googlegroups.com
Hi Josh, 

your idea is right:
{% if ansible_mgt is defined and ansible_mgt.ipv4.address is defined %}
server_address={{ansible_mgt.ipv4.address}}
{% else %}
server_address={{ansible_eth0.ipv4.address}}
{% endif %}

you have to check ansible_mgt first.

David
Linux server specialist/Specialista na správu linuxových serverů
www.karban.eu

jpall...@gmail.com

unread,
Sep 18, 2015, 10:49:04 AM9/18/15
to Ansible Project
Hi David,

of course! thank you very much! 

I didn't recognized that the error message was only saying "ansible_mgt" and not "ansible_mgt.ipv4.address"...facepalm

Regards,
Jost
Reply all
Reply to author
Forward
0 new messages