Ansible — Beginning
I am using localhost for deployment, and I’ve noticed that it’s not as common. I felt frustrated, so I decided to share what I have learned so far. Hoping that what I share can be useful to someone.
Basic Commands
I am using localhost, therefore, I was getting error messages when I was trying to run basic Ansible commands. When I googled for error message, I couldn't find a solution.
Example:
Command to ping to all your nodes:
$ ansible all -m ping
Which should work fine, unless you are using localhost.
Solution is to use localhost instead of all:
$ ansible localhost -m ping
To contact your nodes, run:
$ ansible localhost -a "/bin/echo hello"
You can specify localhost explicitly by adding this to your inventory file:
localhost ansible_connection=local ansible_python_interpreter="/usr/bin/env python"
Now you are guessing, what is an inventory file and where to find it, right?
What is inventory file?
The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. The file can be in one of many formats depending on your Ansible environment and plugins. The default location for the inventory file is
/etc/ansible/hosts
. If necessary, you can also create project-specific inventory files in alternate locations.
Thanks to Juniper for such great & short explanation.
Second, to check if you have an inventory file already, run this:
$ cat /etc/ansible/hosts
If you see something similar to the above, congratulations! You have a default inventory file already. If you don’t have default inventory file, just simply create one. Just make sure that you are using the same path.
Now, in order to explicitly specify localhost, lets add it to our inventory file:
Fists, let’s start by opening the file:
$ vi /etc/ansible/hosts
It will look approximately like this. Next, let’s add this line at the very end and save the file:
localhost ansible_connection=local ansible_python_interpreter="/usr/bin/env python"
In vi editor, to save & exit, you need to press Esc key first, then enter colon ( : ), then type wq. However, for me that gave an error:
Then I tried to edit the file with sudo, but it didn’t work either. However, the error message was different. So I decided to find a way to modify the inventory file. However, while I was trying different things, I did something very bad.
People were suggesting to initialize new inventory file. Which I did, but it didn’t work for me, it actually caused even more problems. I decided to remove that file, that’s when I accidentaly removed the original inventory file.
Note to self:
Never ever in your life use rm command without -i option
Since, it’s impossible (or almost impossible ) to restore the file after rm command. I had no other choice but create a new inventory file:
$ sudo touch /etc/ansible/hosts
If I try to ping it now, I will get the following:
Then when I opened the file and paste the following line:
localhost ansible_connection=local ansible_python_interpreter="/usr/bin/env python"
This line ansible_connection=local is telling Ansible that the connection is local. By doing so, we are setting the local Ansible variable.
Save & exit.
Now let’s check if that worked. Run the following:
$ ansible localhost -m ping
Great!
Host Groups
Right now I have only one host. Eventually, I might have many of them. In order to make out life as simple as possible, we can create host groups. Also, apparently grouping he hosts is considered to be a common practice. Let’s create one!
We need to open our inventory file for writing, and add the following line above the existing one:
[local]
So that it should look something like this:
To check if it works, we need to run:
$ ansible local -m ping
Where local is the name of the group.
This command will run the module against all hosts inside the group. Right now we have only one host inside the group. We could also try to add another one and see if it works:
I know that this is kinda cheating haha, but for the sake of proof-of-concept it counts.
To see the list of hosts in the specific group, run:
$ ansible local --list-host
Now we can remove that line: