Configuring a vm with Ansible
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.
Create root folder,
cspell:disable >inventoryandroles:cspell:enable >Terminal window mkdir tuto-ansiblemkdir tuto-ansible/inventorymkdir tuto-ansible/rolesWe can also create the right
cspell:disable >layoutfor the roles by hand, but instead, let’s use this command which will do it more completely:cspell:enable >Terminal window ansible-galaxy init tuto-ansible/roles/dockerThe result will look at something like this :
cspell:disable >cspell:enable >Terminal window roles/docker/tasks/main.ymlhandlers/main.ymltemplates/main.ymlfiles/main.ymlvars/main.ymldefaults/main.ymlmeta/main.yml
Playbook & Tasks
Now that the folders have been created, we’ll move on to creating the
cspell:disable >tasksandplaybook. Let’s take the firsttaskas an example:cspell:enable >- name: install prerequisites # task nameansible.builtin.apt: # the function to be used by the task, in this case 'aptname: "{{ item }}" # here we've used a loop to deploy the various packagesupdate_cache: trueloop: "{{ pre_install_packages }}" # this is where the value of the "{{ item }}" seen above will come frombecome: true # option to switch to root modeRegarding
cspell:disable >"{{ pre_install_packages }}", this is avariablefound inroles/docker/defaults/main.yml:cspell:enable >pre_install_packages:- apt-transport-https- ca-certificates- curl- gnupg-agent- software-properties-commonNote that it’s good practice to use
variablesrather than hard-coding everything intasks, as this makes it easier to modify afterwards.Open the
cspell:disable >roles/docker/tasks/main.ymlfile and add :cspell:enable ># install prerequisites- name : install prerequisitesansible.builtin.apt :name : "{{ item }}"update_cache : trueloop : "{{ pre_install_packages }}"become : true# add gpg-key- name : add gpg-keyansible.builtin.apt_key :url : "{{ gpg_key_url }}"become : true# add docker repo- name : add docker repoansible.builtin.apt_repository :repo : deb "{{ docker_url }}" focal stablebecome : true#install docker- name : install dockeransible.builtin.apt :name : "{{ item }}"loop : "{{ main_pkg }}"become : trueAdd the
cspell:disable >variablesto theroles/docker/defaults/main.ymlfile:cspell:enable >---# default file tasks/main.ymlgpg_key_url: https://download.docker.com/linux/ubuntu/gpgdocker_url: https://download.docker.com/linux/ubuntupre_install_packages:- apt-transport-https- ca-certificates- curl- gnupg-agent- software-properties-commonmain_pkg:- docker-ce- docker-ce-cli- docker-compose- containerd.ioIn the
cspell:disable >inventoryfolder, create aninventory.inifile. This is where we will define how and which vm we want to reach:cspell:enable >[vm]<ip-address-of-the-vm> ansible_connection=ssh ansible_user=azureadminFinally, in the root folder, create a
cspell:disable >playbook.ymlfile. This will launch our task:cspell:enable >---- name: Docker # playbook namehosts: 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
To apply our
cspell:disable >playbook, simply run the following command, in the root folder:cspell:enable >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.