Ansible & Semaphore
In this section Ansible and Semaphore technologies are introduced. Also, an example combining both platforms with the eManager will be presented.
Ansible
Ansible provides open-source automation that reduces complexity and runs everywhere. Using Ansible lets you automate virtually any task. It is known for its simplicity and flexibility, not only automates configuration management but also facilitates device monitoring.
Ansible is designed around the following principles:
- Agent-less architecture: Low maintenance overhead by avoiding the installation of additional software across IT infrastructure.
- Simplicity: Automation playbooks use straightforward YAML syntax for code that reads like documentation. Ansible is also decentralized, using SSH existing OS credentials to access to remote machines.
- Scalability and flexibility: Easily and quickly scale the systems you automate through a modular design that supports a large range of operating systems, cloud platforms, and network devices.
- Idempotence and predictability: When the system is in the state your playbook describes Ansible does not change anything, even if the playbook runs multiple times.
Semaphore
Semaphore is a continuous integration and delivery platform, offers seamless integration with monitoring tools, enabling users to incorporate monitoring into their development pipelines effortlessly.
Example of use with the eManager
This example is only a proof of concept to show how eManager can be integrated with these tools.
In this example we will:
- manage two eManagers in our local WiFi Network
- use Semaphore server running in PC via docker.
- use asymmetric keys for user credentials.
Note
The process detailed below has been tested with Debian 10 and Ubuntu 20.04.
eManagers settings
Here we generate a pair of asymmetric keys to simplify SSH login for the Semaphore server.
Security issues
Please, keep in mind that this process will make your PC gain free SSH access to all the eManagers. Also, make sure you don't share the content of ~/.ssh/semaphore_key
to anyone.
- Connect the eManagers and the PC to the same WiFi network.
- Determine the WiFi IP of every eManager via in each eManager terminal.
- Store all the IP addresses in a file for now. We'll use them all along this tutorial.
- We will use asymmetric keys in order to allow SSH access to the Semaphore server. Execute in your PC terminal in order to generate aymmetric keys.
- Add this key to the list of authorized keys for each eManager. That is, for every eManager IP execute the following in your PC. Replace
<eManager IP>
by the corresponding IP address then executeSSH_COMMAND="exec sh -c 'cd; umask 077; mkdir -p ".ssh" && { [ -z `tail -1c .ssh/authorized_keys 2>/dev/null` ] || echo >> ".ssh/authorized_keys" || exit 1; } && cat >> ".ssh/authorized_keys" || exit 1;'" PUB_KEY=$(cat ~/.ssh/semaphore_key.pub) printf '%s\n' "${PUB_KEY}" | { ssh -a -x root@"${IP}" "${SSH_COMMAND}"; } || echo "Ups! Something went wrong."
Installing Semaphore and Ansible in your PC
- Install docker and Ansible in your laptop:
- Let's create a new docker compose project. Create a new directory
- Create a
docker-compose.yml
file insemaphore_docker
directory with the following content:services: mysql: restart: unless-stopped image: mysql:8.0 hostname: mysql volumes: - semaphore-mysql:/var/lib/mysql environment: MYSQL_RANDOM_ROOT_PASSWORD: 'yes' MYSQL_DATABASE: semaphore MYSQL_USER: semaphore MYSQL_PASSWORD: semaphore semaphore: restart: unless-stopped ports: - 3000:3000 image: semaphoreui/semaphore:v2.9.37 volumes: - ./semaphore-example/:/data/demo-project environment: SEMAPHORE_DB_USER: semaphore SEMAPHORE_DB_PASS: semaphore SEMAPHORE_DB_HOST: mysql SEMAPHORE_DB_PORT: 3306 SEMAPHORE_DB_DIALECT: mysql SEMAPHORE_DB: semaphore SEMAPHORE_PLAYBOOK_PATH: /tmp/semaphore/ SEMAPHORE_ADMIN_PASSWORD: admin SEMAPHORE_ADMIN_NAME: admin SEMAPHORE_ADMIN_EMAIL: admin@localhost SEMAPHORE_ADMIN: admin SEMAPHORE_ACCESS_KEY_ENCRYPTION: gs72mPntFATGJs9qK0pQ0rKtfidlexiMjYCH9gWKhTU= ANSIBLE_HOST_KEY_CHECKING: False depends_on: - mysql volumes: semaphore-mysql:
- Clone our git example:
- Bring up the docker containers with
- If everything went right the terminal should print
Don't close this terminal until you are done with Semaphore.
semaphore-1 | You can login with admin@localhost or admin. semaphore-1 | Run Semaphore with semaphore server --config /etc/semaphore/config.json semaphore-1 | Loading config semaphore-1 | Validating config semaphore-1 | MySQL semaphore@mysql:3306 semaphore semaphore-1 | Tmp Path (projects home) /tmp/semaphore semaphore-1 | Semaphore v2.9.37 semaphore-1 | Interface semaphore-1 | Port :3000 semaphore-1 | Server is running
- If the docker above is running, writing localhost:3000 in your browser should open your local Semaphore UI.
Use credentials
admin
/admin
. Then pressSIGN IN
. - A
Create New Project
menu should appear. In this example we useeManagers example
for the Project Name, and let the other fields empty. Then clickCREATE
. An empty project will appear.
Defining the Repository
Let's tell Semaphore where to find the ansible files. In our case we use the GitHub repo we cloned above. As a consequence, this Repository is just a local project.
Let's configure the Repository.
-
Let's register a new empty key, this is an auxiliary key. Click
Key Store
and then clickNEW KEY
. UseNone
for the Key Name, chooseNone
for type then pressCREATE
. -
Let's configure the local Repository. Click
Repositories
and then clickNEW REPOSITORY
with the following fields
Then press CREATE
.
Note
The path /data/demo-project
is mapped to semaphore-example
in the docker-compose file.
Create an Environment
Let's create an empty environment. Click Environment
and then click NEW ENVIRONMENT
.
Use Empty
for the Environment Name and write {}
in the Extra variables box. Then press SAVE
.
Create an Inventory
In this section we tell Ansible Semaphore where to find the fleet of eManagers that should manage. This typically has two steps:
- identify a list of IP corresponding to the eManagers.
- choose the type of user credentials.
For the first step, edit in a new terminal the file semaphore_docker/semaphore-example/invs/devices/hosts
.
[devices]
192.168.0.25 ansible_connection=ssh ansible_python_interpreter=/usr/bin/python3
192.168.0.38 ansible_connection=ssh ansible_python_interpreter=/usr/bin/python3
For the second step, we will use the asymmetric keys created above.
- Execute in the PC terminal. Copy the output to the clipboard.
- Open the Semaphore web. Click
Key Store
and then clickNEW KEY
. Fill the form as follows - Click
Inventory
and then clickNEW INVENTORY
. Fill the form as follows
Create a task
Our GitHub project contains three Playbooks,
- install_wireguard.yml
- ping_devices.yml
- reboot.yml
Here we create a task that installs wireguard in our eManager fleet.
-
In the
Task Templates
section, clickNEW TEMPLATE
. -
Choose the name you prefer, then must fullfill the different fields with the following information:
We let the other fields empty. - Press
CREATE
. - You can use same Inventory, Repository and Environment to create more tasks with the playbooks
ping_devices.yml
andreboot.yml
Run a task
Click the task you want to run from Task Templates
. Then click RUN
.