How to Use the Command 'ansible-playbook' (with Examples)
Ansible is a powerful automation tool used in IT for configuration management, application deployment, and task automation. One of its most significant components is the ansible-playbook
command, which enables users to execute tasks defined in YAML playbooks on remote machines over SSH. Ansible playbooks allow administrators to automate procedures on one or more remote hosts, ensuring consistency and efficiency across infrastructure operations. The following sections illustrate various use cases of the ansible-playbook
command, demonstrating its versatility and importance in the DevOps ecosystem.
Use Case 1: Run Tasks in Playbook
Code:
ansible-playbook playbook
Motivation: Running tasks directly from a playbook allows for streamlined and organized execution of automated routines. This method is straightforward, eliminating the need for manual interventions, ensuring that complex procedures are consistently executed on remote machines.
Explanation:
ansible-playbook
: This command initializes and executes the playbook on the specified remote hosts.playbook
: This is a placeholder for the name of the YAML file that contains all the tasks you wish to execute. The playbook should be well-structured and tested to ensure desired outcomes.
Example Output:
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [hostname1]
ok: [hostname2]
TASK [Ensure that Apache is installed] *****************************************
changed: [hostname1]
ok: [hostname2]
PLAY RECAP *********************************************************************
hostname1 : ok=2 changed=1 unreachable=0 failed=0
hostname2 : ok=2 changed=0 unreachable=0 failed=0
Use Case 2: Run Tasks in Playbook with Custom Host Inventory
Code:
ansible-playbook playbook -i inventory_file
Motivation: Utilizing a custom host inventory provides the flexibility to manage a varied group of servers across different environments without altering the playbook. This approach is particularly beneficial when you have different sets of machines for development, testing, and production.
Explanation:
-i inventory_file
: The-i
option specifies a custom inventory file, which defines the group of hosts on which the playbook will be executed. The inventory file can be a static file or a dynamic inventory script.
Example Output:
PLAY [group] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server1]
ok: [server2]
TASK [Restart service] *********************************************************
changed: [server1]
changed: [server2]
PLAY RECAP *********************************************************************
server1 : ok=2 changed=1 unreachable=0 failed=0
server2 : ok=2 changed=1 unreachable=0 failed=0
Use Case 3: Run Tasks in Playbook with Extra Variables Defined via the Command-Line
Code:
ansible-playbook playbook -e "variable1=value1 variable2=value2"
Motivation: Passing extra variables on the command line allows for dynamic customization of playbooks. This feature is useful in scenarios where certain parameters might change frequently or need to be specified at runtime.
Explanation:
-e "variable1=value1 variable2=value2"
: The-e
flag is used to pass extra variables to the playbook. Each variable carries key-value pairs which can be used within the playbook to influence task execution.
Example Output:
PLAY [Dynamic variables applied] **********************************************
TASK [Set variable values] ****************************************************
ok: [target_host]
PLAY RECAP *********************************************************************
target_host : ok=1 changed=0 unreachable=0 failed=0
Use Case 4: Run Tasks in Playbook with Extra Variables Defined in a JSON File
Code:
ansible-playbook playbook -e "@variables.json"
Motivation: Using a JSON file to store extra variables is advantageous when managing complex configurations or when the number of parameters is too high to be effectively handled directly from the command line. It promotes reusability and better organization of variables.
Explanation:
-e "@variables.json"
: The-e
option allows extra variables to be loaded from a JSON file. The@
symbol indicates that the input file (variables.json
) is being used.
Example Output:
PLAY [Load variables from JSON] ***********************************************
TASK [Display variables] ******************************************************
ok: [target_host] => {
"msg": "Variables have been successfully sourced from variables.json"
}
PLAY RECAP *********************************************************************
target_host : ok=1 changed=0 unreachable=0 failed=0
Use Case 5: Run Tasks in Playbook for the Given Tags
Code:
ansible-playbook playbook --tags tag1,tag2
Motivation: Selective task running through tags enables greater control over which parts of the playbook are executed, improving efficiency and reducing the potential for errors. This functionality is beneficial during testing when only certain tasks need verification.
Explanation:
--tags tag1,tag2
: The--tags
option allows you to specify tags associated with tasks inside the playbook. Only tasks with these tags will be executed during the run.
Example Output:
PLAY [Execute specific tags] **************************************************
TASK [Task with specific tags] ************************************************
ok: [target_host]
PLAY RECAP *********************************************************************
target_host : ok=1 changed=0 unreachable=0 failed=0
Use Case 6: Run Tasks in a Playbook Starting at a Specific Task
Code:
ansible-playbook playbook --start-at task_name
Motivation: When troubleshooting or refining a specific segment of a playbook, starting execution from a particular task can save time. It also helps avoid performing redundant operations on systems that are already in a desired state.
Explanation:
--start-at task_name
: This option is used to begin playbook execution from a named task. All preceding tasks are skipped.
Example Output:
PLAY [Start execution from a specific task] ***********************************
TASK [Specific task execution] ************************************************
ok: [target_host]
PLAY RECAP *********************************************************************
target_host : ok=1 changed=0 unreachable=0 failed=0
Use Case 7: Run Tasks in a Playbook Without Making Any Changes (Dry-Run)
Code:
ansible-playbook playbook --check --diff
Motivation: Performing a dry run allows users to preview the changes a playbook will make without actually applying them. This approach is crucial for verifying task logic, ensuring changes align with expectations, and avoiding unintended modifications.
Explanation:
--check
: This option is responsible for running the playbook in dry-run mode, implying that no actual changes are made to the systems.--diff
: When used alongside--check
, it highlights potential changes that would result from executing the playbook.
Example Output:
PLAY [Dry-run mode] ***********************************************************
TASK [Check mode with diff] ***************************************************
ok: [target_host] => (item={}) => {
"msg": "This is what would change"
}
PLAY RECAP *********************************************************************
target_host : ok=1 changed=0 unreachable=0 failed=0
Conclusion:
The ansible-playbook
command is an invaluable tool for IT operations and automations. Its capacity to execute complex processes across multiple hosts makes it a staple in modern infrastructure management. Whether managing inventories, leveraging extra variables, or running specific tasks, the flexibility and control offered by ansible-playbook
empower administrators to achieve efficiency and reliability in their automated tasks. The varied use cases illustrated above provide a snapshot of its capabilities, demonstrating why it remains a cornerstone in the toolset of many DevOps professionals.