Playbook: A single YAML file Play: defines a set of activities (tasks) to be run on hosts Task: An action to be performed on the host Module: The different actions run by tasks

  • Execute playbook: ansible-playbook playbook.yaml

Verifying Playbooks

Check Mode

Ansible executes “dry run” where no actual changes are made on hosts ansible-playbook install_nginx.yaml --check

Note

Not all modules support check mode

Diff Mode

Provides a before-and-after comparison of playbook changes. It is helpful to understand and verify the impact of playbook ansible-playbook install_nginx.yaml --check --diff

Syntax Check

Checks the syntax of the playbook ansible-playbook install_nginx.yaml --syntax-check

Ansible-lint

It is a tool to check complex playbooks for bugs, errors, stylistic errors, and suspicious constructurs. ansible-lint style_example.yaml

Conditions

Conditions

Conditions are used in playbooks. There are modules that will help you. You can use most logic operators

when

- name: install nginx on debian
  apt: 
	  name: nginx
	  state: present
  when: ansible_os_family == "Debian" or "Ubuntu"

You can also use ‘ansible_facts’: when: ansible_facts['os_family'] == "Debian" and ansible_facts['distribution_major_version'] == '18'

Conditionals in Loops

---
- name: Install Softwares
  hosts: all
  vars:
    packages:
      - name: nginx
        required: True
      - name: mysql
        required: True
      - name: apache
        required: False

  tasks:
    - name: Install "{{ item.name }}" on Debian
      apt:
        name: "{{ item.name }}"
        state: present
		when: item.required == True ######
      loop: "{{ packages }}"

Register

You can output the result of a task using register

Link to original