Skip to main content

Using Ansible Playbook Command

1. Running Playbooks

Command Description Options Example
ansible-playbook Executes Ansible playbooks Standard options like -i, -l, --check ansible-playbook site.yml -i inventory.ini

Example: Running a playbook with inventory

ansible-playbook site.yml -i production.ini --check
Note: The --check flag runs the playbook in dry-run mode, showing what would change without actually making changes.

2. Checking Syntax

Example: Syntax Check

ansible-playbook --syntax-check site.yml
Option Values Description
--syntax-check true Validate playbook syntax without executing
--check false Run in check mode (dry-run)
Note: The --syntax-check flag validates YAML structure and playbook format, ensuring no runtime errors before execution.

3. Performing Dry Run

Example: Running a playbook with dry-run

ansible-playbook site.yml --check
Option Description Usage
--check Run playbook in dry-run mode Simulates changes without applying them
--diff Show differences in changed files Used with --check to show what would change
Note: Dry-run does not execute tasks but still loads and evaluates all variables and conditions.
Warning: Some modules may still perform actions during a dry-run, such as checking file existence or validating configuration syntax.

4. Showing Differences

Option Values Description
--diff -D Shows the differences in files changed by tasks
--diff-only -d Shows only the differences, not the full file content

Example: Using --diff flag

ansible-playbook site.yml --diff
Note: The --diff option is particularly useful when running playbooks that modify files, such as configuration files or templates. It helps in reviewing what changes were made.

5. Limiting Hosts

Use the --limit option to restrict playbook execution to specific hosts or groups.

Option Description Example
--limit Restrict execution to specific hosts or groups ansible-playbook site.yml --limit webservers
--limit Use pattern matching to limit hosts ansible-playbook site.yml --limit 'webservers:<web1>'

Example: Limiting to a specific host

ansible-playbook site.yml --limit web1.example.com

Example: Limiting to a host group

ansible-playbook site.yml --limit webservers

Example: Using pattern matching

ansible-playbook site.yml --limit 'webservers:<web1>'

6. Using Tags

Tag Description Usage
--tags Run only tasks with specified tags ansible-playbook site.yml --tags "web"
--skip-tags Skip tasks with specified tags ansible-playbook site.yml --skip-tags "db"

Example: Tagging Tasks

- name: "Install web server"
  apt:
    name: nginx
    state: present
  tags:
    - web
    - server

- name: "Configure database"
  template:
    src: db.conf.j2
    dest: /etc/db.conf
  tags:
    - db
    - config

Example: Running Specific Tags

ansible-playbook site.yml --tags "web"

ansible-playbook site.yml --tags "web,db"

ansible-playbook site.yml --skip-tags "test"
Note: Tags are useful for running subsets of a playbook during development or deployment.

7. Starting at Task

When running an Ansible playbook, you can start execution at a specific task using the --start-at-task option. This is useful for debugging or re-running a specific part of a playbook without executing all previous tasks.

Option Description Example
--start-at-task Start playbook execution at a specific task name ansible-playbook site.yml --start-at-task="Install Apache"

Example: Starting at a Specific Task

ansible-playbook site.yml --start-at-task="Configure Firewall"
Note: The task name must exactly match the name field of a task in your playbook.
Warning: If the specified task is not found, Ansible will exit with an error.

8. Setting Verbosity

Option Values Description
-v 1 Verbose mode, shows basic output
-vv 2 More verbose, shows task names and status
-vvv 3 Very verbose, includes task details and variables
-vvvv 4 Debug mode, shows all internal Ansible operations

Example: Running a playbook with verbose output

ansible-playbook site.yml -vv

9. Listing Tasks

Option Values Description
--list-tasks ansible-playbook List all tasks in a playbook without executing them
--list-tags ansible-playbook List all tags in a playbook without executing them
--list-hosts ansible-playbook List all hosts in a playbook without executing them

Example: Listing Tasks in a Playbook

ansible-playbook example.yml --list-tasks
Note: The --list-tasks option is useful for reviewing the order and structure of tasks before running a playbook.

10. Listing Hosts

Command Description Options Example
ansible-playbook List all hosts in inventory --list-hosts ansible-playbook site.yml --list-hosts
ansible-playbook List all hosts in inventory with details --list-hosts + -v ansible-playbook site.yml --list-hosts -v

Example: Listing hosts in a playbook

ansible-playbook site.yml --list-hosts
Note: The --list-hosts option is useful for verifying which hosts are included in the inventory before running a playbook.

11. Using Extra Variables

Variable Usage Description
--extra-vars -e Pass extra variables to the playbook
--extra-vars --extra-vars "key=value" Set a single variable
--extra-vars --extra-vars "@file.json" Load variables from a JSON file
--extra-vars --extra-vars "@file.yml" Load variables from a YAML file

Example: Setting a variable with -e

ansible-playbook site.yml -e "environment=production"

Example: Loading variables from a file

ansible-playbook site.yml --extra-vars "@vars.json"
Note: Extra variables override inventory and group variables, but can be overridden by host_vars or group_vars.

12. Setting Inventory

Option Values Notes
inventory hosts.ini Path to inventory file
inventory hosts.yaml YAML format inventory file
inventory hosts.json JSON format inventory file
inventory inventory/ Directory containing inventory files
inventory localhost, Static inventory with comma-separated hosts

Example: Static Inventory File (hosts.ini)

[webservers]
web1.example.com
web2.example.com

[databases]
db1.example.com
db2.example.com

[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsa

Example: Dynamic Inventory Script

#!/bin/bash
# inventory.py - Dynamic inventory script for AWS EC2 instances

echo '{
  "ec2": {
    "hosts": [
      "10.0.1.100",
      "10.0.1.101"
    ],
    "vars": {
      "ansible_user": "ec2-user"
    }
  }
}'

Was this article helpful?