1
votes

I have a scenario where I need to set a variable in group var depending on a string parameter named "provider" I pass when running the ansible playbook.

ansible 2.3.0.0, python version = 2.7.5 In groups_vars.yml, this is what I am trying to achieve

if provider=azure then
insecure_registrys: ['{{azure_ip.state.ip_address}}:5000'] else if
provider=vsphere then
insecure_registrys: ['11.168.25.xx:5000']

I'm trying this way after checking in on an issue in github

insecure_registrys: "{{ ( true | ternary('['{{azure_ip.state.ip_address}}:5000']','['11.168.25.xx:5000']'))}}"

true above will be replaced by provider check later.It passes the syntax check but fails in the middle

FAILED! => {"failed": true, "msg": "{% if etcd_interface != '' %}{{ hostvars[inventory_hostname]['ansible_' + etcd_interface].ipv4.address }}{%- else %}{{ hostvars[inventory_hostname].ansible_default_ipv4.address }}{% endif %}: template error while templating string: expected token ',', got '{'. String: {{ ( true | ternary('['{{azure_ip.state.ip_address}}:5000']','['11.168.25.xx:5000']'))}}"}

Could you help me in this situation? Thx

1

1 Answers

0
votes

Never use nested Jinja2 expressions, be simpler:

insecure_registries: "{{ [azure_ip.state.ip_address+':5000'] if provider == 'azure' else ['11.168.25.xx:5000'] }}

And with ternary filter:

insecure_registries: "{{ (provider == 'azure') | ternary( [azure_ip.state.ip_address+':5000'], ['11.168.25.xx:5000'] ) }}