Quantcast
Channel: Adetokunbo Ige, Author at The New Stack
Viewing all articles
Browse latest Browse all 6

Automate Routine Tasks With an Ad-Hoc Ansible Script

$
0
0
Featured image from Automate Routine Tasks with an Ad-hoc Ansible Script shows clocks in various time zones

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.

The post Automate Routine Tasks With an Ad-Hoc Ansible Script appeared first on The New Stack.

Dynamically generate inventory files to simplify everyday tasks using Ansible and Python.

Viewing all articles
Browse latest Browse all 6

Trending Articles