[Ansible] – How to use variables?

Syntax variable names

Spaces, and dots and special characters as for example ($) are not valid syntaxes for the variable names.  (E.g  ‘my car’, ‘my.car’ or my$1). Instead of space use the underline (_) in the variables names. Also, numbers are accepted as part of the name. ( E.g my_car and var1 )

Scope Definition

It is possible defined variables valid for all which will be in the global scope and valid jus for the execution related to the ansible script structures as valid just for specific hosts. If the same variables are defined in many places/scopes than ansible will adopt the more specific one for the structure where each is used. For example, if a value is informed in the command line used to execute the script then it will be the value used.

Following the command line to overriding the value of variables

$ {…} -e “var=value”

Following an example of the above command.

$ ansible-playbook myplaybook.yml -e “service=httpd”

How to define the variables in the playbooks?

Use the vars section to defined them as the following example

- hosts: myserver.example.com
  vars:
    id: 400
    path: /etc/tmp/

Also, it is possible to define them in a file. Ansible has a specific name of dirs and a structure that can be used to define the variables. Following an example of how it will be set up in the playbook.

- hosts: myserver.example.com
  vars_files:
    - dir/vars.yml

How to use the variables in the playbooks?

The variables need to be used as a String type. Following an example.

- name: Check the {{ service }} is started and enabled
  service: 
    name: "{{ service }}"
    enabled: true 
    state: started

How to define variables for specific hosts?

It is possible to be done in the inventory file. Following some examples.

[servers]
# here the value of my_var will be applied in this host only
myserver.example.com  my_var=value
[servers]
servera.example.com
serverb.example.com

[servers:vars]
# Here the vars can be used for all servers
my_var=value

What is the best approach to defining variables?

The best approach is to create files to define them by key-pairs using the structure adopted by ansible. Ansible is implemented to looking for them in the directories group_vars and host_vars as described in the following an example.

project
├── ansible.cfg
├── group_vars
│   ├── users
│   ├── databases
│   └── packages
├── host_vars
│   ├── servera.example.com
│   ├── serverb.example.com   
├── inventory
└── playbook.yml

For further information see the Variables section in the Ansible docs.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s