Deploying files
This chapter covers the following subjects:
• Using Modules to Manipulate Files • Managing SELinux Properties • Using Jinja2 Templates
RHCE exam topics
• Use Ansible modules for system administration tasks that work with: • File contents • Use advanced Ansible features • Create and use templates to create customized configuration files
Using Modules to Manipulate Files
File Module Manipulation Overview
Common modules to manipulate files copy
- Copies files to remote locations fetch
- Fetches files from remote locations file
- Manage file and file properties
- Create new files or directories
- Create links
- Remove files
- Set permissions and ownership
acl
- Work with file system ACLs find
- Find files based on properties lineinfile
- Manages lines in text files blockinfile
- Manage blocks in text files replace
- Replaces strings in text files based on regex synchronize
- Performs rsync-based synchronization tasks stat
- Retrieves file or file system status
- enables you to retrieve file status information.
- gets status information and is not used to change anything
- use it to check specific file and perform an action if the properties are not set as expected. Shows:
- which permission mode is set,
- whether it is a link,
- which checksum is set on the file
- etc.
- See
ansible-doc statfor list of full output
Lab: View information about /etc/hosts file
Lab: write a message if the expected permission mode is not set.
Lab: Use the file Module to Correct File Properties Discovered with stat
Managing File Contents
Use lineinfile or blockinfile instead of copy to manage text in a file
Lab: Change a string, based on a regular expression.
Lab: Manipulate multiple lines
When blockinfile is used, the text specified in the block is copied with a start and end indicator.
Lab: Creating and Removing Files
Use the file module to create a new directory and in that directory create an empty file, then remove the directory recursively.
- state: absent recursively removes the directory.
Moving Files Around
copy module copies a file from the Ansible control host to a managed machine.
fetch module enables you to do the opposite
synchronize module performs Linux rsync-like tasks, ensuring that a file from the control host is synchronized to a file with that name on the managed host.
copy module always creates a new file, whereas the synchronize module updates a current existing file.
Lab: Moving a File Around with Ansible
- Ansible creates a subdirectory on the control node for each managed host in the dest directory and puts the file that fetch has copied from the remote host in that subdirectory:
Lab: Managing Files with Ansible
1. Create a file with the name exercise81.yaml and give it the following play header:
2. Add a task that creates a new empty file:
3. Use the stat module to check on the status of the new file:
4. To see what the status module is doing, add a line that uses the debug module:
5. Now that you understand which values are stored in newfile, you can add a conditional play that changes the current owner if not set correctly:
6. Add a second play to the playbook that fetches a remote file:
7. Now that you have fetched the file so that it is on the Ansible control machine, use blockinfile to edit it:
8. In the final step, copy the modified file to ansible2 by including the following play:
9. At this point you’re ready to run the playbook. Type ansible-playbook exercise81.yaml to run it and observe the results.
10. Type ansible ansible2 -a "cat /tmp/motd" to verify that the modified motd file was successfully copied to ansible2.