Clinical-Genomics / event-driven-architecture

Project tracking for event driven POC
0 stars 0 forks source link

2. [Guide] Using Ansible #10

Closed seallard closed 5 months ago

seallard commented 6 months ago

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.

pip install pipx
pipx install --include-deps ansible
pipx ensurepath
brew install sshpass

2. Verify Ansible install.

Open a new shell.

ansible --version

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.

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.

sshpass -p 'passwd' ssh ansible_user@localhost -p 2222

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.

[local]
localhost ansible_port=2222 ansible_user=ansible_user ansible_password=passwd ansible_become_password=passwd

2. Create an Ansible playbook.

Create an Ansible playbook called hello_world_playbook.yaml with the following content.

---
- name: Hello World Playbook
  hosts: local
  become: true

  tasks:
    - name: Create a hello world file
      copy:
        content: "Hello, World!\n"
        dest: /tmp/hello.txt

3. Configure fake VM.

Use the inventory and playbook to configure the fake VM with Ansible.

ansible-playbook -i inventory hello_world_playbook.yaml

4. Verify that the fake VM was configured.

Open a shell in the container.

docker exec -it fake_vm_container /bin/bash

Verify that the playbook was applied.

cat /tmp/hello.txt
seallard commented 6 months ago

I'll move this guide into Atlas at a later stage.

ChrOertlin commented 6 months ago

Done!

seallard commented 6 months ago

done

diitaz93 commented 5 months ago

Done ✅

seallard commented 5 months ago

@islean any progress? Done!