Create a File on Remote Host
In this step, you will create a file on a remote host using the Ansible File module.
First, create a new Ansible playbook file called /home/labex/project/file-module-playbook.yaml and open it in a text editor.
Add the following content to the playbook file:
- hosts: localhost
tasks:
- name: Create a file on remote host
file:
path: /home/labex/file.txt
state: touch
file: Ansible module to manipulate the file system.
path: Specifies the path to the file, in this case /home/labex/file.txt.
state: Specifies the state of the file. Here, touch indicates that the file will be created if it does not exist, or updated with access and modification timestamps if it already exists.
The purpose of this playbook is to create a file named file.txt on the remote host.
Then, run the playbook using the following command:
ansible-playbook file-module-playbook.yaml
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 [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Create a file on remote host] ********************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Finally, verify that the file file.txt is created in the specified path on the remote host.
ll /home/labex/file.txt
Example output:
-rw-rw-r-- 1 labex labex 0 Mar 10 03:12 file.txt
You will see the message indicating that /home/labex/file.txt was successfully created.