What Ansible is and how it works
Ansible is an open source automation system: you describe the desired state of your servers in playbooks, and Ansible itself brings the managed nodes to that state over SSH. No agents need to be installed on the servers.
Ansible is a good fit for configuring servers, deploying applications and managing network devices. Creating the cloud infrastructure itself — virtual machines and networks — is better left to Terraform: for the comparison, see the Terraform reference, section “What Terraform is and how it works”.
How a playbook run works
On the control node — an ordinary computer or server with Ansible installed — you maintain the inventory with the list of hosts and your playbooks. The ansible-playbook command reads the playbook, selects hosts from the inventory and runs the tasks of each play on those hosts in turn.
Normally Ansible connects to the nodes itself and runs the tasks — the push model. There is also the reverse option ansible-pull, where the nodes pull their configuration from a git repository, but it is rarely used.
# on the control nodeansible-playbook site.yml -i inventory.iniEach task calls a module — code that Ansible copies to the managed node into a temporary directory, runs there, usually with Python, and then removes. No separate daemon is needed on the node: connections to Linux go over SSH, and to Windows over WinRM or SSH.
ansible-pull, the nodes pull the configuration themselves.What a playbook is made of
A playbook is a YAML file in which you declare the desired state, not a sequence of commands. It consists of plays — large blocks, each tied to a host group and containing an ordered list of tasks:
- name: Set up web servers hosts: webservers tasks: - name: Ensure nginx is installed ansible.builtin.apt: name: nginx state: present - name: Ensure nginx is running ansible.builtin.service: name: nginx state: started- Control node — the machine that
ansible-playbook,ansibleand the other tools run from; Ansible is not installed on the managed nodes. - Managed nodes — servers, network devices and other machines that Ansible manages.
- Inventory — a list of managed nodes grouped together, for example
webservers. - Task — a single module call with specific arguments.
- Module — the code behind an action: install a package, copy a file, restart a service.
- Collection — the distribution format for playbooks, roles, modules and plugins; modules are grouped into collections.
Module names in playbooks are written in full: ansible.builtin.apt is the fully qualified name (FQCN) of the apt module from the built-in collection. Fully qualified names avoid ambiguity when the same module exists in different collections.
Idempotency and reporting
Most modules are idempotent: if the system is already in the desired state, the module changes nothing. So the playbook can be run again — the result will not change.
After the run, Ansible prints the recap (PLAY RECAP): how many tasks on each host finished as ok, changed or failed. The changed=0 line in the second run confirms that the playbook changes nothing when run again.
web1 : ok=3 changed=2 failed=0web2 : ok=3 changed=2 failed=0What you need to know first
- SSH and Linux basics — files, permissions, processes; the Linux reference.
- YAML syntax — playbooks are written in it; for details see the section “Playbooks: plays, tasks and handlers”.
- Python on the managed nodes — the modules run through it; for Windows nodes, PowerShell.
Section sources
the section is written from these pages- Introduction to Ansibledocs.ansible.com/projects/ansible/14/getting_started/introduction.html(opens in a new tab)
- Ansible conceptsdocs.ansible.com/projects/ansible/14/getting_started/basic_concepts.html(opens in a new tab)
- Managing Windows hosts with Ansibledocs.ansible.com/projects/ansible/14/os_guide/intro_windows.html(opens in a new tab)