Getting Started with Ansible managing CentOS on Digital Ocean

In this example we'll take a freshly installed Linux box running CentOS 7.4 on a
Digital Ocean droplet and use Ansible to make basic configurations.

I assume that you already have Ansible installed on your desktop/notebook, whatever machine is
in front of you. If not, check out the linked article.

Create the host machine on Digital Ocean

For this article I've created the host machine manually.

Visit Digital Ocean, sign up if you don't have an account yet. (Using that link you are supposed to get $10 credit).

If you have not done so yet, Create an ssh keypair and upload the public key under the "SSH keys" section of the Security of your Profile. This will be useful as this will allow you to connect to the remote host without providing a password.

Now, in order to create your first Droplet, visit the list of droplets and click on the "Create" button and select "Droplets".

Select CentOS 7.4 64 bit as that's what we are checking out now. For size select the smallest. Even that is way to big for our needs.

For datacenter select whatever you like. I usually just select New York 1.

"Select additional options" can be left alone for now.

At "Add your SSH keys" you should see the SSH key you added earlier. Select the checkbox next to it.

Make sure you are asking for 1 Droplet.

The hostname can be anything now. Even the one they offer.

Click "Create".

After about 30 second your Droplet will be ready.

Hover over the IP address and a link copy will appear. Click on that to get the IP address in your clipboard.
If you can paste from there in your editor then you can save some extra work.

Create a file called inventory.cfg with the following content, just use the IP address of your Droplet instead of mine:

examples/ansible/centos_first/inventory.cfg

[all]
159.65.236.52

The "inventory file" of Ansible is a single configuration file that holds all the hosts you'd like to manage. The hosts can be grouped in various ways, but now as we only have one machine we created a group called "all". and put the IP address of the machine in that group.

Check if Ansible can access the hosts using Ping

Let's verify that Ansible can access the machine.
Just as with network you'd use the ping command, Ansible also provides a command called "ping" that checks if the remote machine is accessible to Ansible. Instead of sending ICMP packets, the ping of Ansible will try to log in to the remote machine using standard SSH.

Run the following command:

$ ansible -i inventory.cfg all -u root -m ping

It will first ask you to check the authenticity of the host by displaying a message like this:

The authenticity of host '159.65.236.52 (159.65.236.52)' can't be established.
ECDSA key fingerprint is SHA256:5G1v0wAEaqgQVfXrrqYlp4kRFTLJc263H3CvcqUEnlg.
Are you sure you want to continue connecting (yes/no)?

If you type in yes then it will add the signature of the host to
~/.ssh/known_hosts and it won't bother you again with the same question.

If you don't want to check the authenticity of the host you can tell ansible to tell the ssh command using the --ssh-common-args flag to not check the authenticity and to not save the signature in the known_hosts file.

authenticity of the host

For the upcoming command I'll assume that when you ran the previous command you ran it without the special parameter for ssh
and then you typed "yes" when the ssh client wanted to save the signature in the "~/.ssh/known_hosts" file. If not run this again:

$ ansible -i inventory.cfg all -u root -m ping

The next time you run the above command it should work without asking any further questions.

Collect information from the remote server

Ansible has plenty of modules, but it also allows you to execute arbitrary command on the remote machine:

Comments

Author: Gabor Szabo

Gabor who runs the Code Maven site helps companies set up test automation, CI/CD
Continuous Integration and Continuous Deployment and other DevOps related
systems. Gabor can help your team improve the development speed and reduce the risk of bugs.