I have a linux environment set up, and I have ansible installed. To see how to setup ansible from scratch follow this blog entry:
Setting up ubuntu linux for ansible development
Some tips that helped me while I was researching ansible
(I’m still learning so this may or may not be right, use it more as stepping stones to progress your own knowledge)
Yml
Ansible makes use of .yml files. Pronounced “yeah-mil”. These files are indent sensitive and you seperate commands be new lines and indents. If you are unfamiliar with yml structures I suggest googling a bit for more information on yml files are structured.
Hosts
There is a global ansible hosts file.
/etc/ansible/hosts
However I always recommend against editing global files. Setup a local inventory file rather.
In your hosts files you declare host level variables that indicate the addresses/locations you would like to access and alter from.
Playbooks
A playbook defines individual plays to be executed in a predefined order
Play
A play is a single task or action to be execute
Modules
A play can use modules to achieve tasks or actions
Lets get started with a simple ansible script to create a directory
Go to a new directory where you would like to create the ansible script and create a folder called “practice”.
In this folder create a yml file called “test.yml”.
Note you can name the yml file anything you like, I’ve just chosen test for this example.
Then create an inventory folder for you hosts file. In the inventory folder add a “hosts” file (no extension).
1 2 3 4 | practice/ inventory/ hosts test.yml |
Add hosts
In your “hosts” file add the following:
1 2 | [local] localhost ansible_connection=local |
This indicates a variable “local” with the value localhost ansible_connection=local. This tells ansible that you are working on your local machine.
Point to local in test.yml
Add these lines to your test.yml file in order to run your first ansible command.
1 2 | - hosts: - local |
You can check that ansible picks up this configuration by running:
1 | <p class="p1">ansible-playbook -i inventory/hosts test.yml --list-hosts</p> |
And you should get the response:
1 2 | hosts (1): localhost |
The -i refers to:
-i INVENTORY, –inventory-file=INVENTORY
specify inventory host path
(default=/etc/ansible/hosts) or comma separated host
list.
Then the inventory/hosts indicates the “hosts” file you created in the inventory directory.
The all indicates that all the listed hosts will be displayed. You could also say:
1 | ansible-playbook -i inventory/hosts --limit localhost test.yml --list-hosts |
And that would filter to just the “local” section. In this case it would return the same result:
1 2 | hosts (1): localhost |
The playbook
Lets start adding a task. Open your test.yml file in you practice folder, and add the following lines:
1 2 | [local] localhost ansible_connection=local |
This indicates the environment you wish to alter. Run the playbook:
1 | ansible-playbook test.yml -i inventory/hosts |
If you get this message:
skipping: no hosts matched
Just make sure you indicated the “-i inventory/hosts” part.
Add tasks
Now we will begin adding the task of creating a new folder. We will be making use of an ansible module called “file”.
Add the following code to your test.yml file:
1 2 3 4 | tasks: - name: create a dir called temp file: path=temp state=directory mode=0755 |
“name” is referring to the name of the task, this will display in the response on the command line
“file” is the module you are using, and then path, state and mode are configuration values you set for the task
You should now have in your test.yml file:
1 2 3 4 5 | - hosts: - local tasks: - name: create a dir called temp file: path=temp state=directory mode=0755 |
Run your ansible playbook
1 | ansible-playbook test.yml -i inventory/hosts |
You should now see a temp folder inside your practice folder you created.
And that’s it. As simple as that.