Skip to content

Setting Up Ansible Environment and Hosts #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f1f2f95
add web-dev, db-dev, volumes and networks
Shafiya-Heena Jun 10, 2024
3a20c25
structured the directory names
Shafiya-Heena Jun 10, 2024
a1f058e
contains web-dockerfile and config files
Shafiya-Heena Jun 10, 2024
07e0932
wordpress setup scripts along with config files
Shafiya-Heena Jun 10, 2024
a444808
remove setup and staff scripts temporarly
Shafiya-Heena Jun 11, 2024
816dda9
remove .env and .gitignore
Shafiya-Heena Jun 11, 2024
eb562bc
update docker-compose
Shafiya-Heena Jun 11, 2024
55a8f08
add .env.example and .gitignore as it is needed for docker-compose
Shafiya-Heena Jun 11, 2024
2405ac6
making the environment more generic and removing the index related files
Shafiya-Heena Jun 11, 2024
c5055ea
update Dockerfile
Shafiya-Heena Jun 11, 2024
29a81d7
replacing index with dev
Shafiya-Heena Jun 11, 2024
e0b78f6
replacing index with dev
Shafiya-Heena Jun 11, 2024
c42e6eb
update Dockerfile
Shafiya-Heena Jun 11, 2024
75fba7e
update to add ssh for ansible
Shafiya-Heena Jun 12, 2024
f2db65c
update for ssh in web
Shafiya-Heena Jun 12, 2024
56ebddb
add sysadmin-ssh-keys dir to store keys generated by script
Shafiya-Heena Jun 12, 2024
83d17bc
updated to remove authorized_keys file
Shafiya-Heena Jun 12, 2024
ac06af8
update generate script
Shafiya-Heena Jun 13, 2024
22a6b11
remove version
Shafiya-Heena Jun 13, 2024
d271002
updated to use interpolation in docker-compose
Shafiya-Heena Jun 13, 2024
092b880
update README
Shafiya-Heena Jun 13, 2024
cd5a29e
update readme
Shafiya-Heena Jun 13, 2024
f861973
update README
Shafiya-Heena Jun 13, 2024
e4482dc
adjust the spacing
Shafiya-Heena Jun 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# WordPress variables
WP_VERSION=6.3.1

# Setup WordPress variables
WP_ADMIN_EMAIL=
WP_ADMIN_USER=
WP_ADMIN_PASS=

7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

# Local configuration
.env

#SSH keys
sysadmin-ssh-keys/rsa_sysadmin*
sysadmin-ssh-keys/authorized_keys
59 changes: 54 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ See [`CONTRIBUTING.md`][org-contrib].

The aim of the project is to establish a robust and localized development environment utilizing Ansible and Docker. This environment will mirror a professional work setting, incorporating a dedicated security server (Bastion), automation through Ansible, a web server, and a data storage server. This configuration will simplify and secure development processes and serve as a blueprint for future projects at CC.

Docker containers:

- Bastion (SSH jump server)
- Ansible
- Web server (Apache2/WordPress)
- Database server (MariaDB)
#### Docker containers:

The [`docker-compose.yml`](docker-compose.yml) file defines the following
containers:

- WIP: Bastion (SSH jump server)
- **ansible-dev** - Ansible
- **web-dev** - Web server (Apache2/WordPress)
- **db-dev** - Database server (MariaDB)


![image](https://github.com/creativecommons/ansible-dev/assets/90766122/21baa18d-715e-4908-9620-15c768994011)
Expand All @@ -40,6 +44,51 @@ Docker containers:
See [Create Local Ansible Dev Environment Using Docker](https://opensource.creativecommons.org/programs/project-ideas/#ansible-dev-env) for more details.


### Setup

- Create the `.env` file:
```shell
cp .env.example .env
```

- Execute the `generate_ssh_keys` script:
```shell
./generate_ssh_keys.sh
```

- Build and start Docker:
```shell
docker-compose up
```

- Wait for the build and initialization to complete


#### SSH (Work in Progress)

The SSH setup has been established and is currently in use for the Ansible container. Follow the steps below to generate and use the SSH keys for the sysadmin user:

- Execute the generate-ssh-keys.sh script to generate the keys used by the sysadmin user:
```shell
./generate-ssh-keys.sh
```

- Bring down the existing Docker containers and start them again:
```shell
docker-compose down
docker-compose up -d
```

- Ensure the Docker containers are running:
```shell
docker ps
```

- Execute the following command to confirm that SSH is working fine:
```shell
ssh -i ./sysadmin-ssh-keys/rsa_sysadmin -p 22001 sysadmin@localhost
```

## Related Links
- [Ansible Documentation](https://docs.ansible.com/)
- [FrontPage - Debian Wiki](https://wiki.debian.org/FrontPage)
Expand Down
46 changes: 30 additions & 16 deletions ansible/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# https://docs.docker.com/engine/reference/builder/

# https://hub.docker.com/_/debian
FROM debian:bookworm-slim

Expand All @@ -9,7 +8,7 @@ ARG DEBIAN_FRONTEND=noninteractive
# Configure apt to avoid installing recommended and suggested packages
RUN apt-config dump \
| grep -E '^APT::Install-(Recommends|Suggests)' \
| sed -e's/1/0/' \
| sed -e 's/1/0/' \
| tee /etc/apt/apt.conf.d/99no-recommends-no-suggests

# Resynchronize the package index files from their sources
Expand All @@ -20,26 +19,41 @@ RUN apt-get install -y \
python3 \
python3-pip \
python3-venv \
openssh-client
openssh-client \
openssh-server \
wget \
vim \
sudo

# Clean up packages: Saves space by removing unnecessary package files and lists
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Create sysadmin user and add to sudoers
RUN useradd -m -s /bin/bash sysadmin && echo "sysadmin:sysadmin" | chpasswd && \
usermod -aG sudo sysadmin

# Ensure SSH directory exists with correct permissions
RUN mkdir -p /home/sysadmin/.ssh && \
chown sysadmin:sysadmin /home/sysadmin/.ssh && \
chmod 700 /home/sysadmin/.ssh

# Clean up packages: Saves space by removing unnecessary package files
# and lists
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/*
# Create privilege separation directory for SSH
RUN mkdir -p /run/sshd

# Create a virtual env and install ansible using pip
RUN python3 -m venv /opt/ansible-venv --system-site-packages && \
/opt/ansible-venv/bin/pip install --no-cache-dir ansible
# Create a virtual environment and install Ansible using pip
RUN python3 -m venv /opt/ansible-venv --system-site-packages && \
/opt/ansible-venv/bin/pip install --no-cache-dir ansible

# Create a directory for Ansible configuration
RUN mkdir /etc/ansible/

# Copy local configuration files to the image
COPY ../config/ /etc/ansible/

# Set environment variables for Ansible
ENV PATH="/ansible-venv/bin:$PATH"
ENV PATH="/opt/ansible-venv/bin:$PATH"
ENV ANSIBLE_CONFIG=/etc/ansible/ansible.cfg

# Set the default command to run Ansible
CMD ["ansible", "--version"]
# Expose SSH port
EXPOSE 22

# Start SSH service
CMD ["/usr/sbin/sshd", "-D"]

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[defaults]
inventory = /etc/ansible/hosts
remote_user = root
remote_user = sysadmin
host_key_checking = False
retry_files_enabled = False
9 changes: 9 additions & 0 deletions ansible/etc-ansible-config/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[local]
localhost ansible_connection=local

[web]
web-dev

[db]
db-dev

2 changes: 0 additions & 2 deletions config/hosts

This file was deleted.

88 changes: 83 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,91 @@
# https://docs.docker.com/compose/compose-file/

services:

ansible-dev:
container_name: ansible
container_name: ansible-dev
build:
context: .
dockerfile: ansible/Dockerfile
networks:
- dev-backend
volumes:
- ./ansible/etc-ansible-config:/etc/ansible/
- ./sysadmin-ssh-keys/rsa_sysadmin:/home/sysadmin/.ssh/id_rsa:ro
- ./sysadmin-ssh-keys/rsa_sysadmin.pub:/home/sysadmin/.ssh/id_rsa.pub:ro
- ./sysadmin-ssh-keys/rsa_sysadmin.pub:/home/sysadmin/.ssh/authorized_keys:ro
ports:
- "22001:22"
environment:
- USER=sysadmin
entrypoint: |
sh -c "
exec /usr/sbin/sshd -D
"

web-dev:
container_name: web-dev
depends_on:
- db-dev
build:
args:
WP_VERSION: ${WP_VERSION:?have you copied .env.example to .env?}
context: .
dockerfile: web/Dockerfile
networks:
- dev-backend
environment:
MYSQL_ROOT_PASSWORD: root
PMA_HOST: db-dev
PMA_PORT: 3306
WORDPRESS_CONFIG_EXTRA: |
# Use dispatch port by default
if ('${CODESPACE_NAME:-}') {
define('WP_HOME', 'https://${CODESPACE_NAME:-}-8080.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN:-}');
} else {
define('WP_HOME', 'http://localhost:8080');
define('WP_SITEURL', 'http://localhost:8080');
}
WORDPRESS_DB_HOST: db-dev:3306
WORDPRESS_DB_PASSWORD: root
WORDPRESS_DB_USER: root
WORDPRESS_DB_NAME: wordpress
WORDPRESS_USER: root
init: true
ports:
- '8080:80'
- '22002:22'
restart: on-failure
volumes:
- ./config/ansible.cfg:/etc/ansible/ansible.cfg
- ./config/hosts:/etc/ansible/hosts
command: sh -c 'trap "exit" TERM; while true; do sleep 1; done'
- ./web/config-web/etc-apache2-sites-available:/etc/apache2/sites-available:ro
- ../cc-legal-tools-data:/var/www/git/cc-legal-tools-data:ro
- ../chooser:/var/www/git/chooser:ro
- ../faq:/var/www/git/faq:ro
- ../mp:/var/www/git/mp:ro
- wp-data:/var/www/dev
- ./sysadmin-ssh-keys/rsa_sysadmin:/home/sysadmin/.ssh/id_rsa:ro
- ./sysadmin-ssh-keys/rsa_sysadmin.pub:/home/sysadmin/.ssh/id_rsa.pub:ro
- ./sysadmin-ssh-keys/rsa_sysadmin.pub:/home/sysadmin/.ssh/authorized_keys:ro

db-dev:
container_name: db-dev
environment:
MYSQL_DATABASE: wordpress
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: root
image: mariadb
networks:
- dev-backend
restart: on-failure
volumes:
- db-data:/var/lib/mysql

volumes:
db-data:
name: db-data
wp-data:
name: wp-data

networks:
dev-backend:
name: dev-backend

18 changes: 18 additions & 0 deletions generate_ssh_keys.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

# Define key parameters
KEY_PATH=./sysadmin-ssh-keys
KEY_FILENAME=rsa_sysadmin

# Create a directory to store the SSH keys if it doesn't exist
mkdir -p ${KEY_PATH}

# Generate SSH key pair only if they don't already exist
if [ ! -f ${KEY_PATH}/${KEY_FILENAME} ]
then
ssh-keygen -b 4096 -t rsa -C sysadmin -f ${KEY_PATH}/${KEY_FILENAME} -N ''
echo "SSH keys generated and stored in ${KEY_PATH}/${KEY_FILENAME}"
else
echo "SSH keys already exist in ${KEY_PATH}/${KEY_FILENAME}"
fi

2 changes: 2 additions & 0 deletions sysadmin-ssh-keys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This directory contains the SSH keys used for the sysadmin user.

Loading