Lesson 3 of 6 · Config Management with Ansible

Playbooks and Modules

Plays, tasks, and modules, a playbook that prepares Oracle servers, ok and changed results, and variables, templates, and logic.

Beginner 5 sections 4 min read

Anatomy of a playbook

A playbook is a YAML file describing what should be true on a group of servers. It contains one or more plays; each play targets some servers and lists tasks; each task calls a module with arguments. Because modules are idempotent, a playbook can run again and again, changing only what isn't already right.

PLAYBOOKprepare_oracle.yml PLAYhosts: oracle · become: true · vars TASK 1TASK 2TASK 3 install the Oraclepreinstall packageset a kernelparametercreate the Oracledirectories ansible.builtin.dnfansible.posix.sysctlansible.builtin.file

Here's that playbook: preparing Oracle Linux servers for an Oracle Database install, the same way on every server.

# prepare_oracle.yml
- name: Prepare Oracle database servers
  hosts: oracle
  become: true
  vars:
    oracle_dirs:
      - /u01/app/oracle
      - /u01/app/oraInventory
      - /u02/oradata

  tasks:
    - name: Install the Oracle preinstall package (packages, oracle user, kernel settings)
      ansible.builtin.dnf:
        name: oracle-database-preinstall-19c
        state: present

    - name: Allow larger shared memory segments
      ansible.posix.sysctl:
        name: kernel.shmmax
        value: "{{ shmmax_bytes }}"
        state: present

    - name: Create Oracle directories, owned by oracle
      ansible.builtin.file:
        path: "{{ item }}"
        state: directory
        owner: oracle
        group: oinstall
        mode: "0775"
      loop: "{{ oracle_dirs }}"

Modules

Modules do the actual work. Each describes a desired state, checks the server, and changes only what's needed. Module names are written in full, as namespace.collection.module, such as ansible.builtin.file. Some that DBAs use all the time:

ModuleWhat it does
ansible.builtin.dnf, apt, packageInstall, update, or remove packages
ansible.builtin.user, groupManage OS users and groups, such as oracle and dba
ansible.builtin.fileDirectories, permissions, ownership, and links
ansible.builtin.copy, templatePut files on servers; template fills in variables first
ansible.builtin.lineinfileMake sure one line is in a file, such as an entry in /etc/oratab
ansible.posix.sysctl, community.general.pam_limitsKernel parameters and user limits
ansible.builtin.systemd_serviceStart, stop, and enable services
ansible.builtin.command, shellRun a command, such as sqlplus or opatch, when no module exists
community.postgresql.postgresql_db, postgresql_userCreate PostgreSQL databases and users, idempotently

ok, changed, and failed

Every task reports one of these results, and the run ends with a recap per server:

$ ansible-playbook prepare_oracle.yml
TASK [Install the Oracle preinstall package] *****************
changed: [db01.acme.local]
ok: [db02.acme.local]

TASK [Create Oracle directories, owned by oracle] ************
changed: [db01.acme.local] => (item=/u01/app/oracle)
...

PLAY RECAP ****************************************************
db01.acme.local : ok=4  changed=3  unreachable=0  failed=0  skipped=0
db02.acme.local : ok=4  changed=0  unreachable=0  failed=0  skipped=0

ok means the server was already right, and nothing changed. changed means Ansible fixed something. Here, db01 was a new server and needed three changes; db02 was already prepared, so nothing changed. Run the playbook again, and db01 will show changed=0 too.

Variables, templates, and logic

Variables and Jinja2
Values in double curly braces, such as "{{ oracle_base }}", are filled in from variables: in the play, the inventory's group_vars, or the command line. Ansible uses the Jinja2 templating language for this.
Templates
The template module renders a file with variables filled in, such as a response file for a silent Oracle install, or an init.ora with each environment's memory settings.
Facts
At the start of a play, Ansible gathers facts about each server: OS version, memory, CPUs, disks, and IP addresses. Tasks can use them, such as sizing memory from ansible_memtotal_mb.
Conditions, loops, and register
when: runs a task only if a condition is true; loop: repeats it for each item in a list; register: saves a task's result for later tasks.
Handlers
Tasks that run only when notified by a change, such as restarting a service after its configuration file changes, and only once, at the end of the play.
# check the database version, and act only on servers that need it
- name: Get the Oracle version
  ansible.builtin.shell: |
    . ~/.bash_profile
    echo "SELECT version_full FROM v\$instance;" | sqlplus -s / as sysdba
  become_user: oracle
  register: db_version
  changed_when: false             # a read-only check never counts as a change

- name: Show servers that still need the latest Release Update
  ansible.builtin.debug:
    msg: "{{ inventory_hostname }} is on {{ db_version.stdout | trim }}"
  when: target_version not in db_version.stdout

Key takeaways

  • A playbook holds plays; a play targets hosts and lists tasks; each task calls a module.
  • Modules are idempotent: they report ok when nothing needed changing, and changed when they fixed something.
  • Use full module names, prefer real modules over command and shell, and make commands safe to repeat.
  • Variables, Jinja2 templates, facts, conditions, loops, register, and handlers make playbooks flexible.
  • Mind YAML's indentation: spaces only, and keep levels lined up.