An Ansible playbook is a YAML file that declares the desired state of remote machines. Instead of manually installing and configuring an application on each machine, you describe the result you want—such as NGINX installed and running—and Ansible applies the tasks to the selected hosts. section 4.2 section 4.2.2
A basic playbook names the work, selects `hosts` from an inventory, and lists `tasks` in execution order. `become: true` lets tasks run as root; `vars` can hold reusable values. A task can install a package, copy a configuration file, or set a service’s state. For example, `state: present` requests an installed package, while `state: started` requests a running service. section 4.2.2 section 4.2.4
Start with NGINX
Create `devops/ansible/install-nginx.yaml` with `hosts: all`, `become: true`, and an `apt` task requesting `nginx` with `state: present`. Add a `service` task requesting `state: started` and `enabled: yes`. Running the playbook reports each task’s status for each host, so you can see whether Ansible made a change. section 4.2.2
Listing 4.7 Ensuring NGINX is running with Ansible
- name: Install NGINX
hosts: all
become: true
tasks:
- name: Install NGINX
apt:
name: nginx
state: present
update_cache: yes
- name: Ensure NGINX is running
service:
name: nginx
state: started
enabled: yes
The complete small playbook shows how an ordered task list both installs NGINX and ensures its service is running. section 4.2.2
To run a playbook from GitHub Actions, set up Python and Ansible, provide an SSH private key, create an inventory of remote hosts, and configure Ansible to use that inventory. A connection check with `ansible all -m ping` can verify the setup before `ansible-playbook` runs. The inventory, not the playbook’s task list, determines which machines belong to a selected host group. section 4.2.1 section 4.2.2
The same pattern grows from service setup into application deployment:
- For a Python application, update packages, install dependencies, create a virtual environment and directories, copy source files, install `requirements.txt`, then copy Supervisor and NGINX configurations. Configuration tasks can notify handlers to reload Supervisor or restart NGINX. section 4.2.3
- For a Node.js application, reuse the deployment pattern with a `vars` value for Node.js 18, copy the application and `package.json`, run `npm install`, and configure Supervisor and NGINX. section 4.2.4
This makes a playbook useful beyond a one-time setup command: the same declared tasks can be run for one host or many. These examples stay with basic playbooks rather than Ansible’s full range of options. section 4.2.2 section 4.2.1

