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%
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:

#!/usr/bin/env python3
import re
import json
import os
# Get the current directory
current_directory = os.getcwd()
# Concatenate the current directory with the file name
server_file = os.path.join(current_directory, 'servers.txt')
def read_servers_file(server_file):
"""Reads the server file and extracts the IP addresses."""
ips = []
with open(server_file, 'r') as f:
lines = f.readlines()
for line in lines:
if 'ip_address=' in line:
match = re.search(r'ip_address=([\d\.]+)', line)
if match:
ips.append(match.group(1))
return ips
def generate_inventory(ips):
"""Generates the inventory in JSON format."""
inventory = {
'_meta': {
'hostvars': {}
},
'all': {
'hosts': ips
}
}
return inventory
def main():
"""Main function."""
ips = read_servers_file(server_file)
inventory = generate_inventory(ips)
print(json.dumps(inventory))
if __name__ == '__main__':
main()

Step 4

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

---
- name: Extract ctlplane IP addresses and run command on servers
hosts: all
gather_facts: yes
become: yes
remote_user: ec2-user #change this to the remote user
tasks:
- name: Run command on servers and save output locally
ansible.builtin.shell: "date"
register: command_output
run_once: yes
- name: Debug command output
ansible.builtin.debug:
msg: "{{ command_output.stdout }}"
- name: Create your local file on master node
ansible.builtin.file:
path: "report.txt"
state: touch
mode: '0644'
delegate_to: localhost
become: no
- name: Create report.txt file or append to existing file
ansible.builtin.lineinfile:
path: "report.txt"
line: "{{ item }} - {{ command_output.stdout }}"
loop: "{{ ansible_play_batch }}"
delegate_to: localhost
become: no

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:

ansible-playbook -i dynamic_inventory.py ansible-playbook.yml

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.