Ansible Tutorial: Doing Deployments

ansible tutorial
ansible tutorial

Ansible is an open-source software provisioning, configuration management, and application deployment tool created by Michael DeHaan widely used in the Devops universe; many people like it for its simplicity of learning and the fact that it manages everything itself (without any agent unlike tools like Puppet or Chef). You will discover ansible with this tutorial.

There are many applications to automate deployments but today we will see the last born of this kind of tool which is really very easy to set up.

 

Ansible Tutorial: Installation of Ansible

As for all my tutorials, I will purpose this one on an Ubuntu 16.04 but you can easily find the installation methods on other installation systems.

sudo apt-get -y install ansible

Unlike other deployment tools, Ansible will attack the servers itself in SSH. However to do this, we will create a public / private key locally and send the public key to the server on which we want to allow Ansible to intervene:

ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.100

Now, we will insert our server in the /etc/ansible/hosts file like this:

[web]
192.168.0.100

The [web] is not mandatory but allows to create or group of servers and directly call web in future configurations.

Now that everything is installed, we will test that everything is ok by running the following method:

 

 ansible all -m ping -u root

If it’s ok, you will have in return:

192.168.0.100 | SUCCESS => {
     "changed": false,
     "ping": "pong"
}

Our first deployment with Ansible

Now that our Ansible is ready, we will take the opportunity to create our first deployment.

To create our first deployment, we will create a playbook that is a file in the Yaml format that Symfony developers for example know very well. We will install Git and Apache like this in a test.yml file:

 

- name: Installation de Symfony
  hosts: web 
  remote_user: root 
  tasks: 
    - name: Installer Git
    apt: name=git state=latest
    - name : Installer Symfony
    command: chdir=/var/www/ git clone https://github.com/symfony/symfony.git

As you can see, the syntax is relatively simple to understand; on the other hand indeed, an apprenticeship will be essential to master Ansible. However, in the face of competition, it is really easy and some things will even become intuitive.

Here is the explanation of each line written above:

name: it’s a label that will appear when you launch your deployment script. It will be useful to follow the good progress (or not) of your script.

hosts: you put the host, IP, or shortcut you specified in the /etc/ansible/hosts file (which can contain multiple servers).

remote_user: the user that will be used on the server. In general the one you used for your SSH key.

tasks: it is the definition of a task to perform that will be broken down into several possible functions. In our case, we will do two: a first to install Git and a second to clone the project Symfony.

apt: this is the shortcut to make the apt-get (package manager Ubuntu / Debian).

command: this is the method to make a more classic order. However, everything will not be feasible so you will sometimes have to look for specifics to apply your orders. Here for example, the chdir allows to launch the order in a very precise file.

To launch our script, we will launch the following command:

 

ansible-playbook test.yml

You will see the script unpick the “task” and tell you if everything is ok or if it encounters an error.

Complex installation of composer

As some people know, Symfony needs to use composer (package manager in PHP) to fully install. When you manipulate Ansible for the first time, it’s quite complex to create your deployment file, but with a good Google search like “composer install with ansible”, you should find all the help you need.

Here is the method to do it with Ansible to put following your “task”:

 

    - name: download composer
      get_url:
        url: https://getcomposer.org/installer
        dest: /tmp/installer
      tags: composer

    - name: install composer
      shell: cat /tmp/installer | php -- --install-dir=/usr/local/bin
      args:
        creates: /usr/local/bin/composer
      tags: composer

    - name: rename composer.phar in composer
      shell: mv /usr/local/bin/composer.phar /usr/local/bin/composer
      args:
        creates: /usr/local/bin/composer
      tags: composer

    - name: to put composer in executable
      file: 
        path: /usr/local/bin/composer
        mode: a+x
        state: file
      tags: composer

If you are familiar with the Linux world, you should understand these lines very quickly. These are other useful examples for creating deployment scripts.

Launch composer with conditions and variable

Now, we will start composer by using a variable and putting a condition in place (to do a clean job).

As a reminder, the first time, it’s necessary to make a “composer Install” which will also create a composer.lock file. Then, it will be necessary to make the composer update to update the packages.

 

Here is what we will have in Ansible:

    - stat: path=/var/www/symfony/composer.lock
      register: composer_file

    - name: composer install symfony
      command: chdir=/var/www/symfony composer install
      when: composer_file.stat.exists == False

    - name: composer update symfony
      command: chdir=/var/www/symfony composer update
      when: composer_file.stat.exists == True
As you can see, the “when” creates a condition that checks whether the composer.lock file exists or not. The register is used to create a reusable variable.

Install our environment

We saw everything for Symfony but it would be nice to install our environment. We will also take the opportunity to see a variable of type list {{ item }} which will refer to the items defined below:
 - name: Installation du package
      apt: name={{ item }} state=latest
      with_items:
        - apache2
        - libapache2-mod-php7.0
        - php7.0
        - php7.0-fpm
        - php7.0-mysql
        - php7.0-curl
        - curl
        - php7.0-mbstring
        - php7.0-xml
        - php7.0-zip
        - php7.0-cli
        - php7.0-bcmath

    - name: enabled mod_rewrite
      apache2_module: name=rewrite state=present
      notify:
        - restart apache2
We take this opportunity to activate the mod rewrite of Apache 2 which is not activated by default on Ubuntu and to notify Apache of a restart to take into account this activation.

Conclusion Ansible tutorial

Feel free to have fun with what we just showed you in this Ansible tutorial. If the subject really interests you, I would not hesitate to remake a second tutorial on its subject.

Has this Ansible tutorial interested you? I can do other Ansible tutorials if you ask me.

(Visited 22 times, 1 visits today)
About Judicaël Paquet 121 Articles
Judicaël Paquet (agile coach and senior devops) My activities in France and Switzerland: - agile transformation architect - customized agile training - sensitization and manager coaching - agile maturity audits and situations - agile coaching (teams, orga, product owner, scrum master, agile coach) Specialties: scrum, kanban, management 3.0, scalability, lean startup, agile method.

Be the first to comment

Leave a Reply

Your email address will not be published.


*