The Cheatcode
This week, I focused a lot on, “I can’t remember this, where can I find the answers?”
As always, if you want email updates on this series, subscribe to my newsletter:
RedHat provides a cheat code during the exam. Documentation. I intend to exploit that as much as I can. Because rote memorization sucks…
I ran through topics in ABC order. Doing labs and finding the right documentation in case I need help.
Topics:
- Ad Hoc commands
- Ansible.cfg
- Ansible Vault
- Boot process
- Deploying files
- Handlers, testing, and blocks
- Hostname patterns
Click the header to each section to visit my notes for that topic.
Ad Hoc commands
Official doc here.
The man page for the ansible command:
Ad Hoc commands are not a complicated topic. You can use them to gather information from a server or to make changes.
These are useful for scripts. This one provisions new VMS for my lab. Then bootstraps the VMs for Ansible.
Ansible.cfg
It’s going to be important to generate an ansible.cfg file on the fly. You can get example options from the ansible-config command.
ansible-config command
Useful command for viewing current Ansible settings and seeing available options.
See the man page here:
man ansible-config
List available options that go in ansible.cfg file:
ansible-config list
View your current config file:
ansible-config view
View config path and other information:
ansible-config --version
Generate an ansible.cfg file with all entries commented out:
ansible-config init --disabled > ansible.cfg
This prints out a large file. You’d have a lot to go through during the exam to get a workable ansible.cfg. So it’s probably best to create a bare minimum working version by hand.
Here is what I have for my lab:
Ansible Vault
ansible-vault command
View options and information:
Create a vault using a vault password file:
Edit the file:
If you have the vault listed as a variable file. Either in a playbook or under a variable folder such as: group_vars/all/secrets2.yml. You can list the vault password file to decrypt globally in ansible.cfg:
You can also add it as an option when you run ansible-playbook or have it in the playbook itself.
The man page also shows you how to decrypt a vault, view the contents, encrypt an existing file, and change a vault’s password.
Vault options for ansible-playbook command
Use –help and grep to quickly see vault options:
Vault options for ansible.cfg
Use the same strategy to quickly see vault options to set globally in ansible.cfg:
Boot process
Managing the boot process will be more challenging because there are no modules that manage this. A solid understanding of the boot process and systemd will be needed here.
Setting the default systemd target
Say we want to change the default systemd target from multi-user.target to graphical.target.
The relevant man pages here may be:
I didn’t find exact instructions on how to change the default target. But there is information on symlinking in the systemd.unit. And also mentions using the systemctl command to make the symlink.
Let’s check options for the systemctl command:
Using the systemctl method:
This method isn’t great because you have to run around with your head cut off trying to figure out a way to make it indempotent.
If you have a solid understanding of Ansible this is doable. In Sander Van Vugt’s cert guide, he mentions just making the symlink with the file module:
Using the symlink method
If you can’t remember the search path, the paths are listed at the top of the systemd-unit man page:
If you can remember that you need to symlink a unit to /etc/systemd/system/default.target then I think you’ll be good here.
You’ll also want to remember where systemd keeps all of the default unit files. List is listed at the top of the systemd man page:
The file module documentation has a symlink example at the bottom:
Come to think of it, the command module with the systemctl command is looking a lot easier now. It’s good to know both methods in case the exam objectives throw you for a loop.
Rebooting
You may need to use the reboot module for changes to take effect. The module documentation covers everything you need to know with examples:
Cron
I struggled a bit with the lab for this section. I do not remember learning about service facts. Nor did I remember about the logger command. Curse me for procrastination for so long after RHCSA.
I found some useful documentation though.
Cron specific documentation:
service_facts module:
logger command for printing log messages:
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.
Handlers, testing, and blocks
A handler won’t run if a task in the playbook fails.
Use force_handlers to make handlers run even if a tasks fails. ignore_errors can also accomplish this.
Handlers run after the play is finished. If you have multiple plays, the handlers for the first play will run before the second play begins.
Useful doc to search: Error handling in playbooks
The fail module also exists and let’s you specify a clear failure message:
A good blocks document exists and covers blocks, rescue, and always.
Hostname patterns
Match hosts and groups when running Ansible commands. This is useful to match a more specific set of hosts in you inventory. See patterns.
What now?
Getting distracted has been my biggest challenge this week. And I did not study as much as I would have liked. There are just so many other fun things to do besides study.
Next week I’ll take a practice exam to gauge my progress. I think knowing problem areas will help push me forward.
