Using Variables in Fetch Module
In this step, you will explore a more advanced usage of the Ansible Fetch module by incorporating variables. You will fetch files based on dynamic values defined in your playbook.
First, complete /home/labex/project/using_variablesin_fetch_modules.yml
file.
Open it in a text editor and add the following content to the playbook file:
- name: Fetch Module Lab
hosts: localhost
gather_facts: false
vars:
file_path: "/home/labex/example/example_4.txt"
dest_path: "/home/labex/project/example_4.txt"
tasks:
- name: Fetch files using variables
fetch:
src: "{{ file_path }}"
dest: "{{ dest_path }}"
flat: true
vars
: This section allows defining variables to be used within the playbook.
fetch
: This is the Ansible module used to fetch files from remote hosts.
src
: This specifies the source path of the file to be fetched. It uses the file_path
variable to dynamically provide the source file path.
dest
: This specifies the destination path where the fetched file should be stored. It uses the dest_path
variable to dynamically provide the destination file path.
flat
: This parameter controls the behavior of the destination path. When set to true
, it ensures that the fetched file is placed in the destination directory without creating any subdirectories. This is useful when fetching a single file and wanting to keep the directory structure simple.
In this configuration, it's used to fetch a file from the source file path (file_path
variable) on the remote host and store it in the destination file path (dest_path
variable) on the local Ansible control node. The use of variables allows for dynamic specification of file paths, providing flexibility and reusability in the playbook.
Then, run the playbook with the following command:
ansible-playbook using_variablesin_fetch_modules.yml
Example output:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [Fetch Module Lab] **************************************************
TASK [Fetch files using variables] *********************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Next, verify that the example_4.txt
file has been fetched locally from the remote host.
ll /home/labex/project/example_4.txt
Example output:
-rw-rw-r-- 1 labex labex 23 Mar 14 11:16 /home/labex/project/example_4.txt
Finally, try specifying file_path
and dest_path
with the -e
option:
ansible-playbook using_variablesin_fetch_modules.yml -e file_path="/tmp/example_5.txt" -e dest_path="/tmp/target/example_5.txt"
Example output:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [Fetch Module Lab] **************************************************
TASK [Fetch files using variables] *********************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Verify that the example_5.txt
file has been fetched locally from the remote host.
ll /tmp/target/example_5.txt
Example output:
-rw-rw-r-- 1 labex labex 23 Mar 14 11:24 /tmp/target/example_5.txt