Technical skill

Ansible

Repeat a setup across several computers

If several computers need the same software and settings, doing each by hand can produce different results. Ansible lets you describe the work once and run it on selected computers. The computer running Ansible is the control node; the target computers are managed hosts. An inventory is the list of hosts and groups, such as web computers and database computers. A playbook is a file of tasks that says what should be set up.

For typical Linux hosts, Ansible connects over SSH, a way to log in to another computer over a network. Tasks can request administrator privileges when a change needs them. Check the inventory and connection account before running a playbook, especially if it can change configuration.

This small inventory puts one example machine in a group called web:

[web]
web-01.example.test

These commands display the parsed inventory and ask Ansible to connect to the web group:

ansible-inventory -i inventory.ini --list
ansible web -i inventory.ini -m ansible.builtin.ping

The Ansible ping module checks whether Ansible can connect and run a module on the host. It does not send an ICMP echo request like the Linux ping command. Confirm the selected host before running tasks with privilege escalation.

Describe the desired setup

A playbook uses YAML, a text format for structured data. It contains plays that choose hosts and ordered tasks that call modules. A module is the Ansible component that carries out a task, such as installing a package or starting a service. This playbook asks for nginx to be installed and its service to be running:

- name: Configure web hosts
  hosts: web
  become: true
  tasks:
    - name: Ensure nginx is installed
      ansible.builtin.package:
        name: nginx
        state: present
    - name: Ensure nginx is running
      ansible.builtin.service:
        name: nginx
        state: started

The become: true line asks Ansible to use privilege escalation for the tasks, which is needed for some system changes. Run the playbook with ansible-playbook -i inventory.ini site.yml. Modules that describe a desired state are usually idempotent: after a successful run, running the play again makes no change if the host already matches. A raw command or a module without state handling may repeat side effects, so check what each task does.

--check asks supported modules to estimate what would change without applying it. This is a simulation; some modules and task logic cannot predict their normal results in check mode. Test consequential playbooks in a safe environment, then limit the first live run to a small group and inspect the output and service state.

Keep playbooks, roles, and ordinary configuration in version control. Ansible Vault can encrypt variables or files while stored, but store its password safely and restrict access to decrypted runtime data. The play recap reports unreachable hosts separately from task failures. Use that distinction to see whether a problem is in the connection or the task before expanding the target group.

Interactive exercises

Materials

Skillsets
Skillset Skyeng DevOps TeamLead preview
image descriptionSkyeng DevOps TeamLead
by Artem Naumenko (slack2)
15 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset PostgreSQL DBA preview
image descriptionPostgreSQL DBA
by Artem Naumenko (fb)
1630 users
Skillset DevOps engineer preview
image descriptionDevOps engineer
by Infinite Education
1211 users
Skillset DevOps engineer course preview
image descriptionDevOps engineer course
by Artem Naumenko (fb)
1151 users
Show more
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful