Ansible Playbook for dynamically updating the inventory with the new container IP to deploy a webserver in it!

Krithika Sharma
5 min readJan 17, 2021

--

📋In this article, we will create an
Ansible playbook that will retrieve new Container IP
and update the inventory. So that further Configuration
of Webserver could be done inside that Container.

Ansible
Ansible is a popular open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intra-service orchestration and provisioning. It is both light weight and simple to deploy, manage and use. The Ansible platform makes it easy for administrators and developers to automate many tasks, including applying updates to machines on the network to managing devices on the network.

Ansible Platforms
Ansible has three offerings:
• Ansible: A free, open source automation product
• Ansible Tower: An enterprise offering which gives you a graphical interface and enables integration with other services and tools. Tower gives permission control and will also save a record of all Ansible playbook activity, useful for auditing purposes.

  • Ansible Galaxy: This refers to the Galaxy website where users can share roles, and to a command line tool for installing, creating and managing roles.

Usage

You can leverage the capabilities of Ansible in multiple ways:

• Ad-Hoc: Issue ansible tasks direct from the command line. This is a good place to start to understand the basics of what Ansible can do prior to learning the playbooks language — adhoc commands can also be used to do quick things that you might not necessarily want to write a full playbook for.

• Playbooks: These are automation scripts. Playbooks are Ansible’s configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process.

  • Automation Framework: Requires the Ansible Tower.
  • Check Mode: An option for running ad-hoc commands or playbooks without making changes. This deployment guide walks you through working with playbooks.

Key terms

• Playbook: A file used to set configurations and automate any manual processes. These are plain text files written in the Ansible automation language which describe the intended endstate of a deployment or task being executed.

• Ansible automation language: The structure used when writing playbooks and other resources for Ansible. The Ansible automation language uses YAML and is intended to be both human and machine readable.

  • Plays: Operations within a playbook and execute tasks.

• Tasks: Used within a play to call modules and run in order.

• Roles: A grouping of files and playbooks which makes it easier to organize complex operations.

• Inventory: A list of hosts, or servers, that the control machine can use in its orchestration tasks.

  • Host: In Ansible, a host is a remote machine that is assigned to individual variables and they are further grouped together. Each host has a dedicated name or unique IP address to make its identification easy and quick. They can be given simple port number too if you don’t have to access them over SSH connection.
  • Modules: Also referred to as ‘task plugins’ or ‘library plugins’, these control system resources and provide integrations with other services. Modules are the main components that help to install packages, allow APIs to interact together and plan actions for system files too. There are a variety of modules in Ansible that are programmed to automate almost everything inside the tool.
  • Plug-ins: They are the special pieces of code that help to write code quickly. Plug-ins automate the development tasks and help to speed up the deployment work to the maximum level.
  • Control machine: Also referred to as a ‘control node’. The system where you run your Ansible playbooks from.

Let’s get into the practicles:

Ansible playbook to install, launch and configure docker

  • vars_prompt: to prompt the user for certain input
  • yum_repository : to add or remove YUM repositories in RPM-based Linux distributions.
  • pip: to manage Python library dependencies
  • package: Installs, upgrade and removes packages using the underlying OS package manager.
  • service: Controls services on remote hosts.
  • docker_image: Build, load or pull an image, making the image available for creating containers.
  • docker_container: Manage the life cycle of docker containers.
  • debug: This module prints statements during execution and can be useful for debugging variables or expressions without necessarily halting the playbook.Useful for debugging together with the ‘when:’ directive.
  • blockinfile: This module will insert/update/remove a block of multi-line text surrounded by customizable marker lines.
retriving the ip dynamically and updating the inventory

let’s run the playbook:

this error is due to the attribute, which should be IPAddress( I gave IPaddress a small hence the error)

inventory getting dynamically updated as we used blockinfile module and retrived ip using ansible facts

We can see that our inventory is updated, the IP of our container is 172.17.0.3

output of the webpage, the same content which we gave in our playbook

Thankyou! keep learning! keep growing! keep sharing!

Krithika Sharma
If you enjoyed this, follow me on Medium for more
Let’s connect on LinkedIn

--

--