Ansible Playbook Command (with examples)
Ansible is an open-source automation tool that simplifies IT infrastructure management by automating tasks such as server configurations, software installations, and application deployments. Ansible Playbooks allow you to define a set of tasks in a structured and reusable format. The ansible-playbook
command is used to execute these tasks defined in a playbook on remote machines over SSH.
In this article, we will explore eight different use cases of the ansible-playbook
command, along with code examples and explanations for each use case.
Use Case 1: Run tasks in playbook
Code:
ansible-playbook playbook.yml
Motivation: The first and most basic use case is to run the tasks defined in a playbook. This is useful when you want to execute a set of tasks without any additional configurations or variables.
Explanation:
The ansible-playbook
command is followed by the name of the playbook file (playbook.yml in this example). It will execute all the tasks defined in the playbook on the targeted remote machines.
Use Case 2: Run tasks in playbook with custom host inventory
Code:
ansible-playbook playbook.yml -i inventory.ini
Motivation: In some cases, you may want to specify a custom host inventory file instead of using the default inventory file. This is useful when you have a different set of hosts for a specific playbook or when you want to group your hosts differently.
Explanation:
The -i
option is used to specify the custom host inventory file (inventory.ini in this example). The ansible-playbook
command will then use this inventory file to determine the target hosts for executing the tasks defined in the playbook.
Use Case 3: Run tasks in playbook with extra variables defined via the command-line
Code:
ansible-playbook playbook.yml -e "variable1=value1 variable2=value2"
Motivation: Sometimes, you may need to pass additional variables to your playbook at runtime. This can be done by defining extra variables via the command-line. It allows you to dynamically change the behavior of the playbook without modifying the playbook itself.
Explanation:
The -e
option is used to define extra variables in the form of “variable=value”. Multiple variables can be defined by separating them with a space. In this example, we are passing two extra variables (variable1 and variable2) with their respective values (value1 and value2) to the playbook.
Example Output:
PLAY [Example Playbook] *********************************************************
TASK [Print Variable 1] *********************************************************
ok: [example_host] => {
"msg": "Value of variable1 is value1"
}
TASK [Print Variable 2] *********************************************************
ok: [example_host] => {
"msg": "Value of variable2 is value2"
}
PLAY RECAP *********************************************************************
example_host : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
In the above example playbook, we have two tasks that print the values of variable1 and variable2. The output shows that the values passed via the command-line are successfully used in the playbook.
Use Case 4: Run tasks in playbook with extra variables defined in a JSON file
Code:
ansible-playbook playbook.yml -e "@/path/to/variables.json"
Motivation: Instead of defining extra variables via the command-line, you may prefer to store them in a JSON file. This provides a cleaner and more readable way of managing multiple variables.
Explanation:
The -e
option is used to specify a JSON file containing the extra variables. The file is preceded by the “@” symbol. In this example, the variables.json file located at /path/to/variables.json is used.
Use Case 5: Run tasks in playbook for the given tags
Code:
ansible-playbook playbook.yml --tags tag1,tag2
Motivation: Large Ansible playbooks often contain multiple tasks, and sometimes you may want to execute only a specific subset of tasks. Tags allow you to categorize tasks and selectively execute them based on the specified tags.
Explanation:
The --tags
option is used to specify one or more tags separated by commas. In this example, the playbook will execute only the tasks associated with tag1 and tag2. Tasks that do not have any tags or have different tags will be skipped.
Use Case 6: Run tasks in a playbook starting at a specific task
Code:
ansible-playbook playbook.yml --start-at task_name
Motivation:
If you want to execute only a specific task or a subset of tasks in a playbook, you can use the --start-at
option. This is useful when you want to skip the execution of previous tasks and start from a specific point in the playbook.
Explanation:
The --start-at
option is used to specify the name of the task from which the playbook should start execution. In this example, the playbook will skip all the tasks before the task_name and start executing from that point onwards.
Conclusion
In this article, we covered eight different use cases of the ansible-playbook
command, along with code examples and explanations for each use case. These examples demonstrate the flexibility and power of Ansible Playbooks in automating various tasks on remote machines. By mastering the ansible-playbook
command, you can efficiently manage your IT infrastructure, deploy applications, and streamline your operations.