Advanced File Management with Ansible
In this final step, we will explore some advanced file management techniques with Ansible, including file permissions, conditional file creation, and using multiple file-related modules.
Setting File Permissions and Ownership
When creating files, you often need to set specific permissions and ownership. Let us create a playbook that demonstrates this:
- Create a new file in the WebIDE:
- Navigate to the
~/project/ansible-files
directory
- Right-click and select "New File"
- Name the file
file_permissions.yml
- Add the following content:
---
- name: Manage file permissions and ownership
hosts: local
tasks:
- name: Create a script file with execute permissions
copy:
dest: ~/project/script.sh
content: |
#!/bin/bash
echo "This script was created by Ansible"
echo "Current user: $(whoami)"
echo "Current directory: $(pwd)"
mode: "0755"
- name: Create a read-only configuration file
copy:
dest: ~/project/readonly.conf
content: |
## This is a read-only configuration file
setting1 = value1
setting2 = value2
mode: "0444"
In this playbook:
- The
mode
parameter is used to set file permissions.
0755
means read, write, and execute for the owner, and read and execute for group and others.
0444
means read-only for everyone.
- Run the playbook:
cd ~/project/ansible-files
ansible-playbook file_permissions.yml
You should see output similar to:
PLAY [Manage file permissions and ownership] **********************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Create a script file with execute permissions] **************************
changed: [localhost]
TASK [Create a read-only configuration file] **********************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- Let us check the permissions of the created files:
ls -l ~/project/script.sh ~/project/readonly.conf
You should see output similar to:
-rwxr-xr-x 1 labex labex 118 Aug 15 12:34 /home/labex/project/script.sh
-r--r--r-- 1 labex labex 73 Aug 15 12:34 /home/labex/project/readonly.conf
- Let us verify that the script can be executed:
~/project/script.sh
You should see output similar to:
This script was created by Ansible
Current user: labex
Current directory: /home/labex/project/ansible-files
Conditional File Creation
Sometimes you need to create files only when certain conditions are met. Let us create a playbook that demonstrates conditional file creation:
- Create a new file in the WebIDE:
- Navigate to the
~/project/ansible-files
directory
- Right-click and select "New File"
- Name the file
conditional_file.yml
- Add the following content:
---
- name: Conditional file creation
hosts: local
vars:
environment: "development"
create_debug_file: true
create_backup: false
tasks:
- name: Create environment-specific configuration
copy:
dest: "~/project/{{ environment }}_config.yml"
content: |
## Configuration for {{ environment }} environment
debug: {{ 'enabled' if environment == 'development' else 'disabled' }}
log_level: {{ 'DEBUG' if environment == 'development' else 'INFO' }}
- name: Create debug log file
copy:
dest: ~/project/debug.log
content: |
## Debug log file
## Created: {{ ansible_date_time.iso8601 }}
mode: "0644"
when: create_debug_file
- name: Create backup directory
file:
path: ~/project/backup
state: directory
mode: "0755"
when: create_backup
In this playbook:
- The
when
directive is used for conditional execution of tasks.
- Jinja2 conditionals are used in the file content to change values based on variables.
- The
file
module is used to create a directory.
- Run the playbook:
cd ~/project/ansible-files
ansible-playbook conditional_file.yml
You should see output similar to:
PLAY [Conditional file creation] **********************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Create environment-specific configuration] ******************************
changed: [localhost]
TASK [Create debug log file] **************************************************
changed: [localhost]
TASK [Create backup directory] ************************************************
skipped: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Notice that the "Create backup directory" task was skipped because create_backup
was set to false
.
- Let us examine the created files:
cat ~/project/development_config.yml
cat ~/project/debug.log
ls -la ~/project/ | grep backup
You should see the contents of the two files that were created, and confirm that no backup directory was created.
Ansible provides several modules for file management. Let us create a playbook that demonstrates using multiple file-related modules:
- Create a new file in the WebIDE:
- Navigate to the
~/project/ansible-files
directory
- Right-click and select "New File"
- Name the file
file_modules.yml
- Add the following content:
---
- name: Demonstrate file-related modules
hosts: local
tasks:
- name: Create a directory
file:
path: ~/project/ansible_demo
state: directory
mode: "0755"
- name: Create a file using the copy module
copy:
dest: ~/project/ansible_demo/copied.txt
content: "This file was created using the copy module.\n"
- name: Create a symbolic link
file:
src: ~/project/ansible_demo/copied.txt
dest: ~/project/ansible_demo/link_to_copied.txt
state: link
- name: Create a file with blockinfile module
blockinfile:
path: ~/project/ansible_demo/block.txt
create: true
block: |
This is a block of text
that will be inserted
as a single unit.
marker: "## {mark} ANSIBLE MANAGED BLOCK"
In this playbook:
- The
file
module is used with state: directory
to create a directory.
- The
file
module is used with state: link
to create a symbolic link.
- The
blockinfile
module is used to create a file with a block of text surrounded by marker comments.
- Run the playbook:
cd ~/project/ansible-files
ansible-playbook file_modules.yml
You should see output similar to:
PLAY [Demonstrate file-related modules] ***************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Create a directory] *****************************************************
changed: [localhost]
TASK [Create a file using the copy module] ************************************
changed: [localhost]
TASK [Create a symbolic link] *************************************************
changed: [localhost]
TASK [Create a file with blockinfile module] **********************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- Let us verify the results:
ls -la ~/project/ansible_demo/
cat ~/project/ansible_demo/copied.txt
cat ~/project/ansible_demo/link_to_copied.txt
cat ~/project/ansible_demo/block.txt
You should see:
- A directory called
ansible_demo
- A file called
copied.txt
with the specified content
- A symbolic link called
link_to_copied.txt
pointing to copied.txt
- A file called
block.txt
with a block of text surrounded by marker comments
The output of the last command should be similar to:
## BEGIN ANSIBLE MANAGED BLOCK
This is a block of text
that will be inserted
as a single unit.
## END ANSIBLE MANAGED BLOCK
This demonstrates the versatility of Ansible's file management capabilities.