- 1 year ago
Calling an Ansible script inside of a ansible script and/or within a Loop module
To call an Ansible script inside another Ansible script or within a loop module, you can use the include
or import_playbook
directives. Here's how you can do it:
Using
include
directive:yaml- name: Include Ansible script
include: path/to/your_script.yml
This will include the specified Ansible script (
your_script.yml
) at the specified location.Using
import_playbook
directive:yaml- name: Import Ansible playbook
import_playbook: path/to/your_playbook.yml
This will import the specified Ansible playbook (
your_playbook.yml
) at the specified location.Using a loop module:
yaml- name: Loop over items and call Ansible script
include: path/to/your_script.yml
loop:
- item1
- item2
- item3
This will loop over the specified items and call the Ansible script (
your_script.yml
) for each item.
Make sure to replace path/to/your_script.yml
and path/to/your_playbook.yml
with the actual paths to your Ansible script or playbook.
Note: The include
directive is more suitable for including tasks or plays within a playbook, while the import_playbook
directive is used to import entire playbooks. Choose the appropriate directive based on your requirements.
Remember to follow the correct YAML syntax and indentation rules when working with Ansible scripts and playbooks.
For more information, refer to the Ansible documentation on task includes: https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_includes.html