Giter VIP home page Giter VIP logo

ansible-lab-environment-in-containers's Introduction

A fully dockerized environment to play with ansible.

Prerequisites:

  • Docker
  • Docker Compose
  • [ kubectl ]

Lab Architecture :

Nodes

This Lab consists of 5 containers:

  • 1 control node container (control-node).

  • 4 managed hosts as containers :

    • Alpine with SSH connection (ansible_connection=ssh)
    • RHEL8 with docker connection (ansible_connection=docker)
    • CentOS7 with docker connection (ansible_connection=docker)
    • Alpine+systemd with docker connection (ansible_connection=docker)

All these containers are defined in docker-compose.yml

Playbook Sample

The playbook sample is under playbook-example/hello.yml.

It consists of 4 plays:

  • First play: target all managed hosts to create a simple file

  • Second play: targets the alpine node to run NodeJS app (running inside container on port 3000, running externally on port 3001 - check docker-compose )

  • Third play: targets both RedHat containers ( rhEL8, centos7) to run nginx (conclude exposed ports from docker-compose )

  • Fourth play: target Alpine-systemd container to run Apache Web server ( external port 8083)

Configuration

  • RHEL8 and CentOS7 containers does not need pre configuration as it used ansible_connection=docker.

  • Alpine container requires pre-configuration as ansible connects to it thru SSH :

  • Generate SSH key pair in ./secrets directory:
ssh-keygen -t rsa -N "" -f secrets/id_rsa

Using compose

Setup

docker-compose up -d

Play

## 
docker-compose exec controlnode ash
########
/playbook $ ansible all --list-hosts
## alpinesystemd-example-com
## alpine-example-com
## centos7-example-com
## rhel8-example-com

/playbook $ ansible all -m ping

Ansible Ping output

## 
docker-compose exec controlnode ash
########

# run the playbook sample:
/playbook $ ansible-playbook hello.yml

Playbook Hello  output

Validation

on your host, navigate to :

http://localhost:3001

http://localhost:8081

http://localhost:8082

http://localhost:8083

Cleanup

docker-compose down

Using Kubernetes + compose

Setup

export stackname=infra-ansible
export DOCKER_STACK_ORCHESTRATOR=kubernetes
docker stack deploy -c docker-compose.yml ${stackname}

Play

# Get podID of control node
controlnode_podid=$(kubectl get pods -lcom.docker.service.id=${stackname}-controlnode -o jsonpath='{.items[0].metadata.name}')
kubectl exec -it ${controlnode_podid} ash

########
/playbook $ ansible all --list-hosts 
/playbook $ ansible alpine-example-com -m ping
# run the playbook sample:
/playbook $ ansible-playbook hello.yml

Cleanup

docker stack rm ${stackname}

ansible-lab-environment-in-containers's People

Contributors

abdennour avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ansible-lab-environment-in-containers's Issues

Docker Compose Port Conflict and Service Replica Issues in Ansible Lab Environment

Description

I am encountering issues with my Docker Compose setup for an Ansible lab environment. Specifically, there are port conflicts when running multiple replicas of a service, and difficulties with fixed host port assignments for these replicas.

Environment

  • Docker version: 24.0.7
  • Docker Compose version: 1.29.2,
  • OS: ubuntu 22.04

Steps to Reproduce

  1. Run docker-compose up -d with the following Docker Compose configuration:

Error message

`bash
Creating alpinesystemd-example-com ...
WARNING: The "managedhost-alpine" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash.
Creating alpinesystemd-example-com ... done
Creating centos7-example-com ...
Creating centos7-example-com ... done
Creating ansible-lab-environment-in-containers_controlnode_1 ...
Creating ansible-lab-environment-in-containers_managedhost-alpine_1 ... done
Creating ansible-lab-environment-in-containers_managedhost-alpine_2 ...
Creating ansible-lab-environment-in-containers_managedhost-alpine_2 ... error
WARNING: Host is already in use by another container
Creating ansible-lab-environment-in-containers_managedhost-redhat-base_1 ... done

Creating rhel8-example-com ... done_managedhost-alpine_3 (0b10325726bc3a1231eab82fc08f09bb5e235baa6acfc001a4ea37c601d2c471): Bind for 0.0.0.0:3001 failed: port is already allocated

ERROR: for ansible-lab-environment-in-containers_managedhost-alpine_2 Cannot start service managedhost-alpine: driver failed programming external connectivity on endpoint ansible-lab-environment-in-containers
Creating ansible-lab-environment-in-containers_controlnode_1 ... done

ERROR: for managedhost-alpine Cannot start service managedhost-alpine: driver failed programming external connectivity on endpoint ansible-lab-environment-in-containers_managedhost-alpine_3 (0b10325726bc3a1231eab82fc08f09bb5e235baa6acfc001a4ea37c601d2c471): Bind for 0.0.0.0:3001 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.

`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.