Skip to content

Instantly share code, notes, and snippets.

@odyssey4me
Last active July 31, 2018 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odyssey4me/7def43fef79564edc7aa6d918f344135 to your computer and use it in GitHub Desktop.
Save odyssey4me/7def43fef79564edc7aa6d918f344135 to your computer and use it in GitHub Desktop.
mbuil
$ ansible-playbook test.yml
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *********************************************************************************************************************************
TASK [set_fact] **********************************************************************************************************************************
ok: [localhost]
TASK [debug] *************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"address": "2.2.2.2"
}
}
PLAY RECAP ***************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
$ ansible-playbook test.yml -e mgmt_vlan=native
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *********************************************************************************************************************************
TASK [set_fact] **********************************************************************************************************************************
ok: [localhost]
TASK [debug] *************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"address": "1.1.1.1"
}
}
PLAY RECAP ***************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
- hosts: localhost
connection: local
gather_facts: no
vars:
mgmt_interface: eth0
mgmt_vlan: 10
device:
"eth0":
address: "1.1.1.1"
"eth0.10":
address: "2.2.2.2"
tasks:
- set_fact:
device_to_get: "{{ (mgmt_vlan == 'native') | ternary(mgmt_interface, mgmt_interface ~ '.' ~ mgmt_vlan) }}"
- debug:
msg: "{{ device[device_to_get] }}"
- debug:
msg: >-
{%- if mgmt_vlan == 'native' %}
{%- set device_to_get2 = mgmt_interface %}
{%- else %}
{%- set device_to_get2 = mgmt_interface ~ '.' ~ mgmt_vlan %}
{%- endif %}
{{- device[device_to_get2] }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment