Creating our Ansible repository

Our first goal with Ansible is to be able to run commands on remote hosts. In order to do that efficiently, we can configure our local environment. Because we don't want to have to redo those steps time and time again and because ultimately we want to source control everything, we will create a new Git repository. To do that, we will repeat the same steps as when we created our EffectiveDevOpsTemplate repository.

Once logged in to GitHub, create a new repository for the CloudFormation template:

  1. In your browser, open https://github.com/new.
  2. Call the new repository Ansible.
  3. Check the checkbox Initialize this repository with a README
  4. Finally, click the Create repository button.

 

  1. Once your repository is created, clone it into your computer:
$ git clone https://github.com/<your_github_username>/ansible 
  1. Now that the repository is cloned, we will go into the repository and copy the template previously created in the new GitHub repository:
$ cd ansible  

At its base, Ansible is a tool that can run commands remotely on the hosts in your inventory. The inventory can be managed manually by creating an INI-like file where you list all your hosts and IPs, or dynamically if it can query an API. As you can imagine, Ansible is perfectly capable of taking advantage of the AWS API to fetch our inventory. To do so, we will download a Python script from the official Ansible Git repository and give the execution permissions:

$ curl -Lo ec2.py http://bit.ly/2v4SwE5
$ chmod +x ec2.py  

Before we can start testing this Python script, we also need to provide a configuration for it.

Create a new file in the same directory and call it ec2.ini.

In it, we will put the following configuration:

[ec2] 
regions = all 
regions_exclude = us-gov-west-1,cn-north-1 
destination_variable = public_dns_name 
vpc_destination_variable = ip_address 
route53 = False 
cache_path = ~/.ansible/tmp 
cache_max_age = 300 
rds = False 

Once this is done, you can finally validate that the inventory is in a working state by executing the ec2.py script:

$ ./ec2.py  

This command should return a big, nested JSON of the different resources found on your AWS account. Among those is the public IP address of the EC2 instance we created in the previous section.

The last step in our bootstrapping is to configure Ansible itself such that it knows how to get the inventory of our infrastructure, which user to use when it tries to SSH into our instances, how to become root, and so on.

We will create a new file in the same location and call it ansible.cfg.

Its content should be as follows:

[defaults] 
inventory      = ./ec2.py 
remote_user    = ec2-user 
become         = True 
become_method  = sudo 
become_user    = root 
nocows         = 1  

At that point, we are ready to start running Ansible commands.

Ansible has a few commands and some simple concepts. We will first look at the ansible command and the concept of modules.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset