Ansible for devops without Vagrant

November 12, 2023, Update to add: One reason to prefer Vagrant over docker is actually because of the mission involved in setting up projects running services. Since containers typically run single processes, systemd isn't necessary to coordinate these processes.

 

 So I'm currently working through Jeff Geerling's great book Ansible for Devops.


However, I wasn't so keen on installing and running vagrant when everything should be doable (I believe) using docker-compose.

At the same time, I'm going through his book so that I can better manage my few Raspberry Pis at home, and I'd like to be able to test my ansible scripts before running them on the live pis.

 

So what I decided to do was kill two birds with one repo and set up a docker-compose based test environment that can run my ansible scripts, but that mirrors my current ripi setup (mostly).

 

Home network

My current homelab is nothing special. Three pis -- coffee, xray, and pizza -- each with a static address on my private network and a dns record pointing to it.
 
So the idea was really simply to set up a docker-compose .yaml file with three services mirroring those, and a primary service I've called "driver" that I can exec into and run ansible from.
 
If you take a look at the "driver" service you'll see I map whatever is in the "ansible" directory to the root's home directory - this should therefore contain your playbooks, inventory, etc. and it'll show up in "driver"'s home.
 
The `ansible/inventory` file simply contains the name of the three services.
 

The Dockerfile


The Dockerfile is pretty simple, but there is some interesting stuff in it.
We install openssh-server and ansible.

In order to make things simple, we generate an ssh-key for root in the container, and then we copy the id_rsa.pub into `authorized_keys` in order to allow any of the containers to ssh into any of the others. Since they share a network, they should just be able to access one another.

Finally, I set an ssh config that disables strict host checking (although it may actually make sense to limit this to xray, pizza, and coffee. I might make this change).


Getting it to work

Once you've cloned the repo, it's as simple as running `docker-compose up` to get the services up and running.
You can then docker-compose exec into "driver" and start running commands.






Comments

Popular posts from this blog

JCE's Gifted and Talented Center

100 Go Mistakes 10-16

100 Go Mistakes: 1-9