All Linux HowTo's

Ansible Variables – a 5 minute Intro

Share This:

Ansible supports “variables” just like any scripting language. Actually, Ansible uses the YAML format and YAML supports variables. Confused, don’t be. It’s simple. You don’t need to know YAML to use Ansible and i bet you’ve already got things working with Ansible enough that you’re ready to start expanding your automation processes and variables will help you do that.

As always we lead with examples. The only simple example i can give for using variables with Ansible is when creating multiple users.

The following instructs Ansible to make sure several users exists and are members of the “wheel” group. Put the following into a file called “playbook-variables.yml”:

---
- hosts: all

  sudo: yes

  tasks:

  - name: add or edit a few users and put them in the wheel group
    user: name={{ item }} state=present groups=wheel
    with_items:
       - ben
       - sally
       - kym

We create a file to be referred to by Ansible which lists the target server(s). We’ll call it “host.thisserver” file has the following contents:

[local]
localhost

The above means “this server” only. We could specify a list of just the server we want.

Now we run the Ansible command to create the users:

ansible-playbook playbook-variables.yml -i host.thisserver

Reference: http://docs.ansible.com/ansible/playbooks_variables.html

Leave a Reply

Your email address will not be published. Required fields are marked *