Chapter 1: Build Your Practice Lab

First project and daily use

Phases 13 and 14. With the baseline saved, everything from here on is your own work. Create the base Ansible project the exercises copy from, prove it reaches every server, and set up a one-command way into the lab from your own computer.

Phase 13 · Your first Ansible project (~10 min)

  • Create ~/ansible with ansible.cfg and an inventory.
  • Prove SSH, the devops user and sudo work on every server.

VM: create the project folder

It lives on workstation, so resetting the servers never touches it. Get there with rht-vmctl ws.

mkdir -p ~/ansible/{roles,collections} && cd ~/ansible

VM: write ansible.cfg

[defaults]
inventory          = ./inventory
remote_user        = devops
roles_path         = ./roles:/usr/share/ansible/roles
collections_path   = ./collections:~/.ansible/collections:/usr/share/ansible/collections
host_key_checking  = False
interpreter_python = auto_silent     # stops the "discovered Python interpreter" warning

[privilege_escalation]
become          = True
become_method   = sudo
become_user     = root
become_ask_pass = False

Every setting here is explained in chapter 3.

VM: write the inventory

A set of groups that is handy for practice:

[dev]
servera.lab.example.com

[test]
serverb.lab.example.com

[prod]
serverc.lab.example.com
serverd.lab.example.com

[webservers:children]
prod

VM: prove it reaches every server

ansible-inventory --graph
ansible all -m ping                  # every server: "pong"
ansible all -m command -a id         # uid=0(root) everywhere
ansible --version | head -1          # ansible [core 2.14.18]

VM: put it under Git

Restoring workstation to clean removes ~/ansible, so keep your work in Git and commit as you go:

cd ~/ansible && git init

Daily use

Getting in

Getting into the lab: SSH to the host as usual, step into workstation with lxc exec, then work exactly as in the classroom. The ssh rhce shortcut does the first two hops in one command.

On the host, rht-vmctl ws drops you at the student prompt on workstation. From your own computer, one SSH alias takes you straight there:

Host rhce
    HostName <HOST_LAN_IP>
    User <HOST_USER>
    RequestTTY yes
    RemoteCommand lxc exec --project rhce workstation -- su - student
ssh rhce            # lands on [student@workstation ~]$

Starting and stopping the lab

rht-vmctl stop all       # frees ~7 GiB of RAM
rht-vmctl start all

Moving files in and out

Because the host can't reach the VMs over the network, copy files through LXD instead:

lxc file push notes.txt workstation/home/student/            # host → VM
lxc file pull workstation/home/student/ansible/site.yml .     # VM → host

Before each exercise in this guide

rht-vmctl reset servers on the host.

lab start NAME on workstation. It creates ~/NAME with that exercise's ansible.cfg, inventory and starter files.

firewalld runs on every server, as in the classroom. If an exercise sets up a web server, the playbook has to open the port (ansible.posix.firewalld) before curl from workstation works.

Run the playbook, verify the result, then run it again: the second run should report changed=0.

lab finish NAME on workstation puts the project away in ~/lab-archive/. Then reset the servers on the host.