Skip to content

Expert · Ansible in depth · topic 53 of 57 · 35 min

RHEL System Roles and Ansible Vault

Explanation

RHEL System Roles are Red Hat's supported roles for common jobs: time sync, SELinux, networking, storage, firewall and more. Install the rhel-system-roles package and use them by setting their variables. Ansible Vault encrypts files that hold secrets (passwords, keys) so they can sit safely in Git; you give the vault password when you run the playbook.

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.

bash
# 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  ...
Each role has a README and example playbooks under /usr/share/doc/rhel-system-roles/. The same roles are also in the collection redhat.rhel_system_roles.
yaml
- 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
You can also write the role name as rhel-system-roles.timesync.
yaml
- 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.selinux
Everything from the SELinux module of this course, as data. Check the README for the exact variable names in your version.

Other 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.

bash
$ 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
yaml
# 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
no_log: true stops Ansible printing the secret in its output and logs.
bash
$ 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_pass
A password file is handy for automation; keep it OUT of Git (add it to .gitignore) and readable only by you. With ansible-navigator, interactive prompts may need extra options; check ansible-navigator --help.

Common 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

timesync · selinux · network · storage · firewall

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.

bash
$ 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)

Task 1 · Question

Which command changes the password of an encrypted vault file?

Task 2 · Code · Python

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;".

Task 3 · Practice

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

Question 1

1.Which package provides RHEL System Roles?

Question 2

2.What does the first line of a vault-encrypted file start with?

Question 3

3.What does no_log: true do?

Question 4

4.Where are example playbooks for system roles on RHEL?

Question 5

5.Which is a safe place for the vault password file?

Read more (free)

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.

From Roles, templates, Galaxy and Vault

Where can you find RHEL System Roles examples during the exam?Related

Answer: /usr/share/doc/rhel-system-roles/

Installed with the package.

From RHCE (EX294): automation with Ansible

Which file lists roles and collections to install for a project?Related

Answer: requirements.yml

ansible-galaxy install -r requirements.yml.

From Roles, collections and ansible-galaxy

Which package gives you ansible-playbook on RHEL 9?Related

Answer: ansible-core

ansible-core.

From What Ansible is and how it works

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

  1. This is the last topic of “Ansible in depth”. The module exam comes next.
  2. Module 10 exam: Ansible in depth · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Certifications and career. 57 topics left in the course before the final exam.

← Previous: Roles, collections and ansible-galaxy