A
u
Belaid Youba Hadj Arab, Etienne Deneuve
Jun 4, 2023 · 4 min read

Configuring a vm with Ansible

Side view worker wearing gloves

We’re going to configure the virtual machine we deployed earlier, and for that we’re going to use Ansible.

Ansible

In the first part, we installed Ansible, now we’re going to use it to deploy Docker on our vm.

You should know that there are good practices regarding the layout of the folder that will contain your Ansible playbooks. A quick look here will tell you more. In our example, we’ll settle for the bare minimum.

  1. Create root folder, inventory and roles :

    Terminal window
    mkdir tuto-ansible
    mkdir tuto-ansible/inventory
    mkdir tuto-ansible/roles
  2. We can also create the right layout for the roles by hand, but instead, let’s use this command which will do it more completely:

    Terminal window
    ansible-galaxy init tuto-ansible/roles/docker
  3. The result will look at something like this :

    Terminal window
    roles/
    docker/
    tasks/
    main.yml
    handlers/
    main.yml
    templates/
    main.yml
    files/
    main.yml
    vars/
    main.yml
    defaults/
    main.yml
    meta/
    main.yml

Playbook & Tasks

  1. Now that the folders have been created, we’ll move on to creating the tasks and playbook. Let’s take the first task as an example:

    - name: install prerequisites # task name
    ansible.builtin.apt: # the function to be used by the task, in this case 'apt
    name: "{{ item }}" # here we've used a loop to deploy the various packages
    update_cache: true
    loop: "{{ pre_install_packages }}" # this is where the value of the "{{ item }}" seen above will come from
    become: true # option to switch to root mode
  2. Regarding "{{ pre_install_packages }}", this is a variable found in roles/docker/defaults/main.yml :

    pre_install_packages:
    - apt-transport-https
    - ca-certificates
    - curl
    - gnupg-agent
    - software-properties-common

    Note that it’s good practice to use variables rather than hard-coding everything in tasks, as this makes it easier to modify afterwards.

  3. Open the roles/docker/tasks/main.yml file and add :

    # install prerequisites
    - name : install prerequisites
    ansible.builtin.apt :
    name : "{{ item }}"
    update_cache : true
    loop : "{{ pre_install_packages }}"
    become : true
    # add gpg-key
    - name : add gpg-key
    ansible.builtin.apt_key :
    url : "{{ gpg_key_url }}"
    become : true
    # add docker repo
    - name : add docker repo
    ansible.builtin.apt_repository :
    repo : deb "{{ docker_url }}" focal stable
    become : true
    #install docker
    - name : install docker
    ansible.builtin.apt :
    name : "{{ item }}"
    loop : "{{ main_pkg }}"
    become : true
  4. Add the variables to the roles/docker/defaults/main.yml file:

    ---
    # default file tasks/main.yml
    gpg_key_url: https://download.docker.com/linux/ubuntu/gpg
    docker_url: https://download.docker.com/linux/ubuntu
    pre_install_packages:
    - apt-transport-https
    - ca-certificates
    - curl
    - gnupg-agent
    - software-properties-common
    main_pkg:
    - docker-ce
    - docker-ce-cli
    - docker-compose
    - containerd.io
  5. In the inventory folder, create an inventory.ini file. This is where we will define how and which vm we want to reach:

    [vm]
    <ip-address-of-the-vm> ansible_connection=ssh ansible_user=azureadmin
  6. Finally, in the root folder, create a playbook.yml file. This will launch our task:

    ---
    - name: Docker # playbook name
    hosts: vm # the host on which we'll apply the playbook, in this case vm (as in inventory.ini)
    roles:
    - docker # the name of the role we want to use

Deployment

  1. To apply our playbook, simply run the following command, in the root folder:

    Terminal window
    ansible-playbook -i inventory/inventory.ini playbook/yml --private-key=@/.ssh/vm_id_rsa -v

Conclusion

Here you have a virtual machine created via Terraform, with Docker deployed on it via Ansible. The Terraform + Ansible combo is very practical, offering a wide range of possibilities and freedom to manage resource deployment and configuration.

Ansible Azure Docker

Related articles