Linux Labs

Automation: Git and Ansible / Stage 7 of 8

Roles, variables and secrets

Not running Starting usually ~20 s
Leave the lesson

Step 1 of 6 · on a machine · 5 min

The edit reached one site out of three

What you will learn

  • Move the tasks that playbooks share into a role roles/<name>/ — tasks, handlers, templates and default values — and call it from a play with the roles list.
  • Set variables in a role's defaults, group_vars, host_vars, a play's vars and -e, and know in advance whose value the template gets.
  • Encrypt secrets with ansible-vault and run a playbook with the vault password from a file outside the project.

Three sites run on app1 — shop, blog and docs, each with its own playbook in ~/ansible-lab. A colleague wrote shop.yml for Ansible and copied it for the other two sites. Yesterday he changed the support phone on the pages to 8-800-555-02-02 and ran all three playbooks. See what the sites return and what the playbooks say.

Exercise · look

Look at the sites and the playbooks

In ~/ansible-lab see what the three sites return, and find the support phone and the database password in the playbooks.

First, predict

Your colleague changed the phone and ran all three playbooks without errors. On how many sites will curl show 8-800-555-02-02?

Now start the exercise, run the command and compare your prediction with the output.

Sign in and start The machine is available after signing in
Check yourself 1 question

Your colleague changed the phone in shop.yml and ran all three playbooks, yet blog and docs show the old number. Why?

  • The runs went through, and the blog and docs pages are as their playbooks say: grep -n '8-800' *.yml shows the old number in blog.yml and docs.yml.

  • nginx reads the page from disk on every request: cat /srv/blog/index.html shows the same number as curl.

  • Each run applies its own playbook in full. Nobody wrote the new number into blog.yml and docs.yml — grep -n '8-800' *.yml.

The three playbooks are independent copies of the tasks, and the other two do not see an edit in shop.yml.

Ubuntu 24.04
Loading…