Ansible is an IT automation tool that brings together many components to specify what exactly needs to be done to the infrastructure. In dynamic environments, such as a cloud-hosted setup, Ansible variables give administrators organization and fine-grained control over host configurations.
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
The structure of Ansible -- modules, plays, playbooks, variables and facts -- is integral to how IT organizations maintain desired state and scale-up automation. In an Ansible configuration management setup, the control host is the management machine from which tasks execute on managed hosts. The set of instructions in a play defines the tasks to be accomplished, and plays are gathered in a playbook, which is the complete set of instructions to automate hosts. Modules enable tasks scheduled on managed hosts without requiring an administrator to consider every approach necessary to complete the tasks. Managed hosts are listed in the host inventory, and when a playbook is applied, it works on hosts within this inventory.
By organizing the inventory in a smart way, an Ansible administrator can easily decide where to execute specific tasks. It's common to define sections in the inventory file so that the user quickly identifies all hosts, as well as hosts with specific tasks, such as web or database servers. The host inventory can be provided in a static way or can be generated dynamically -- for instance, by using Lightweight Directory Access Protocol. Dynamic host inventory generation makes sense when workloads reside on infrastructure as a service or other frequently changing environments.
Ansible variables organize the rules
Ansible playbooks often manage rapidly changing environments, which makes variables important. Ansible variables allow playbooks to work with static code that is dynamically adapted to specific environments. Instead of including oft-changed values, such as usernames or IP addresses, directly in the playbook, the user refers to external variables.
Ansible variables address a range of scopes. A global variable applies to all managed hosts. Variables with a play scope apply to a play and related structures. The Ansible admin can apply variables to specific hosts as well.
Because of these layers of scope, the administrator should set an order of precedence for variables. The order of precedence is as-mentioned. If a global variable is specified on the command line while running a playbook, it will always precede the playbook-specific variable. This prioritization convention might seem counterintuitive to some administrators. And it doesn't stop there.
There is an exception to Ansible variable precedence: Variables that are included using the command include_vars take precedence over all others. Many administrators expect the variables specified as a command-line option to come first, and include_vars could create a lot of confusion when structuring an Ansible configuration.
Just the facts
The fact describes a specific type of variable that contains a discovered value about a specific host. The Ansible setup module discovers these values. With facts, Ansible administrators can apply plays only to specific hosts that match a specific value. An overview of all host facts can be generated by running the ansible some.host.com -m setup command against a managed host (some.host.com in this example). Inclusions put variables and facts into playbooks.
Ansible uses Jinja2, a templating language for Python, to apply code to specific hosts. Jinja2 templates modify files before they are distributed to hosts. They make it possible to work with specific control structures that apply code to hosts only after that code is modified.
Beginners sometimes struggle to get insight into the working of Ansible, with so many items interacting with each other. This web of relationships is what makes Ansible such a scalable and powerful configuration management tool. If you are new to Ansible, just start applying playbooks, and once you understand how that really works, seek out more advanced components.
Take a closer look at configuration management options from Puppet
Determine which configuration management tool is best for your organization
The Ansible roadmap is automating its way beyond the DevOps niche