Cover image for DevOps, Vagrant with Ansible

DevOps, Vagrant with Ansible

alialp profile image Ali Alp Updated on ・3 min read

Do you want to automate the setting up of your production/test servers? so keep reading.

All resources are in Github at alicommit-malp/ansible-examples


you need to install Vagrant ver >= 1.7.0 on your system from here

git clone https://github.com/alicommit-malp/ansible-examples
cd ansible-examples/vm-docker-mariadb
sudo vagrant up --provision

//password is alialp

//test your connection to the mariaDb from localhost
telnet 33306

Use case

  • creating a virtual machine, any distribution or version
    • in our case CentOs7
  • installing the docker daemon and docker client
  • running your desire docker image as a container
    • in our case MariaDb version 10.2.14

we will use Vagrant to create our virtual machine , you can install it from here for your desired operation system.

then we will ask the Vagrant to make a VM for us using the centOs7 image and map the localhost port 33306 to the VM's port of 3306, futhermore to assign as the ip address of the VM

config.vm.box = "generic/centos7"
config.vm.network :private_network, ip: ""
config.vm.network "forwarded_port", guest: 3306, host: 33306

Vagrant has a smooth integration with Ansible, therefore we can directly use Ansible to provision the freshly created CentOs7 VM as we demand, by passing the name of the playbook file path to it.

ansible.playbook = "mariaDb-docker-playbook.yml"

therefore we will end up with a Vagrantfile like this

Vagrant.require_version ">= 1.7.0"

Vagrant.configure(2) do |config|

  config.vm.hostname = "maria_host"
  config.vm.box = "generic/centos7"

  config.vm.network :private_network, ip: ""
  config.vm.network "forwarded_port", guest: 3306, host: 33306

  config.ssh.insert_key = false

  config.vm.provision "ansible" do |ansible|
    ansible.verbose = "v"
    ansible.raw_arguments  = "--ask-vault-pass"
    ansible.playbook = "mariaDb-docker-playbook.yml"

and we will run it like this

sudo vagrant up --provision

be aware that the --provision parameter will apply any changes to the VM if there is therefore after one time running the mentioned command it will create the VM and provision it with Ansible playbook and if you make changes afterwards to the playbook file and run the same command Vagrant will apply the changes.

for the sake of our example we are using a playbook file like this

- name: provision_mariadb_on_docker_in_vm
  hosts: all
    - vars.yml
  become: true


    - name: Installing docker related dependencies
        name: "{{ item }}"
        state: latest
        - yum-utils
        - device-mapper-persistent-data
        - lvm2
        - python-pip

    - name: Configuring docker-ce repo
        url: https://download.docker.com/linux/centos/docker-ce.repo
        dest: /etc/yum.repos.d/docker-ce.repo
        mode: 0644

    - name: Installing docker
        name: "{{ item }}"
        state: latest
        - docker-ce
        - docker-ce-cli 
        - containerd.io

    - name: Starting and Enabling Docker service
        name: docker
        state: started
        enabled: yes

    - name: Install docker python package 
        name: docker 

    - name: Setting up the mariaDB container 
        image: mariadb:10.2.14
        name: mariadb
        state: started
        restart: yes
        - "33306:3306"
          MYSQL_ROOT_PASSWORD: "{{mariadb_root_password}}"
          MYSQL_PASSWORD: "{{mariadb_password}}"
          MYSQL_USER: "{{mariadb_username}}"

as it can be seen above, the Ansible file is very straight forward, the only point which i need to make here is to explain a bit about the vars.yml file because if you look inside of it you will see something like this


yes you have guessed correctly, it has been encrypted, and this is another fantastic feature of the Ansible, despite of the fact that I have shared the repository with you but still you can not figure out what is the value of the MYSQL_PASSWORD for instance

MYSQL_ROOT_PASSWORD: "{{mariadb_root_password}}"
MYSQL_PASSWORD: "{{mariadb_password}}"
MYSQL_USER: "{{mariadb_username}}"

but the ansible can figure the values out in runtime.

Ansible Vault

with ansible valut you can encrypt the variable files with password so when you are provisioning the server you need to provide that password or you can use a password file if you wish to automate this part as well.

you can test the connection to the MariaDb living inside the container which is living inside the VM like this

❯ telnet 33306
Connected to
Escape character is '^]'.

All resources are in Github at alicommit-malp/ansible-examples

Happy coding :)

Posted on by:

alialp profile

Ali Alp


“Take it easy” is nonsense , take it as hard as you can and don’t let it go :)


markdown guide