Getting Started With Ansible

Its easier than you think

Its easier than you think to get started with Ansible. Even if you don’t have an official development environment set up you can start to try out and see for your self how easy it is to get started with Ansible on your laptop within a few minutes.

For more information and projects on using Ansible in your day to day projects, check out the new book, Practical Ansible

The aim of this article is to give you a quick and dirty introduction into the why, what and how to of Ansible without getting you too bogged down with the details.

What is Ansible?

Just in case you are here because you have heard people preaching about Ansible and have no idea what it is, well basically it is an configuration management tool like Puppet or Chef. The reason why you would want an configuration management tool is to allow you to configure all of your servers and environments from one central location with code, without needing to access these environments and servers directly.

Installing Ansible in Windows

You may not have a choice in the matter and thankfully Windows has moved a long way to now provide users with a workable Linux Bash Shell which will work perfectly fine, if Windows is the only option you have in running and using Ansible. You can install and run Ansible using Cygwin, but I have found running it on the Ubuntu Bash Shell for Windows to be the best option. Go to the following link to get started and install the Bash Shell on your PC:

Once you have the Bash Shell installed you will then be able to run the rest of the commands below.

Now, Installing Ansible

If you are on Windows or Linux, all you need to do is open your Bash Shell and you will be able to run the following.

NOTE: Each command will be preceded with a hash(#) symbol

  • First update your package manager and install all the packages which will be needed by Ansible:
    # sudo apt-get update
    # sudo apt-get install python2.7 python-pip git libffi-dev libssl-dev -y
  • Now, using the pip command, install ansible:
    # pip install ansible
  • Just to verify we can also check the version we are running:
    # ansible — version
Output from ansible — version command

Basic Ansible Configurations

We don’t really need to do too much with configurations just yet. We will set up a hosts file in the directory we are working in to allow us to communicate by referring to our hosts by name.

  • In our current directory, create a hosts file with the vim command:
    # vim hosts
  • Now enter in the following configurations:
    I am working on a Windows laptop and this is why I have labelled the “windows” host as my localhost. I also have a Virtual Machine on my laptop running Linux which I am able to SSH to. I have labelled this as linux and placed the IP address below.

Running Basic Ansible Modules

Our configuration was pretty simply, but we can now use these configuration to run basic ansible commands. Ansible comes with a standard group of modules which allow you to perform almost anything:

Click here to see a list of all Ansible modules

For our example, we are going to use the “ping” module to verify we can access what we have entered in our hosts file:

  • To run the ansible command against windows:
    #ansible -i hosts — connection=local windows -m ping
    Here we have done the following:
    - run the ansible command
    - specify we are using the hosts file to locate our servers with the -i option
    - running the command locally with the — connection=local
    - we have specified we want to run this over the “windows” host. If we wanted to run it against the Linux host we would use “linux” as the option, or we could use “all”
    - lastly, we use the -m option to let Ansible know we want to use a module, in this case the ping module.
Calling the ping module successfully should show us a “pong”

Running Basic Commands

We aren’t limited by modules either. By using the -a option we can run commands as well. In the example below we are going to run a simple process command on our Linux host.

  • Already logged into our environment, we will run the following command:
    #ansible -i hosts — connection=local linux -a “ps aux”
    We have run a similar command to the example above, but in this case:
    - we have specified we want to run this over the “linux” host.
    - now instead of a module, we use the -a option to call the “ps aux” command, from the command line.
Using commands in Ansible

Running a Basic Playbook

Finally we will put together a very basic Playbook as an example to show you how they work. An Ansible Playbook is a group of modules which are run as a set of tasks in a certain order across a host.

  • To get started create file called “test.yml” in your current directory. This is a YAML format file and you need to ensure spacing is correct or the Playbook will fail.
    # vim test.yml
  • Now enter the following text to create the Playbook:
    — -
    - hosts: localhost
    — debug: msg=”Ansible is working”
  • Finally to run the playbook we run the following command:
    ansible-playbook test.yml -i hosts — connection=local
Running your first playbook

Summing up, I hope you can now start to see the power of Ansible and in future posts we will try to elaborate further on what you can achieve with the tool.

Found this post useful? Kindly tap the clap button below! :)

About The Author

Vince has worked with Splunk for over 5 years, developing apps and reporting applications around Splunk, and now works hard to advocate its success. He has worked as a system engineer in big data companies and development departments, where he has regularly supported, built, and developed with Splunk. He has now published his first book via Packt Publishing — Learning Splunk Web Framework.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vince Sesto

Vince Sesto

Vincent Sesto is a DevOps Engineer, Endurance Athlete, Coach and Author. One of his passion’s in life is endurance sports as both an athlete, coach and author.