Ansible Playbooks

General

  • Community Playbooks: Ansible Galaxy
    • Install with ansible-galaxy collection install my_namespace.my_collection
  • Ansible register is a way to capture the output from task execution and store it in a variable.
  • If you start a value with {{ foo }}, you must quote the whole expression to create valid YAML syntax

Examples

Ansible Linux Demos - Github ansible/product-demos

Ansible Playbook: Esnure nano is installed

--- # yaml separator
 - name: iluvnano # play name
   hosts: centos  # host or group to run on from inventory file
   vars: # playbook variables
     http_port: 80
   tasks:
    - name: ensure nano is there # name of this task
      apt:            # specified module
        name: nano    # what to install
        state: latest # latest version

Ansible Playbook: APT Update & Upgrade

Run with: ansible-playbook ansible/apt.yml --user myname --ask-pass --ask-become-pass -i ansible/hosts

- hosts: "ubuntu"
  become: yes
  tasks:
    - name: apt
      apt:
        update_cache: yes
        upgrade: 'yes'

Notify Ansible Handlers

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_handlers.html#handler-example

- name: Verify apache installation
  hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
    - name: Write the apache config file
      ansible.builtin.template:
        src: /srv/httpd.j2
        dest: /etc/httpd.conf
      notify:
      - Restart apache
  handlers:
    - name: Restart apache
      ansible.builtin.service:
        name: httpd
        state: restarted