Create a temp directory with ansible on your local computer

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.

Experiment with more file modules

http://docs.ansible.com/ansible/list_of_files_modules.html

Setting up ubuntu linux for ansible development

Using ansible on ubuntu for the first time

The first task I was shown when following ansible tutorials was editing the hosts file. I ran into a few issues though, hence this post. The solutions I found that helped are documented below:

Install ansible

sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible

Backups the hosts file

sudo mv /etc/ansible/hosts /etc/ansible/hosts.backup

Edit the hosts  file.

sudo nano hosts

Add your environments and ip addresses, eg:

[local]
127.0.0.1

Get ssh working, and test it can connect to your listed servers

add-username-to-transactions
sudo apt-get install openssh-server

Then test it is working and started:

sudo service ssh start

Run the ansible ping command

ansible all -m ping

Errors

If you get these errors:

Error: Permission denied

Two solutions,

Run the following command with -k

ansible all -m ping -k

This will trigger the password request

OR

Force ask password through the ansible config file:

sudo nano /etc/ansible/ansible.cfg
uncomment:
ask_pass = True

This will also trigger the password request, but by default without needing the -k option.

Then insert your password. You should now get your pong result without the error.