---
- hosts: localhost
gather_facts: yes
vars:
key1: value1
key2: value2
vars_files:
- extra_vars.yml
tasks:
- name: echo local vars
debug:
msg: |
key1 = {{ key1 }}
key2 = {{ key2 }}
- name: echo extra vars
debug:
msg: |
exKey1 = {{ exKey1 }}
exKey2 = {{ exKey2 }}
- name: echo extra vars
debug:
msg: |
pKey1 = {{ pKey1 }}
pKey2 = {{ pKey2 }}
- name: template
template: src=templates/template.txt.j2 dest=/tmp/template.txt
---
exKey1: exValue1
exKey2: exValue2
$ ansible-playbook variable.yml -e "pKey1=pValue1 pKey2=pValue2"
[WARNING]: Host file not found: /etc/ansible/hosts
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [echo local vars] *********************************************************
ok: [localhost] => {
"msg": "key1 = value1\nkey2 = value2\n"
}
TASK [echo extra vars] *********************************************************
ok: [localhost] => {
"msg": "exKey1 = exValue1\nexKey2 = exValue2\n"
}
TASK [echo extra vars] *********************************************************
ok: [localhost] => {
"msg": "pKey1 = pValue1\npKey2 = pValue2\n"
}
TASK [template] ****************************************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=5 changed=1 unreachable=0 failed=0
$ ansible testserver -m setup
testserver | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.0.2.15"
],
"ansible_all_ipv6_addresses": [
"fe80::5054:ff:feb2:63eb"
],
"ansible_architecture": "x86_64",
"ansible_bios_date": "12/01/2006",
"ansible_bios_version": "VirtualBox",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-327.13.1.el7.x86_64",
"LANG": "en_US.UTF-8",
"biosdevname": "0",
"console": "ttyS0,115200",
...
---
- hosts: localhost
gather_facts: yes
vars:
key1: value1
key2: value2
vars_files:
- extra_vars.yml
tasks:
- name: echo local vars
debug:
msg: |
key1 = {{ key1 }}
key2 = {{ key2 }}
- name: echo extra vars
debug:
msg: |
exKey1 = {{ exKey1 }}
exKey2 = {{ exKey2 }}
- name: echo extra vars
debug:
msg: |
pKey1 = {{ pKey1 }}
pKey2 = {{ pKey2 }}
- name: template
template: src=templates/template.txt.j2 dest=/tmp/template.txt
---
exKey1: exValue1
exKey2: exValue2
key1 = {{ key1 }}
key2 = {{ key2 }}
exKey1 = {{ exKey1 }}
exKey2 = {{ exKey2 }}
pKey1 = {{ pKey1 }}
pKey2 = {{ pKey2 }}
ansible_managed = {{ansible_managed}}
template_host = {{template_host}}
template_uid = {{template_uid}}
template_path = {{template_path}}
template_fullpath = {{template_fullpath}}
template_run_date = {{template_run_date}}
$ ansible-playbook variable.yml -e "pKey1=pValue1 pKey2=pValue2"
$ cat /tmp/template.txt
key1 = value1
key2 = value2
exKey1 = exValue1
exKey2 = exValue2
pKey1 = pValue1
pKey2 = pValue2
ansible_managed = Ansible managed: /Users/atsushi/sourcetree/AnsibleExam/variable/templates/template.txt.j2 modified on 2016-06-20 00:30:28 by atsushi on HONODH-no-MacBook-Pro.local
template_host = HONODH-no-MacBook-Pro.local
template_uid = atsushi
template_path = /Users/atsushi/sourcetree/AnsibleExam/variable/templates/template.txt.j2
template_fullpath = /Users/atsushi/sourcetree/AnsibleExam/variable/templates/template.txt.j2
template_run_date = 2016-06-20 00:30:33.460985