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 |
Note: The
--check flag runs the playbook in dry-run mode, showing what would change without actually making changes.
2. Checking Syntax
| 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
| 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 |
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>' |
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" |
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 |
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 |
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 |
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 |
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 |