How to use the command 'ansible-pull' (with examples)

How to use the command 'ansible-pull' (with examples)

Ansible is an open-source automation tool that allows you to manage and configure systems. The ansible-pull command is used to quickly pull desired Ansible playbooks from a version control system (VCS) repository and execute them on the local host. This command simplifies the process of applying configurations to a local machine without requiring a centralized control node.

Use case 1: Pull a playbook from a VCS and execute a default local.yml playbook

Code:

ansible-pull -U repository_url

Motivation: In this use case, we pull a playbook from a VCS repository specified by the repository_url parameter and execute the default local.yml playbook. This is useful when you have a common playbook that needs to be run on multiple local machines.

Explanation:

  • -U repository_url specifies the URL of the VCS repository where the playbook is located. The repository can be either Git or Mercurial.

Example Output:

PLAY [localhost] *****************************************************************************************

TASK [Gathering Facts] ************************************************************************************
ok: [localhost]

PLAY RECAP ***********************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Use case 2: Pull a playbook from a VCS and execute a specific playbook

Code:

ansible-pull -U repository_url playbook

Motivation: This use case is helpful when you want to pull a specific playbook from a VCS repository using the repository_url parameter. It allows you to execute a playbook that targets specific configurations for the local host.

Explanation:

  • -U repository_url specifies the URL of the VCS repository where the playbook is located.
  • playbook is the name of the playbook to be executed.

Example Output:

PLAY [localhost] *****************************************************************************************

TASK [task1] **********************************************************************************************
ok: [localhost] => {
    "msg": "This is task 1"
}

PLAY RECAP ***********************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Use case 3: Pull a playbook from a VCS at a specific branch and execute a specific playbook

Code:

ansible-pull -U repository_url -C branch playbook

Motivation: In this use case, we specify a specific branch of the VCS repository using the -C branch parameter. This allows you to pull the playbook from a particular branch and execute a specific playbook in that branch.

Explanation:

  • -U repository_url specifies the URL of the VCS repository where the playbook is located.
  • -C branch specifies the branch from which to pull the playbook.
  • playbook is the name of the playbook to be executed.

Example Output:

PLAY [localhost] *****************************************************************************************

TASK [task1] **********************************************************************************************
ok: [localhost] => {
    "msg": "This is task 1"
}

TASK [task2] **********************************************************************************************
ok: [localhost] => {
    "msg": "This is task 2"
}

PLAY RECAP ***********************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Use case 4: Pull a playbook from a VCS, specify hosts file and execute a specific playbook

Code:

ansible-pull -U repository_url -i hosts_file playbook

Motivation: This use case allows you to specify a custom hosts file using the -i hosts_file parameter. It enables you to execute a specific playbook on a set of hosts defined in the custom hosts file.

Explanation:

  • -U repository_url specifies the URL of the VCS repository where the playbook is located.
  • -i hosts_file specifies the path to the custom hosts file.
  • playbook is the name of the playbook to be executed.

Example Output:

PLAY [custom_hosts] ****************************************************************************************

TASK [task1] **********************************************************************************************
ok: [host1] => {
    "msg": "This is task 1"
}
ok: [host2] => {
    "msg": "This is task 1"
}

PLAY RECAP ***********************************************************************************************
host1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
host2                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Conclusion:

The ansible-pull command is a versatile tool for quickly pulling and executing Ansible playbooks from a VCS repository on the local host. By understanding the various use cases and options available, you can streamline the configuration management process and efficiently apply configurations to your local machines.

Related Posts

How to use the command iftop (with examples)

How to use the command iftop (with examples)

Description: The iftop command is a network monitoring utility that displays bandwidth usage on a specific network interface by host.

Read More
How to use the command "if" (with examples)

How to use the command "if" (with examples)

The “if” command in batch scripting is used to perform conditional processing.

Read More
Using the WMIC Command for Detailed Process Information (with examples)

Using the WMIC Command for Detailed Process Information (with examples)

Introduction The WMIC command is a powerful tool provided by Windows for retrieving detailed information about running processes on a system.

Read More