TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Infrastructure as Code / Operations / Python

Automate Routine Tasks With an Ad-Hoc Ansible Script

Dynamically generate inventory files to simplify everyday tasks using Ansible and Python.
Jul 15th, 2024 11:31am by
Featued image for: Automate Routine Tasks With an Ad-Hoc Ansible Script
Featured image by Unsplash+ in collaboration with Ave Calvar.

No one wants to spend an entire day running a manual script on a thousand servers. Fortunately, many processes like this can be automated using Ansible.

For example, imagine you are given a task to run a command to detect the Timezone on 1,000 servers. A quality engineer detected that some transactions are dated in the future, and the engineers suspected that some servers might have incorrect time zones. You are given a file that contains the IP addresses of 1,000 servers.

I will walk through completing this task by running an ad-hoc script to dynamically generate an inventory file using Ansible. All of this code is available on GitHub, and this tutorial assumes you already have an understanding of Python, Ansible and Bash.

Step 1

Establish a passwordless SSH connection with the target host. This will enable Ansible to securely log into the target host without having to input the password.

Step 2

Add the IP address to the file servers.txt and ensure the IP address is valid and follows the format in the servers.txt file.

Step 3

Extract the server’s IP address using Python to dynamically generate the inventory file:

Step 4

Save the following Ansible playbook as ansible-playbook.yml:

Step 5

Run the Ansible playbook, which will run a command date on the target servers and display the output in a file called report.txt , with:

Step 6

Your output should look similar to this screenshot.

Output of running Ansible playbook to detect time zone anomalies

Conclusion

Ansible simplifies complex tasks, such as instance infrastructure provisioning, configuration management and software deployment across a large-scale environment. You can find the full code for this tutorial on GitHub.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.