4.9 KiB
How to create all necessary entries for new (web service) VM
Let's assume that you want to add a new web service example.hamburg.ccc.de which is going to be hosted on the VM example on chaosknoten. These are the steps that you need to take to create the VM and add it to the Ansible repo.
IP, DNS, VM
- Allocate a fresh IPv6 in Netbox in the 2a00:14b0:42:102::/64 net. This will be the management address for the VM.
- Add an entry
example.hosts.hamburg.ccc.dewith that AAAA to the name server (ns-intern). - Add an entry
example.hamburg.ccc.deas a CNAME forpublic-reverse-proxyto the same zone. - Commit and reload the zone.
- Create a new VM on chaosknoten, for example by cloning the Debian template 9023. Give it the name
example. - Edit the ethernet interface to be connected to
vmbr0, VLAN tag2. - Configure the IPv6 address in the Cloud-Init section. Leave IPv4 set to DHCP.
- Make sure the VM is started at boot (options).
- Adjust any other VM parameters as needed.
- Boot the VM.
- Add the VM to Netbox. Make sure to enter the VM ID.
- Add an Ethernet interface to the VM; we typically use
eth0as a name. - Add IP for that interface, then choose "Assign IP" and search for the IP you've created. Make it the primary IP of that interface.
Ansible Basics
As the first step, we need to make the host known to Ansible.
- In
.sops.yaml, add an entry for the host. Follow the other entries there.keys.hosts.chaosknoten.ageneeds an age public key (the private key is needed in the host-specific YAML)creation_rulesneeds an entry for the host, referencing the age key.
- In
inventories/chaosknoten/hosts.yaml:- Configure basic connection info:
You typically will want to use router as a jump host so that you can run Ansible on an IPv4 only connection.example: ansible_host: example.hosts.hamburg.ccc.de ansible_user: chaos ansible_ssh_common_args: -J ssh://chaos@router.hamburg.ccc.de - Add the host to the desired roles. As a minimum, you'll want
base_config_hostsandinfrastructure_authorized_keys_hosts. For a typical web service based on Docker Compose, you'll wantdocker_compose_hosts,nginx_hosts, andcertbot_hosts.
- Configure basic connection info:
- In the directorry
inventories/chaosknoten/host_var/:- A file
inventories/chaosknoten/host_var/example.yamlwith the host/service specific configuration. - A file
inventories/chaosknoten/host_var/example.sops.yamlwith the encrypted secrets for the host/service. Runsops inventories/chaosknoten/host_var/example.yamlto edit/create that file. Entries here should generally be prefixed withsecret__to make it easier to see where that variable is coming from in templates etc.- Add an entry
ansible_pull__age_private_keywith the age private key you generated above.
- Add an entry
- A file
Service-specific config
From here, we go into the details of the web service that you want to configure. For a typical web service with Docker Compose, you will likely want to configure the following.
Make inventories/chaosknoten/host_var/example.yaml look like this:
certbot__version_spec: ""
certbot__acme_account_email_address: le-admin@hamburg.ccc.de
certbot__certificate_domains:
- "example.hamburg.ccc.de"
certbot__new_cert_commands:
- "systemctl reload nginx.service"
docker_compose__compose_file_content: "{{ lookup('ansible.builtin.template', 'resources/chaosknoten/example/docker_compose/compose.yaml.j2') }}"
nginx__version_spec: ""
nginx__configurations:
- name: example.hamburg.ccc.de
content: "{{ lookup('ansible.builtin.file', 'resources/chaosknoten/spaceapiccc/nginx/example.hamburg.ccc.de.conf') }}"
This will create compose.yaml from the template resources/chaosknoten/example/docker_compose/compose.yaml.j2', and the nginx config from resources/chaosknoten/spaceapiccc/nginx/example.hamburg.ccc.de.conf. Of course, depending on your service, you might need additional entries. See the other hosts and the roles for more info.
First Ansible run
Before you can run Ansible successfully, you will want to make sure you can connect to the VM, and that the host key has been added to your known hosts:
ssh chaos@example.hosts.hamburg.ccc.dessh -J chaos@router.hamburg.ccc.de chaos@example.hosts.hamburg.ccc.de
Then run Ansible for public-reverse-proxy to add the necessary entries: ansible-playbook playbooks/deploy.yaml --inventory inventories/chaosknoten/hosts.yaml --limit public-reverse-proxy.
Finally run Ansible for the new host: ansible-playbook playbooks/deploy.yaml --inventory inventories/chaosknoten/hosts.yaml --limit example
Commit your changes
Do not forget to commit your changes, whether it's a new host or you are making changes to an existing host.
And always git pull before you run Ansible so avoid reverting anything!