Deploying files
Using Modules to Manipulate Files
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
- 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: 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, template, 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. Ensures 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.
More Deploying Files
Stat module
The outputs of the stat module can be used as a variable to test files.
You can see all of the outputs at:
Example from the docs:
Tests
Another page that will be useful to find quickly during the exam is the tests page. Just open the documentation site and type “tests”. I may end up just opening this right away during the exam.
It’s probably worth working with tests a bit. I wasn’t sure exactly how to test against a bool when I tried but ended up with this:
The not st.stat.writeable threw me for a loop cause every scenario I tried thought it was a string.
Modules to remember:
fetch - Move a file from a the remote host to the ansible control node.
synchronize - Wrapper around rsync to sync files.
copy - Move a file from the control node to the managed host.
lineinfile - Copy a single line of text to a file.
blockinfile - Copy multiple lines to a file.
template - Copy templated file to the host. (indempotent)
acl - Work with system ACLs.
replace - Replaces strings in files based on regex.