This solution is explained in detail in the Codeyourinfra project blog post How to unarchive different files in different servers in just one shot. Check it out!
You have a bunch of servers and quite often you have to manually extract in each one a specific compressed file. It works well when there's not too many servers to change, but such task becomes really boring and even error-prone when it reaches, say, a hundred servers or more. How can it be done in scale?
unarchive_from_url_param is an example of Ansible role which makes it possible. You just need to pass the parameters appropriately. For each server, the role expects a url from where the compressed file is downloaded and a target directory to where the content is extracted. All the hosts that are defined inside the servers group in the Ansible inventory are considered on a single execution.
---
- hosts: servers
roles:
- role: unarchive_from_url_param
vars:
params:
server1:
url: https://archive.apache.org/dist/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bin.tar.gz
target: /opt/maven
server2:
url: https://archive.apache.org/dist/ant/binaries/apache-ant-1.10.1-bin.zip
target: /opt/ant
First of all, turn on the VMs, executing the command $ vagrant up
. Once the test environment is up, execute the command $ ansible-playbook playbook.yml
. Finally, in order to check if the compressed files were properly extracted in both servers, execute the command $ ansible servers -m shell -a "ls /opt"
.
You can also test the solution automaticaly, by executing ./test.sh
or using Molecule. If you prefer the latter, use venv to setup the test environment:
python3 -m venv env && source env/bin/activate && pip install -r requirements.txt
After that, you can choose to perform the test through 3 different ways:
molecule test
As well as the shell script test.sh, it requires:
molecule test -s aws
If your choice is performing the test in AWS, bear in mind you must have your credentials appropriately in ~/.aws/credentials. You can configure it through the AWS CLI tool.
The test is performed in the AWS region Europe - London (eu-west-2). Just run the test and check the running instances through your AWS Console.
molecule test -s docker
That's the way through the role is tested in Travis CI.
It requires Docker.