Expert · Ansible in depth · topic 53 of 57 · 35 min
RHEL System Roles and Ansible Vault
Explanation
RHEL System Roles
Writing a correct role for time sync or SELinux across RHEL 7, 8, 9 and 10 is hard work. Red Hat ships tested roles that do it, with one consistent interface. You do not write tasks; you set the role's variables and include it.
# dnf install -y rhel-system-roles
$ ls /usr/share/ansible/roles/ | head -5
linux-system-roles.certificate
linux-system-roles.firewall
...
rhel-system-roles.timesync
$ ls /usr/share/doc/rhel-system-roles/timesync/
example-timesync-playbook.yml README.md ...- name: Time sync for all servers
hosts: all
become: true
vars:
timesync_ntp_servers:
- hostname: ntp1.example.com
iburst: true
- hostname: ntp2.example.com
iburst: true
roles:
- redhat.rhel_system_roles.timesync- name: SELinux settings with the system role
hosts: webservers
become: true
vars:
selinux_state: enforcing
selinux_policy: targeted
selinux_booleans:
- { name: httpd_can_network_connect_db, state: on, persistent: true }
selinux_fcontexts:
- { target: '/web(/.*)?', setype: httpd_sys_content_t, state: present }
selinux_restore_dirs:
- /web
selinux_ports:
- { ports: '82', proto: tcp, setype: http_port_t, state: present }
roles:
- redhat.rhel_system_roles.selinuxOther useful system roles: network, storage (LVM and file systems), firewall, kdump, sshd, logging, metrics, crypto_policies, podman. Exam tasks often say "use the RHEL system role X to do Y"; the README and examples on the machine are your reference.
Ansible Vault: secrets in the open
Playbooks need secrets: database passwords, API tokens, user password hashes. Putting them in plain YAML in Git is a serious leak. Ansible Vault encrypts a file (or a single string) with AES-256. The encrypted file can be committed safely; only someone with the vault password can read it.
$ ansible-vault create secrets.yml # opens your editor; saved encrypted
New Vault password:
Confirm New Vault password:
$ cat secrets.yml | head -2
$ANSIBLE_VAULT;1.1;AES256
62313365396662343061393464336163383764373764613633653634306231386433626436623361
$ ansible-vault view secrets.yml
$ ansible-vault edit secrets.yml
$ ansible-vault encrypt old-plain.yml # encrypt an existing file
$ ansible-vault decrypt secrets.yml # back to plain text (careful)
$ ansible-vault rekey secrets.yml # change the vault password# secrets.yml (shown decrypted)
db_password: "S3cure-Example-Only"
app_api_token: "replace-me"
# site.yml
- name: Database clients
hosts: appservers
become: true
vars_files:
- secrets.yml
tasks:
- name: App config with the password
ansible.builtin.template:
src: app.conf.j2
dest: /etc/notes/app.conf
mode: "0600"
no_log: true$ ansible-playbook site.yml --ask-vault-pass
Vault password:
$ echo 'my-vault-password' > ~/.vault_pass && chmod 600 ~/.vault_pass
$ ansible-playbook site.yml --vault-password-file ~/.vault_passCommon mistakes
- Committing the vault password file to Git next to the vault: that is like taping the key to the lock.
- Running a playbook that uses a vault file without --ask-vault-pass or a password file: "Attempting to decrypt but no vault secrets found".
- Printing secrets with debug, or letting a failing task print them. Use no_log: true on tasks that handle secrets.
- Guessing System Role variable names. Copy them from the README in /usr/share/doc/rhel-system-roles/.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
RHEL System Roles and Ansible Vault · 1/6
RHEL System Roles are Red Hat's supported roles for common jobs like time sync and SELinux.
Prefer a video by a teacher? Find free videos on “rhel system roles timesync selinux ansible vault tutorial” on YouTube ↗
In real life: Passing an audit at a payments company
A Chennai payments company was told by auditors that database passwords were sitting in plain text in its Ansible Git repository. The team moved every secret into vault-encrypted files, stored the vault password in their secrets manager, added no_log to sensitive tasks, and rotated all passwords that had been exposed. They also switched their NTP and SELinux setup to RHEL System Roles so it was supported and consistent.
$ git grep -l 'ANSIBLE_VAULT' -- '*.yml'
group_vars/prod/vault.yml
group_vars/uat/vault.yml
$ git grep -n 'password:' -- '*.yml' | grep -v vault
(no output)Practice (0/3 done)
Which command changes the password of an encrypted vault file?
Before committing, a team checks that secret files are encrypted. Write unencrypted_secrets(files) where files maps a path to its text. Return the sorted paths that look like secret files (the name contains "vault" or "secret") but whose first line does NOT start with "$ANSIBLE_VAULT;".
Install rhel-system-roles. Write timesync.yml using the timesync role to point all hosts at two NTP servers, and selinux.yml using the selinux role to keep SELinux enforcing and turn on httpd_can_network_connect persistently on webservers. Store a fake db_password in a vault file and print its length (not its value) in a task.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Jinja2 templatesAnsible in depth · same module
- Loops, conditions and error handlingAnsible in depth · same module
- Roles, templates, Galaxy and VaultDevOps: Git, Docker, Kubernetes and CI/CD · Configuration management with Ansible, and YAML
- RHCE (EX294): automation with AnsibleCertifications and career
- What Linux and Red Hat Enterprise Linux areLinux and the shell
- Secrets managementDevOps: Git, Docker, Kubernetes and CI/CD · DevSecOps
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which package provides RHEL System Roles?
Answer: rhel-system-roles
rhel-system-roles.
What does the first line of a vault-encrypted file start with?
Answer: $ANSIBLE_VAULT;
$ANSIBLE_VAULT;1.1;AES256.
What does no_log: true do?
Answer: Hides the task's values from output and logs
Use it for tasks that handle secrets.
Where are example playbooks for system roles on RHEL?
Answer: /usr/share/doc/rhel-system-roles/
Each role has examples and a README there.
Which is a safe place for the vault password file?
Answer: In your home directory with mode 600, ignored by Git (or a secrets manager)
Never commit it.
Which is the safest way to keep a database password used by a playbook?Related
Answer: In an ansible-vault encrypted file, with the vault password kept outside Git
Vault-encrypted files can be committed; the vault password must stay out of Git.
Where can you find RHEL System Roles examples during the exam?Related
Answer: /usr/share/doc/rhel-system-roles/
Installed with the package.
Which file lists roles and collections to install for a project?Related
Answer: requirements.yml
ansible-galaxy install -r requirements.yml.
Which package gives you ansible-playbook on RHEL 9?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- This is the last topic of “Ansible in depth”. The module exam comes next.
- Module 10 exam: Ansible in depth · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Certifications and career. 57 topics left in the course before the final exam.