This guide provides instructions on how to setup Ansible, a container and how to configure it using Ansible. Once you have gone through the guide, you will
Have Ansible installed on your local machine.
Have a container that you can configure with Ansible.
Know how to configure machines with Ansible.
Install Ansible
1. Install Ansible and dependencies.
Use any environment on your local machine, as long as Python 3.8 or later is available in it.
Ansible applies configurations to target machines. In our context at CG, this is usually a VM provisioned by SciLifelab IT. Setting up at local VM is messy, below follows a guide on how to setup a local Docker container that can be configured with Ansible.
1. Create directory.
mkdir ansible_test
cd ansible_test
2. Create a Dockerfile.
Create Dockerfile called Dockerfile in the directory with the following content.
FROM ubuntu:20.04
RUN apt-get update && \
apt-get install -y openssh-server sudo && \
mkdir /var/run/sshd
# Add root user
RUN useradd -rm -d /home/docker -s /bin/bash -g root -G sudo -u 1000 ansible_user && \
echo 'ansible_user:passwd' | chpasswd
# Allow root login via SSH
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# Allow password authentication
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
# Expose port 22 for SSH
EXPOSE 22
# Start the SSH service
CMD ["/usr/sbin/sshd", "-D"]
3. Build an image.
Ensure Docker is running and build an image called fake_vm_image using the Dockerfile.
docker build -t fake_vm_image .
4. Start container.
Start container using the image.
docker run -d -p 2222:22 --name fake_vm_container fake_vm_image
5. Verify that the container is running.
The fake_vm_container should be listed.
docker ps
6. Verify that it is possible to SSH into the container.
Guide
This guide provides instructions on how to setup Ansible, a container and how to configure it using Ansible. Once you have gone through the guide, you will
Install Ansible
1. Install Ansible and dependencies.
Use any environment on your local machine, as long as Python 3.8 or later is available in it.
2. Verify Ansible install.
Open a new shell.
Setup target machine to configure
Ansible applies configurations to target machines. In our context at CG, this is usually a VM provisioned by SciLifelab IT. Setting up at local VM is messy, below follows a guide on how to setup a local Docker container that can be configured with Ansible.
1. Create directory.
2. Create a Dockerfile.
Create Dockerfile called
Dockerfile
in the directory with the following content.3. Build an image.
Ensure Docker is running and build an image called
fake_vm_image
using the Dockerfile.4. Start container.
Start container using the image.
5. Verify that the container is running.
The
fake_vm_container
should be listed.6. Verify that it is possible to SSH into the container.
Use Ansible to configure the container
Exit the SSH, the following steps are done in the
ansible_test
directory on your local machine.1. Create an Ansible inventory.
Create an Ansible inventory file called
inventory
with the following content.2. Create an Ansible playbook.
Create an Ansible playbook called
hello_world_playbook.yaml
with the following content.3. Configure fake VM.
Use the inventory and playbook to configure the fake VM with Ansible.
4. Verify that the fake VM was configured.
Open a shell in the container.
Verify that the playbook was applied.