Ansible
Revision | Date | Description |
|---|---|---|
| 24.07.2024 | Init Changelog |
Introduction
Ansible is a radically simple IT automation system. It handles configuration management, application deployment, cloud provisioning, ad-hoc task execution, network automation, and multi-node orchestration. Ansible makes complex changes like zero-downtime rolling updates with load balancers easy. More information on the Ansible website.
Design Principles
Have an extremely simple setup process with a minimal learning curve.
Manage machines quickly and in parallel.
Avoid custom-agents and additional open ports, be agentless by leveraging the existing SSH daemon.
Describe infrastructure in a language that is both machine and human friendly.
Focus on security and easy auditability/review/rewriting of content.
Manage new remote machines instantly, without bootstrapping any software.
Allow module development in any dynamic language, not just Python.
Be usable as non-root.
Be the easiest IT automation system to use, ever.
Use Ansible
You can install a released version of Ansible with pip or a package manager. See installation guide for details on installing Ansible on a variety of platforms.
Power users and developers can run the devel branch, which has the latest features and fixes, directly. Although it is reasonably stable, you are more likely to encounter breaking changes when running the devel branch. We recommend getting involved in the Ansible community if you want to run the devel branch.
Inventory
Inventory file contain list of hosts on which will run playbooks. File this has one of formats below:
Example file content:
Example file content:
As you can see:
Hosts can be grouped.
To every host you can assign some parameters with its values.
You can generate hosts list dynamically.
Playbook
Playbook is a single file or group of files with dependency set up. It is created in YAML format. Playbook contains all task definitions with detailed instructions which will run on inventory. Every task is build by module, which works out-of-the-box in Ansible. Every module has great documentation on Ansible Doc pages.
Modules examples:
systemctlyumusertemplatecopyshell
Some example Playbooks:
Templates
One of most useful part of Ansible are Templates. It allows to define template file, which will take values for defined variables in it from Playbook config and create ready-to-go files during run. Template can be defined once, and you can use it with different values on every host in your inventory.
Example template:
Example Playbook to use template above:
Template above defines two variables:
{{ inventory_hostname }}- refers to Ansible global variable, which takes value from hostname define in Inventory file.{{ message }}- refers to user's variable which value is defined in Playbook configuration invarssection.
Handlers
Handlers are task definitions, which run every time they are notify after others tasks run. Best way to describe how Handler works is by analyzing Playbook example:
Let's analyze this code:
We define Handler with name
restart httpd, which will restart Apache service usingsystemdmodule.We install Apache on host.
We start Apache service and add it to autostart.
We define and copy config file. But at end of this task, we
notifyblock, which will send information to our Handler that after Playbook full run it should restart Apache service.
Now, when we change something in source file in files/httpd.txt and run Playbook, Ansible will change this config file and because of this change - restart httpd Handler will be notified. But if we run Playbook without any change - nothing will happend.
As default, all handlers are run after successful Playbook run. But if we want to run all Handlers earlier, we should add to our Playbook task below:
How to run Playbook?
Just run command below (it is basic command, for more options check official docs or command manual):
Where:
-K- Ansible ask us for sudo password (this is optional, only if tasks needs root permissions).i- refers to Inventory file path.playbook.yaml- refers to main YAML file with our Playbook.
Dry run
Sometimes we need to only check what Ansible will do or change when we run our Playbook, but we actually do not want to perform any actions on host. To do that, we can use dry-run option. Ansible will run playbook with defined tasks and shows what it will do.
Flags to use dry-run:
--check- main option to rundry-runconfiguration.--diff- universal option (you can use it on normal run), it will show details about changes.
Ad-hoc commands
Ad-hoc commands allows to run Ansible on Inventory without defining Playbook in YAML file. It is useful, when we need to run some commands on lots of hosts fast. Example of usage ad-hoc command: