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 stat for list of full output
Lab: write a message if the expected permission mode is not set.
---- name:stat module testhosts:ansible1tasks:- command:touch /tmp/statfile- stat:path:/tmp/statfileregister:st- name:show current valuesdebug:msg:current value of the st variable is {{ st }}- fail:msg:"unexpected file mode, should be set to 0640"when:st.stat.mode != '0640'
Lab: Use the file Module to Correct File Properties Discovered with stat
---- name:stat module testshosts:ansible1tasks:- command:touch /tmp/statfile- stat:path:/tmp/statfileregister:st- name:show current valuesdebug:msg:current value of the st variable is {{ st }}- name:changing file permissions if that's neededfile:path:/tmp/statfilemode:0640when:st.stat.mode != '0640'
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.
Use the file module to create a new directory and in that directory create an empty file, then remove the directory recursively.
---- name:using the file modulehosts:ansible1tasks:- name:create directoryfile:path:/newdirowner:ansiblegroup:ansiblemode:770state:directory- name:create file in that directoryfile:path:/newdir/newfilestate:touch- name:show the new filestat:path:/newdir/newfileregister:result- debug:msg:| This shows that newfile was created
"{{ result }}"- name:removing everything againfile:path:/newdirstate:absent
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
---- name:file copy moduleshosts:alltasks:- name:copy file democopy:src:/etc/hostsdest:/tmp/- name:add some lines to /tmp/hostsblockinfile:path:/tmp/hostsblock:| 192.168.4.110 host1.example.com
192.168.4.120 host2.example.comstate:present- name:verify file checksumstat:path:/tmp/hostschecksum_algorithm:md5register:result- debug:msg:"The checksum of /tmp/hosts is {{ result.stat.checksum }}"- name:fetch a filefetch:src:/tmp/hostsdest:/tmp/
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:
/tmp/ansible1/tmp/hosts
/tmp/ansible2/tmp/hosts
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.
---- name:testing file manipulation skillshosts:ansible1tasks:- name:create new filefile:name:/tmp/newfilestate:touch- name:check the status of the new filestat:path:/tmp/newfileregister:newfile- name:for debugging onlydebug:msg:the current values for newfile are {{ newfile }}- name:change file owner if neededfile:path:/tmp/newfileowner:ansiblewhen:newfile.stat.pw_name != 'ansible'- name:fetching a remote filehosts:ansible1tasks:- name:fetch file from remote machinefetch:src:/etc/motddest:/tmp- name:adding text to the text file that is now on localhosthosts:localhosttasks:- name:add a messageblockinfile:path:/tmp/ansible1/etc/motdblock:| welcome to this server
for authorized users onlystate:present- name:copy the modified file to ansible2hosts:ansible2tasks:- name:copy motd filecopy:src:/tmp/ansible1/etc/motddest:/tmp