- Several different types:
- Inventory variables
- Play scoped variables
- Task variables
- Extra variables
- Scoped to a host throughout the execution of a playbook
- Assigned in
- The inventory file
- Specific subdirectories
- host_vars/host
- group_vars/group
- Can be referenced directly when executing play on host
- Available in hostvars dictionary
- Assign variable to a single host
[web]
web1.mycompany.com ansible_host=152.240.43.12 opt2=arg2
web2.mycompany.com
.
- Assign variables to groups of hosts
[web]
web1.mycompany.com ansible_host=152.240.43.12
web2.mycompany.com
[web:vars]
proxy=web.mycompany.com
- Ansible will automatically load files under
host_vars
directory- Files with same name as particular host
- Any files in a directory with the same name as a host
[web] web1.myhost.com web2.myhost.com
$INTRO_ANSIBLE_DIR/working-with-playbooks └── host_vars ├── web1.myhost.com.yml └── web2.myhost.com └── vars.yml
- Create inventory variables for myserver host
- Try either way:
cd $INTRO_ANSIBLE_DIR/working-with-playbooks
mkdir -p host_vars
gedit host_vars/myserver.yml
mkdir -p host_vars/myserver
gedit host_vars/myserver/vars.yml
# variables for myserver
---
foo: bar
- Similar to
host_vars
except for groups- Files with same name as particular group
- Any files in a directory with the same name as a group
[web] web1.myhost.com [app] app1.myhost.com
$INTRO_ANSIBLE_DIR/working-with-playbooks └── group_vars ├── web.yml └── app └── vars.yml
- Create a
group_vars
directory and variable file
cd $INTRO_ANSIBLE_DIR/working-with-playbooks/
mkdir -p group_vars
gedit group_vars/web.yml
mkdir -p group_vars/web
gedit group_vars/web/vars.yml
# variables for "web" group
---
bizz: buzz
- Inventory variables available to Ansible throughout playbook run
$ less $INTRO_ANSIBLE_DIR/working-with-playbooks/templates/index.html.j2
.
.
{% if foo is defined %}
<p>
The value for foo is <em>{{ foo }}</em>
</p>
{% endif %}
{% if bizz is defined %}
<p>
The value for bizz is <em>{{ bizz }}</em>
</p>
{% endif %}
.
.
ansible-playbook static-site.yml
- Run the ansible playbook again
- Note behaviour of Copy up static website html task
- Reload website when finished
- Dictionary with variables from each host (including inventory variables)
- Indexed on hostname from inventory
hostvars['myserver']['foo'] == hostvars.myserver.foo
- Can be accessed across plays in the same playbook run
- Have a look at
use-hostvars.yml
- Run
static-site.yml
anduse-hostvars.yml
ansible-playbook static-site.yml use-hostvars.yml
- Change
foo
tohostvars.myserver.foo
inplaybook-localhost.yml
and run playbooks
- The bizz variable still appears as undefined in playbook run
- How would you find that in hostvars?
- A group variable will be defined for every host in a group
- name: Output of bizz
debug:
var: hostvars.myserver.bizz
- Declared in control section of a play
- vars - static declaration
- vars_prompt - interactively prompt user when running playbook
- Can be referenced directly while in play
- Open
static-site.yml
in editor - Edit as shown:
- Rerun ansible-playbook
- name: Set up static website with nginx
hosts: myserver
become: true
vars_prompt:
- name: "participant_name"
prompt: "What is your name"
private: no
vars:
base_path: "/"
course: Introduction to ansible
lesson:
name: Working with play variables
session: 1
tasks:
.
.
- Contain data discovered while executing a task
- Gathering facts phase of play execution
- Tasks that gather user input
- Become part of the host scope once defined
- In a play they can be directly referenced
- In subsequent plays are available in hostvars dictionary for host
tasks:
- name: Assign an arbitrary variable
set_fact:
person_name: Homer Simpson
- name: Assign dictionary
set_fact:
person_details:
street: 754 Evergreen Terrace
city: Springfield
state: ST
- name: Execute code and capture the output
command: uptime
register: uptime_output
.
.
- name: do something with output of uptime
debug:
var: {{ uptime_output.stdout }}
tasks:
- name: set name of person
set_fact:
person_name: Homer Simpson
- name: Get output of uptime
command: uptime
register: uptime_output
.
.
- Use
set_fact
andregister
instatic-site.yml
to assign task variables - Rerun ansible playbook
- Plugin run before play executes
setup
module- Collects detailed information about host and attaches to host scope
- You can get an idea by running
ansible myserver -m setup
- Filter facts to find variables you are interested in:
ansible myserver -m setup -a 'filter=ansible_distribution*'
- Uncomment area in
templates/index.html.j2
- Re-run ansible-playbook and visit static website
<!-- remove line
{% if ansible_distribution is defined %}
<p>
System facts: {{ ansible_distribution }} {{ ansible_distribution_major_version }} {{ ansible_architecture }}
</p>
{% endif %}
--> remove line
- Edit
static-site.yml
as follows - Re-run ansible-playbook and visit static website
- name: Set up static website with nginx
hosts: myserver
become: true
gather_facts: false
tasks:
.
.
- Variables passed to ansible or ansible-playbook via command line
-
ansible-playbook --extra-vars="key1=value1 key2=value2" playbook.yml
-
ansible-playbook -e key1=value1 -e key2=value2 playbook.yml
-
ansible-playbook -e @extra-variables.yml playbook.yml
-
- Ansible variables in order of increasing precedence:
- inventory file
- host facts
- play vars_files
- task vars
- include_vars
- set_facts/registered variables
- extra vars
- [complete list](https://docs.ansible.com/ansible/latest/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)
- Pass extra variables to ansible playbook to override previous examples
- For example you can create a new file called
override-vars.yml
person_name: Marge Simpson lesson: name: Override variables session: 2
-
Then add this on the command line
ansible-playbook static-site.yml -e @override-vars.yml
- There are several types of variables in Ansible
- inventory
- play scoped
- task variables
- extra