This is a repo where you can create an ansible lab setup and start learning ansible.
This setup helps to create a docker environment where you can setup ansible and start learning ansible.
Using docker as an option for learning ansible is better since it is light-weight solution compared to having VMs and can be easily created. Also, in case if you make a mistake you can easily create your environment using the docker-compose
file.
There are three types of working hosts and one controller host in this project. Feel free to change them as you wish. the roles in this setup is as follows:
- Loadbalancer (1x): A sing host running Nginx as loadbalancer
- Web-Server (2x): 2 hosts running Apache as the web server
- Database (1x): Database host running MysqlDB
- Controller (1x): Controller host that runs Ansible and automates the configuration of above-mentioned nodes
NOTE: checkout the docker-compose.yml
file to change the number of each machine. The current version of the docker-compose file creates the environment below:
There are two docker volumes in this setup, one of them is a named volume
and the other one is host volume
:
-
The first one (
named volume
) is namedansible_vol
in thedocker-compose
file and is used to share files (in this case ssh-keys that are created by thecontroller
) among hosts. These files will be used in therun.sh
script. -
The second one is a
host volume
which is bind mountsansible-files
to/code
in the controller so whatever script you create or modify inansible-files/codes/
can be easilty accessed from within thecontroller
Here is a diagram of how the volume setup is:
- Clone the repo
git clone https://github.com/BeardedDonut/ansible-lab
- Navigate to the
hosts-docker
directorycd hosts-docker
- Change the
docker-compose
file, according to your prefered setup - Deploy the setup
docker-compose up -d --build
- Connect to the
controller
nodedocker container exec -it controller bash
- Run zsh
# OPTIONAL: you can also change the zsh theme if you like by modify ~/.zshrc # I personally changed the theme to "alanpeabody" zsh # this might take a few seconds to run
- Go to
/codes/
directory and run the commands below (Note: this directory is share with your host)cd /codes/ # start ssh agent ssh-agent zsh # add master key to ssh agent ssh-add /common/master_key # NOTE: you will be prompted for entering passphrase it 12345
- ping all the hosts
ansible -m ping all # NOTE: You will receive this message: ECDSA key fingerprint is ECDSA key fingerprint is SHA256:ywVL4dyWYdwCipVSqmYzIZPlUA0Q85AaMVe4IbI9ca. Are you sure you want to continue connecting (yes/no)? # Type "yes" and hit enter, do this four times,
- Redo step 8 and you should see something like this:
control | SUCCESS => { "changed": false, "ping": "pong" } lb01 | SUCCESS => { "changed": false, "ping": "pong" } web01 | SUCCESS => { "changed": false, "ping": "pong" } db01 | SUCCESS => { "changed": false, "ping": "pong" } web02 | SUCCESS => { "changed": false, "ping": "pong" }
the ansible-files
directory contains all the required playbooks,templates and the demo flask application files. The codes
directory will be bind mounted to the controller
host and is accessible from /codes
directory inside host.
Ther reason behind this is that if you can edit/update the files on your own environment (not controller
) and have them updated inside the controller
as well. A brief description of what each directory contains is in the following:
- ansible-files/codes/demo: demo flask application files and configurations
- ansible-files/codes/service-playbooks: Ansible playbooks to setup each host
- ansible-files/codes/templates: Templates that are used for setting up this environment
- ansible-files/codes/utility-playbooks: Ansible playbooks that usually used against the entire stack and are usually for troubleshooting and stack monitoring.
After setting up the environment, you can setup the loadbalancer, webserver and database. Follow the commands below to setup all services (Note: you should run these commands from inside the controller)
- Navigate to
/codes/service-playbooks
directory# inside the controller container cd /codes/service-playbooks/
- Run the setup playbook for each service:
# inside the controller container # 1. setup controller ansible-playbook controller.yml # 2. setup database ansible-playbook database.yml # 3. setup webserver ansible-playbook webserver.yml # 4. setup load-balancer ansible-playbook loadbalancer.yml
- To make sure everything is working run the following commands
# insdie controller container curl lb01 # >>> "Hello, from sunny web01!" or "Hello, from sunny web02!" curl lb01/db # >>> "Database Connected from web01!" or "Database Connected from web02!"
Note: I used this repo to create the setup and it was really helpful