Installing and Configuring Ansible on Centos

I’m going to be doing a few posts over the coming weeks around using Ansible with AWS. As a starting point this is a quick post to cover getting Ansible up and running on Centos, with the aim of using it to later provision some AWS services. Here, I will run though the steps to get Ansible installed and run a quick connectivity test to ensure it’s working as expected. For detailed information on Ansible, check out the Ansible site.

Installing Ansible on Centos 7

I will be using a Centos 7 virtual machine to run Ansible. I’ve used a minimal Centos build – before installing Ansible it will be necessary to install some additional software that Ansible depends on. We’ll be installing some Python tools and libraries, then downloading and compiling the latest Ansible release.

To start, we need to add the EPEL repository:

# yum install epel-release

Once done, we can use YUM to install the following:

# yum install gcc git python-pip python-devel libffi-devel openssl-devel

With that done, we want to use pip to install setup tools:

# pip install setuptools --upgrade


That’s it for the per-requiste software. We can now go ahead and download the latest Ansible build from the Ansible page on github, then install the package:

# git clone https://github.com/ansible/ansible.git --recursive
# cd ansible
# make install

This step will take a few minutes to run through – just enough time to grab a coffee. Once the process has finished you should see something like:

We can check it’s installed correctly by running the ‘ansible’ command:

[root@centos02 ansible]# ansible
Usage: ansible  [options]

Define and run a single task 'playbook' against a set of hosts

Options:
  -a MODULE_ARGS, --args=MODULE_ARGS
                        module arguments
  --ask-vault-pass      ask for vault password
  -B SECONDS, --background=SECONDS
                        run asynchronously, failing after X seconds
                        (default=N/A)
  -C, --check           don't make any changes; instead, try to predict some
                        of the changes that may occur
  -D, --diff            when changing (small) files and templates, show the
                        differences in those files; works great with --check
ERROR! Missing target hosts

If all is good, ansible will display some usage help, and an error stating that there are missing target hosts. This is expected – we can move on to doing a proper test.

Testing Ansible Configuration

So, at this point we have a working install of Ansible. I want to finish this post by doing a basic connectivity test, which I will build upon in future posts. As mentioned above, when running Ansible without giving it any arguments it will display an error due to there being no target hosts specified:

Usually we would define a hosts file, detailing the hosts/targets that Ansible will communicate with. However, as this is a quick test I’ll pass Ansible the IP address of a target host as an argument from the command line. Before doing so, there is one last step we need to carry out. Ansible communicates with Linux target hosts via SSH. Rather than having to specify credentials each time, we can set up SSH key authentication.

To do so, generate a key on the Ansible machine by running:

# ssh-keygen

This will generate a new public/private rsa key pair:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):

We then need to add the public key to the target host’s ~/.ssh/authorized_keys file. You can simply display (e.g. # cat id_rsa.pub) the public key on the Ansible machine then paste it into the authorized_keys file on the target. Detailed instructions on doing this can be found here. Once done, you should be able to open an SSH session to the target machine without having to specify any credentials.

Finally, we’re ready to test Ansible. To do so we’ll ask Ansible to ‘ping’ the target host. This will verify that Ansible can communicate with the target host. To do so:

# ansible all -i 192.168.144.138, -m ping

This command is running Ansible, using the ping module only, against a single host which has been passed though as part of the command. If all is well, the command should result in something like:

This confirms that Ansible has been able to successfully communicate with the target host. This is all I want to cover here – next time I’ll have a look at using some examples of using Ansible to automate some tasks against a Linux target, before moving on to looking at Ansible with AWS.

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More