Skip to content

Instantly share code, notes, and snippets.

@magzim21
Created July 16, 2020 09:36
Show Gist options
  • Select an option

  • Save magzim21/0b71aad39a0c6999e56c44f49cb609d5 to your computer and use it in GitHub Desktop.

Select an option

Save magzim21/0b71aad39a0c6999e56c44f49cb609d5 to your computer and use it in GitHub Desktop.
{
"10.100.176.20" : "org-hostname-1.local",
"10.100.176.21" : "org-hostname-2.local",
"10.100.176.22" : "org-hostname-3.local",
"10.100.176.23" : "org-hostname-4.local",
"10.100.176.30" : "org-hostname-5.local"
}
---
- name: Playbook name
hosts: "{{ hosts_group }}"
become: yes
tasks:
- name: add current hostname to /etc/hosts
shell: grep 'PREVIOUS HOSTNAME' /etc/hosts || printf "\n\n### PREVIOUS HOSTNAME\n127.0.0.1 $(hostname)\n::1 $(hostname)\n" >> /etc/hosts
- name: copy hostnames.json
copy:
src: ./hostnames.json
dest: /tmp/hostnames.json
- name: Running rename_hosts script
script: rename_hosts.py /tmp/hostnames.json
# ZABBIX SECTION
- name: Save new hostname as variable
shell: hostname
register: new_hostname
- name: Rename host in zabbix_agent.conf
lineinfile:
path: /etc/zabbix/zabbix_agentd.conf
regexp: '^Hostname='
line: Hostname={{ new_hostname.stdout }}
- name: Restart zabbix-agent
systemd:
state: restarted
enabled: yes
name: zabbix-agent
#!/usr/bin/env python2.7
import subprocess
import json
import sys, os
# TODO ADD EXCEPTIONS
try:
if not os.path.isfile(sys.argv[1]):
print "ERROR: File provided as argument does not exist"
exit(1)
except IndexError:
print "ERROR: json with hosts to rename was not provided.\n" \
"Pass it as the first argument"
exit(1)
# GETTING CURRENT HOSTNAME
get_hostname_cmd = "hostname"
process = subprocess.Popen(
get_hostname_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
old_hostname, error = process.communicate()
if error:
print "ERROR: Can not execute '" + get_hostname_cmd + "' command. Details:"
print error
exit(1)
print "old_hostname is " + old_hostname
# GETTING CURRENT IP
get_ip_cmd = "hostname -I"
process = subprocess.Popen(get_ip_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
host_ip, error = process.communicate()
if error:
print "ERROR: Can not execute '" + get_ip_cmd + "' command. Details:"
print error
exit(1)
host_ip=host_ip.split()
print "host_ip is " + host_ip
# PARSING DATA FROM JSON
try:
with open(sys.argv[1], 'r') as outfile:
# json.dump(hostnames_list, outfile)
hostnames_list = json.load(outfile)
except ValueError:
print "ERROR: Provided json file is not valid"
exit(1)
found = False
for ip, new_hostname in hostnames_list.items():
if ip in host_ip:
rename_host_cmd = "hostnamectl set-hostname " + new_hostname
process = subprocess.Popen(
rename_host_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
output, error = process.communicate()
if error:
print "ERROR: Can not execute '" + rename_host_cmd + "' command. Details:"
print error
exit(1)
print "HOST RENAMED SUCCESSFULLY"
found = True
break
if not found:
print "Hostname of current host was not found in a list.\n" \
"So it was not renamed"
@magzim21
Copy link
Author

ANSIBLE batch rename hostnames

  1. Put desired information into hostnames.json
  2. Comment out whole 'ZABBIX SECTION' in pl-rename.yml if zabbix is not installed.
  3. Run
    ansible-playbook pl-rename.yml --extra-vars="hosts_group=<name of hostgroup from your 'hosts' file>"
  4. Enjoy 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment