Ansible is an open-source automation tool that allows you to manage and configure computer systems. It uses a declarative language called YAML (Yet Another Markup Language) for defining configurations and tasks. Ansible follows a client-server architecture, where the controlling machine (the Ansible server) manages and communicates with the target machines (managed nodes) over SSH.
The basics of Ansible
An inventory file in Ansible contains a list of target hosts (managed nodes) on which Ansible performs operations. You can define the inventory in a simple text file or in dynamic inventory scripts. Each host entry can have associated variables like IP address, username, and SSH key.
Playbooks are YAML files that define a set of tasks to be executed on the managed nodes. They are the heart of Ansible automation. Playbooks consist of one or more plays, and each play targets a specific group of hosts defined in the inventory. Plays contain tasks that describe the actions to be performed.
Tasks are individual units of work in Ansible. Each task typically represents a specific action like installing a package, copying files, or starting a service. Tasks are executed sequentially on the managed nodes. Ansible provides numerous built-in modules for performing various tasks.
Modules are reusable code units that Ansible executes on the managed nodes to perform specific actions. They are written in Python and can be bundled with Ansible or created by the user. Modules provide a wide range of functionalities such as managing packages, files, services, users, and executing commands on remote systems.
A typical Ansible playbook has the following structure:
- name: Playbook Name
- name: Task 1
- name: Task 2
name field is used to give a name to the playbook or task.
hosts field specifies the target hosts or host groups from the inventory.
become field enables privilege escalation, allowing tasks to be executed with administrative privileges if necessary.
tasks field contains a list of tasks to be executed.
To run an Ansible playbook, you use the ansible-playbook command followed by the playbook file name. For example:
Ansible will connect to the managed nodes via SSH and execute the defined tasks sequentially.
These are just the basics of Ansible. There are many more advanced features and concepts you can explore, such as roles, conditionals, variables, templates, and more. Ansible documentation provides comprehensive guidance and examples for learning and using Ansible effectively.