Contents

    Guides

    Infrastructure as code with Ansible

    Somtochukwu Uchegbu

    Somtochukwu is a Technical Writer who enjoys writing technical articles that explain technical concepts.

    Published on

    August 30, 2022
    Infrastructure as code with Ansible

    When managing our applications, part of the essential things we consider is the infrastructure, meaning, and resources that we need to help us handle the scaling of our applications. In recent times, people have tended to move to cloud infrastructure, that is they deploy their applications to the cloud, leaving the provision of hardware and computational resources to the cloud providers to handle.

    In this article, we will focus on how we can use code to optimize the provisioning of our infrastructure in the cloud. Let’s get right into it.

    What is Infrastructure as Code?

    Infrastructure as Code can be described as the automation of infrastructure needed for the end-to-end deployment of an application.

    It is also referred to as the technique that is used to define your infrastructure provisioning and configuration the same way you handle application code.

    Why is Infrastructure as Code Important

    The problem IaC aims to solve is the reduction of time and effort needed to manage the deployment process of an application.

    For instance, you have an application that needs to scale up, and this application needs one hundred servers to run smoothly. Creating each server individually will be an inefficient approach, plus it will increase the time at which it will take your application to scale.

    Like every other introduced concept in tech. there are also the benefits that come with it. Here are some of the benefits of IaC.

    • Increase in deployment speed and efficiency: With IaC implemented, there will be an increase in software deployment and speed, because you only need to run the code that automates the infrastructure provisioning rather than provisioning and configuring these infrastructures personally.
    • Improved software development life cycle: Generally, adopting the concept of IaC leads to a better software development life cycle. One of the key metrics of SDLC is the reduction in production time, implementing IaC helps achieve this. IaC takes over the process of infrastructure management operations, which helps reduce the time spent in building products both in development and production environments,
    • Improved infrastructure management: IaC helps improve the overall structure of infrastructure management. It achieves this by
    • Reduction of human error: IaC reduces human errors. With code in place of humans handling the operations of infrastructure configuration and deployment. It reduces the errors humans are more likely to make.

    Examples of Infrastructure as Code Tools

    There are different tools for implementing infrastructure as code. These tools are used to carry out different tasks in infrastructure provisioning, management, and deployment.

    Here are some of the Infrastructure as Code we have.

    • Ansible
    • Terraform
    • Puppet
    • Pulumi
    • Chef
    • AWS CloudFormation, and more.

    Note: No one tool is used to handle the different aspects of infrastructure management, rather these tools are used together to facilitate much better infrastructure management.

    What is Ansible

    Ansible is an open-source infrastructure as a code tool supported by RedHat. it is used to handle multiple server configurations and management from one point. It helps automate the configuration of multiple servers at once, rather than connecting to an individual server and then carrying out your desired task.

    What makes Ansible Stand Out?

    Here are some points that make Ansible stand out from its alternatives (Chef, Puppet, Salt, etc.):

    • Ansible is often recognized as a simpler beginner-friendly tool
    • Its configuration files are written in YAML, which is easier to understand
    • It is lightweight and does not require many computing resources
    • Compared to its alternatives, to make changes on other servers, with Ansible you only need to SSH into those servers to make changes.

    Writing your first Ansible Script

    In this section, we are going to go over how to install and set up an Ansible on Linux, then we will write a script to automate the installation of Nginx on two of the servers we will provision.

    First of all, to get started, we need to provision two servers we are going to work with. To do that, we are going to need a cloud provider, we are going to use Linode.
    If you do not have an account already, head on to linode.com to create one. After creating an account, create two Linode instances.
    Now that our Linode instances have been created, we need to install Ansible on our local machine.
    To install Ansible on our Linux machine, we need to open our terminal and enter this command:

    sudo apt update 
    sudo apt install software-properties-common 
    sudo add-apt-repository --yes --update ppa:ansible/ansible 
    sudo apt install ansible

    To check if Ansible has been successfully installed, enter this command into your terminal

    ansible --version

    It should return the version of Ansible you have installed, if not, Ansible was not successfully installed.

    Next, we need to change our current directory to /etc/ansible. In this directory, we should see two files and one folder in there, namely:

    • ansible.cfg
    • hosts
    • roles

    Next, we need to edit the host file. The host file is also known as our Inventory. We need to add our Linux servers’ IP addresses to the inventory, alongside their login credentials. That way, our Ansible script knows how to log in to our servers and make the configurations we need it to make.

    Now, while in our host file, let us add our Linux servers IP to a group. We will go down to the end of our file to make the changes like so:

    [linux] 
    
    root@139.177.201.49 
    root@172.105.157.21

    Next, we need to define our login credentials, so Ansible can log in to our servers. We can do this by adding this group below the linux we defined earlier.

    [linux:vars] 
    
    ansible_user=root 
    ansible_password=your_server_password

    Now, we save and exit the hosts file. To check if we have made a connection to our servers, we must enter this command into our terminal.

    ansible linux -m ping

    We should get a response like this.

    root@172.105.157.21 | SUCCESS => { 
         "ansible_facts": { 
              "discovered_interpreter_python": "/usr/bin/python3" 
         }, 
         "changed": false, "ping": "pong" 
    }
    root@139.177.201.49 | SUCCESS => { 
        "ansible_facts": { 
             "discovered_interpreter_python": "/usr/bin/python3" 
         },
         "changed": false, "ping": "pong" 
    }

    We can do more than ping our servers, we can also update the repositories on our servers with this command

    ansible linux -a "apt update"

    You can check out more ansible host flags in their documentation.

    Next, we will not write our first Ansible script, otherwise known as a Playbook.

    Ansible playbooks are the configuration files written in YAML, in which we specify what tasks we want to run on our servers.

    These playbooks contain plays or tasks which we define that will get executed on our servers.

    Let’s create our Ansible Playbook. Firstly, we need to create a YAML file called install_nginx.yml. In our terminal, we need to type this in:

    touch install_nginx.yml

    Now add this piece of code into the file...

    --- 
        - name: install_nginx 
          hosts: linux 
          tasks: 
             - name: install the latest version of nginx 
               apt: 
                  name: nginx 
                  state: latest

    In our Playbook here, we are starting with 3 hyphens indicating that the type of file we are working with is a YAML file.

    Next, we are specifying our host as linux which we created in our hosts file earlier on, followed by our task.

    In our task, we are giving it the name install the latest version of nginx, followed by the module which we are using to perform our installation, here we using the apt module because our servers are running Ubuntu.

    Next, we specify the name of the package we want to install, which is nginx followed by the state of the package, which is latest in our case.

    Finally, save and close the file. To run our Ansible Playbook, all we have to do is run this command in our terminal.

    ansible-playbook install_nginx.yml

    To confirm our Playbook was executed without any errors, we should see this output:

    PLAY [install_nginx] *************************************************************************************************************************************************** 
    
    TASK [Gathering Facts] ************************************************************************************************************************************************* 
    ok: [root@172.105.157.21] 
    ok: [root@139.177.201.49] 
    
    TASK [install the latest version of nginx] ***************************************************************************************************************************** 
    changed: [root@172.105.157.21] 
    changed: [root@139.177.201.49] 
    
    PLAY RECAP ************************************************************************************************************************************************************* 
    root@172.105.157.21 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    root@139.177.201.49 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    There you have it, we just wrote our first Ansible Playbook. There are different ways to write Ansible Playbooks, we only focused on making our Playbook install nginx on our servers.

    Conclusion

    We focused on the key points surrounding Infrastructure as Code on this one. We also went through a brief introduction to Ansible and wrote our first Playbook.

    Thank you for sticking around till the end. Until next time, happy coding!

    Data-rich bug reports loved by everyone

    Get visual proof, steps to reproduce and technical logs with one click

    Make bug reporting 50% faster and 100% less painful

    Rating LogosStars
    4.6
    |
    Category leader

    Liked the article? Spread the word

    Put your knowledge to practice

    Try Bird on your next bug - you’ll love it

    “Game changer”

    Julie, Head of QA

    star-ratingstar-ratingstar-ratingstar-ratingstar-rating

    Overall rating: 4.7/5

    Try Bird later, from your desktop