how to extract the IP from ansible getent wrapper module - ansible

I would like to use getent wrapper module in order to parse the contents of /etc/hosts and save the ip address in a variable for later use. I do not manage to isolate the IP if found on /etc/hosts
we are doing this in order to check that the /etc/hosts on the server farm are up to dated we will use this variable to verify against the dns and if the two address do not match then correct /etc/hosts. I also tried slurp but without much success
---
- hosts: all
tasks:
- name: getent hosts
getent:
database: hosts
key: "{{ansible_hostname}}"
register: results
- name: print result
debug:
msg="{{results}}"
- set_fact:
a_host_ip={{ getent_hosts[ansible_default_ipv4.address] }}
- debug: var=a_host_ip
I would like to have the ip address in the varilable a_host_ip but the actual contents are the hostnames corresponding to the ip address. How can I save the IP address in a variable.

To get a list of IPs:
{{ getent_hosts.keys() }}
To get a single element:
{{ getent_hosts.keys() | first }}

Related

Ansible getting public ip of host from inventory file

I would like to get public ip of a host defined in inventory file
[test-sites:children]
test-site-1
[test-site-1]
test-site-1-2 ipv4=192.168.0.1 ipv6=....
How do I get the ipv4 and ipv6 address of "test-site-1-2" defined in the inventory file? I have checked this answer but it gives all of the addresses (public & private). I am interested in ips defined in the inventory file only.
[test-site-1]
test-site-1-2 ipv4=192.168.0.1 ipv6=....
Q: "How do I get the ipv4 and ipv6 address of "test-site-1-2" defined in the inventory file?"
A: If the playbook is running at "test-site-1-2" simply reference the variables directly. For example
- hosts: test-site-1-2
tasks:
- debug:
var: ipv4
- debug:
var: ipv6
If these variables are needed by other hosts reference to "hostvars" is needed. For example
- hosts: test-site-1
tasks:
- debug:
var: hostvars['test-site-1-2'].ipv4
- debug:
var: hostvars['test-site-1-2'].ipv6
See Basic inventory.

how to set hostname using inventory file

I have my hosts in an inventory file as below:
cnamgw01b ansible_ssh_host=172.17.0.26
cnamgw01a ansible_ssh_host=172.17.1.26
cnamgw02b ansible_ssh_host=172.17.0.23
cnamgw02a ansible_ssh_host=172.17.1.23
cnamgw03a ansible_ssh_host=172.17.1.13
cnamgw03b ansible_ssh_host=172.17.0.13
These are new builds and I would like to set the hostname based on the inventory file. I already have a script in place that updated the inventory file as new VM's are turned up and assigns a random hostname. I would like to take this hostname assigned and set it as the hosts hostname. How can I accomplish this? Also note that I also use folders to subdivide the hosts by region
You can use the ansible module hostname to set hostname.
https://docs.ansible.com/ansible/latest/modules/hostname_module.html
- hosts: all
tasks:
- name: Set hostname
hostname:
name: {{ inventory_hostname }}
You could run something like this to set the system hostname to the inventory hostname:
- hosts: all
tasks:
- name: set system hostname
command: hostnamectl set-hostname {{ inventory_hostname }}
That is, the variable inventory_hostname holds the name of the current host as it was named in your inventory.
This task assumes you have the hostnamectl command available. You could instead write the value of inventory_hostname to /etc/hostname and call the hostname command separately.

How do I correctly use the ansible hostname module?

I am trying to use ansible to modify the hostnames of a dozen newly created Virtual Machines, and I am failing to understand how to loop correctly.
Here is the playbook I've written:
---
- hosts: k8s_kvm_vms
tasks:
- name: "update hostnames"
hostname:
name: "{{ item }}"
with_items:
- centos01
- centos02
...
The result is that it updates each host with each hostname. So if I have 12 machines, each hostname would be "centos12" at the end of the playbook.
I expect this behavior to essentially produce the same result as:
num=0
for ip in ${list_of_ips[*]}; do
ssh $ip hostnamectl set-hostname centos${num}
num=$((num+1))
done
If I were to write it myself in bash
The answer on this page leads me to believe I would have to include all of the IP addresses in my playbook. In my opinion, the advantage of scripting would be that I could have the same hostnames even if their IP changes (I just have to copy the ip addresses into /etc/ansible/hosts) which I could reuse with other playbooks. I have read the ansible page on the hostname module, but their example leads me to believe I would, instead of using a loop, have to define a task for each individual IP address. If this is the case, why use ansible over a bash script?
ansible hostname module
You can create a new variable for each of the servers in the inventory like
[k8s_kvm_vms]
server1 new_hostname=centos1
server2 new_hostname=centos2
Playbook:
---
- hosts: k8s_kvm_vms
tasks:
- name: "update hostnames"
hostname:
name: "{{ new_hostname }}"
I think you need to sudo to change the hostname thus you should add "become: yes"
---
- hosts: k8s_kvm_vms
become: yes
tasks:
- name: "update hostnames"
hostname:
name: "{{ new_hostname }}"

get a particular host name's ip with ansible

I have a host file that looks roughly like this:
[kibanamaster]
efk host_ip host user passwd
[elasticnode]
esnode host_ip user passwd
and I am trying something in the style of
- name: get ip address node1
debug: var=hostvars[inventory_host]['esnode']['ansible_default_ipv4']['address']
register: es_node1
But I get variable not defined. Anyone outthere able to help?
EDIT:
If I do
debug: var=hostvars[LOG1]['esnode']['ansible_default_ipv4']['address']
register: node_1
I get
{"hostvars[LOG1]['ansible_default_ipv4']['address']": "VARIABLE IS NOT DEFINED!"}
hostvars magic variable is a dictionary with keys named after hosts in your inventory.
So you may want to try:
hostvars['esnode']['ansible_default_ipv4']['address']
to get ip address of esnode host.
You can use pre-loaded ansible variable to get values for both ipv4, ipv6 & hostname
IPV4 --> {{ ansible_eth0.ipv4.address }}
IPV6 --> {{ ansible_eth0.ipv6.address }}
Hostname --> {{ ansible_hostname }

Ansible Playbook to push hostname to ip addresses

I have a CSV file that has the host name with corresponding IP address in it. I am trying to write an ansible playbook using the lineinfile = command with a variable that will read the CSV file and place the hostname of the corresponding ipaddress on the host with the ipaddress. I don't know if this is the way to go. I want to run it with the playbook addressed to all host.
If you need to connect to all hosts in your csv file and set hostname to corresponding name value from that file, this will do:
---
- hosts: localhost
tasks:
- add_host: name="{{ item.split(',')[1] | trim }}" ansible_host="{{ item.split(',')[0] }}" group=csv
with_lines: cat host-ip.csv
- hosts: csv
tasks:
- hostname: name="{{ inventory_hostname }}"

Resources