Skip to content

Instantly share code, notes, and snippets.

@icarrr
Created November 12, 2019 08:04
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 icarrr/d3ab91c6579c4275697bf36ac995555e to your computer and use it in GitHub Desktop.
Save icarrr/d3ab91c6579c4275697bf36ac995555e to your computer and use it in GitHub Desktop.
ansible-playbook uses variables from different files

Result

(env) ~/sibunglon/gist ❯❯❯ ansible-playbook main.yml

PLAY [localhost] *******************************************************************************************************************************************************************************************

TASK [Load Variable 1] *************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "example.com/x"
}

TASK [Load variable 2] *************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "example.com/x"
}

TASK [Load variable 3] *************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "example.com/y"
}

PLAY RECAP *************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
---
config_x:
- name: 'Url X'
url: 'example.com/x'
- name: 'Url Y'
url: 'example.com/y'
config_y:
- name: 'Url X'
url: 'example.com/x'
- name: 'Url Y'
url: 'example.com/y'
---
- hosts: localhost
gather_facts: no
vars_files:
- config.yml
tasks:
- name: Load Variable 1
debug:
msg: "{{ config_x[0].url }}"
- name: Load variable 2
debug:
msg: "{{ config_y[0].url }}"
- name: Load variable 3
debug:
msg: "{{ config_y[1].url }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment