Automate with Ansible: Part 3

Laxfed Paulacy
3 min readJan 10, 2023

--

Use a playbook to automate all your apt updates and upgrades, bruh.

Previous

Playbooks

Ansible playbooks are a way to organize and automate the configuration and deployment of software using the Ansible automation tool. They are written in a simple and human-readable language called YAML, and they allow you to specify a series of tasks to be executed on one or more servers.

Each playbook consists of one or more “plays”, which are sets of tasks to be executed on a specific group of servers, called “hosts”. These plays are defined in the playbook file, and they specify which tasks should be executed and in which order. Each task is defined using an Ansible module, which is a pre-built unit of code that can be used to perform a specific type of action, such as installing a package, creating a file, or starting a service.

Ansible playbooks can be executed using the command-line tool ansible-playbook. When you run a playbook, Ansible will connect to the specified hosts, execute the tasks in the order they are defined, and report the results back to you.

Ansible playbooks can be used to perform a wide range of tasks, such as:

  • Installing and configuring software on servers
  • Setting up and configuring services
  • Managing users and permissions
  • Deploying applications
  • Creating and managing databases
  • Automating backups and disaster recovery

One of the main advantages of Ansible playbooks is their ability to be idempotent, meaning that they can be run multiple times without causing any unintended changes. This is achieved by using modules that are designed to check the current state of the system before making any changes. For example, if you use the apt module to install a package, it will check if the package is already installed before attempting to install it. If it is already installed, the task will be considered a success and no changes will be made.

Another advantage of Ansible playbooks is their ease of use and readability. The YAML format used for writing playbooks is simple and easy to understand, even for people with little or no programming experience. Additionally, the use of modules abstracts away much of the complexity of system administration, allowing users to focus on the tasks they want to accomplish rather than the underlying commands and scripts needed to accomplish them.

Ansible playbooks can also be parameterized by using variables, so that the same playbooks can be reused with different values. This allows for greater flexibility and scalability, as playbooks can be easily adapted to work with different environments and use cases.

Ansible also provides the ability to organize your playbooks into roles. Roles are a way to organize playbooks and other files into a common file structure, and they allow you to reuse and share playbooks across different projects.

In conclusion, Ansible playbooks are a powerful and flexible tool for automating the configuration and deployment of software. They are easy to use, easy to read, and can be used to perform a wide range of tasks. They allow for greater scalability and efficiency, and can help organizations to reduce errors and increase productivity.

Create a playbook

Create a playbooks directory under ansible and add an apt.yml file.

mkdir playbooks
touch playbooks/apt.yml

apt.yml

- hosts: "*" # This is a wildcard that will match all hosts.yml in the inventory
become: true # This will run all tasks as root
tasks:
- name: apt update && apt upgrade
apt:
update_cache: true
upgrade: yes

In this example I’m using the following hosts.yml file:

vbox:
hosts:
gotham:
ansible_host: 10.0.0.81
riddler:
ansible_host: 10.0.0.198
joker:
ansible_host: 10.0.0.231

With that all set-up you’ll run the following command:

ansible-playbook ./playbooks/apt.yml --user batman --ask-pass --ask-become-pass -i ./inventory/hosts.yml

Note

We’re still requiring passwords at this point, a future example will use key-based authentication.

A successful output should look like this.

Next

--

--

Laxfed Paulacy

Delivering Fresh Recipes, Crypto News, Python Tips & Tricks, and Federal Government Shenanigans and Content.