What statement do you use for error handling? For instance, if you have code in a block and it fails, give it something to do to roll with the punches and maybe fix the error.
rescue:
- name:
module:
In your block there are tasks that fail. What do you put to run the next code in the block regardless if these fail.
always:
- name:
module:
Create a block of tasks
First it will remove a file
If there are any issues with this task failing, create a file in /tmp called ‘rescuefile’ and allow the playbook to complete.
Next, regardless of success or failure of the first task, have a task run make a log message.
There should be a message noting everything that’s happening in the playbook.
Blocks are great, but what’s a feature they don’t have?
You can’t use a loop that all tasks in block share
block:
tasks:
loop: {{ whatever }}
What are the most commonly used file modules? There are 10
file
copy
fetch - fetch files from remote locations
acl
find
lininfile
blockinfile
replace
synchronize
stat
How do you find related modules to file?
ansible-doc file
Show the statistics of /tmp/temporary
Register a files stats and make a condition based on one of the pieces of info. If it is not met, force the playbook to fail
Check SSHD for the permitrootlogin line and change it to no
If this causes a change, restart sshd
- name: SSH config
hosts: all
tasks:
- name: Disable Root Login
lineinfile:
dest: /etc/ssh/sshd_config
regexp: '^PermitRootLogin'
line: 'PermitRootLogin no'
notify: restart sshd
handlers:
- name: Restart SSHD
service:
name: sshd
state: restartedCreate a file named /tmp/hosts and add the below lines to it:
192.168.4.110 host1.example.com
192.168.4.110 host1.example.com
- name: Add Hosts
hosts: all
tasks:
- name: Create file
file:
path: /tmp/hosts
state: touch
- name: Add junk
blockinfile:
path: /tmp/hosts
block: |
192.168.4.110 host1.example.com
192.168.4.110 host1.example.com
state: presentWhat are four things the file module can do?
Create new files or directories
create links
remove files
set permissions and ownership
What’s the difference between the modules synchronize and copy?
Copy always makes a new file
synchronize just updates it
Copy is used to copy files FROM the control node
How do you copy files FROM the managed node
fetch
What is a checksum used for?
Copy over /etc/hosts to the managed node’s /tmp directory.
Add two lines to it for whatever hosts you want
Register a checksum for /tmp/hosts
Print the checksum
grab the file from /tmp/hosts and put it in your tmp folder
Where did the file go?
Checksums are used to determin if a file has changed and needs to be copied or updated.
- name: Test
hosts: all
tasks:
- name: copy
copy:
src: /etc/hosts
dest: /tmp/hosts
- name: Add junk
blockinfile:
path: /tmp/hosts
block: |
192.168.4.110 host1.example.com
192.168.4.110 host1.example.com
state: present
- name: checksum
stat:
path:
checksum_algorithm: md5
register: result
- name: debug
debug:
msg: {{ result.stat.checksum }}
- name: fetch file
fetch:
src: /tmp/hosts
dest: /tmpA directory was created for it in tmp with it’s name
Create a file on ansible1
register it’s status in a variable and print it
Change the user to ‘ansible’ if that isn’t the owner
Add another play that:
gets the motd from ansible1, put it in your temp directory
Add a play that adds text the motd
copy the motd to ansible2
What’s setting the context at file level rather than the selinux context
chcon vs semanage fcontext
What do you need to run selinux ansible playbooks on a managed node
policycoreutils-python-utils
Install Selinux commands
Create a file
Give it the contenxt type httpd_sys_content_type
Run restorecon
What does it mean to configure a service with a nondefault document root
Changing /var/www/html to a different path via httpd <- example
Create a playbook variable of httpd_read_user_content
Enable SElinux in targeted mode
Check the boolean’s status/info and register it
Show the boolean’s status
Enabled the boolean
Install , start and configure a webserver that has the DocumentRoot set to the /web directory. The file should be called index.html and it should say something welcoming the user to the server.
Ensure that SElinux is enabled and allows acces to the web server document root.
SElinux should allow user to publish web pages from their home directory.
This will reveal something is wrong when you try to curl the page, figure out why.
Best practice for a long playbook like this it to create the file header and add the task names prior.