Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Last active May 16, 2020 23:58
Show Gist options
  • Save luckylittle/3f5af303867a4e2070be9a7548ee6208 to your computer and use it in GitHub Desktop.
Save luckylittle/3f5af303867a4e2070be9a7548ee6208 to your computer and use it in GitHub Desktop.
Red Hat Ansible - Assign a FQDN of Windows machine based on the existence of hostname.properties
---
# Tested on Ansible 2.9.4
# Assign a FQDN of Windows machine based on the existence of hostname.properties
# Copyright (c) 2020 Lucian Maly, Red Hat
- name: 1.0 | FAILOVER_PORTAL | Find out if the 'hostname.properties' file exists.
win_stat:
path: 'C:\Program Files\PRODUCT\FOLDER1\FOLDER2\etc\hostname.properties'
register: hostname_stat
tags:
- failover
- portal
- block:
- name: 1.1 | FAILOVER_PORTAL | Fetch the 'hostname.properties' file from the remote host.
fetch:
src: 'C:\Program Files\PRODUCT\FOLDER1\FOLDER2\etc\hostname.properties'
dest: '/tmp/'
flat: yes
register: fetch_hostname_file
- name: 1.2 | FAILOVER_PORTAL | Read the first uncommented hostname line.
shell: "cat /tmp/hostname.properties | grep -m 1 -e '^hostname='"
register: file_hostname
delegate_to: 127.0.0.1
- name: 1.3 | FAILOVER_PORTAL | Assign the value from the 'hostname.properties' file.
set_fact:
hostname: "{{ file_hostname.stdout.split('=')[1] | trim }}"
when: hostname_stat.stat.exists
tags:
- failover
- portal
- block:
- name: 1.1 | FAILOVER_PORTAL | Use 'GetHostEntry' to determine the hostname.
win_command: powershell.exe -
args:
stdin: "[System.Net.DNS]::GetHostEntry('{{ ansible_ssh_host }}').HostName"
register: hostname_gethostentry
- name: 1.2 | FAILOVER_PORTAL | Assign the value from the 'GetHostEntry' value.
set_fact:
hostname: "{{ hostname_gethostentry.stdout | trim }}"
when: hostname is undefined
tags:
- failover
- portal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment