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 }
Related
In my ansible host inventory, i got 3 host ip as below
[host-group-1]
123.23.23.21
123.23.23.22
i have a variable as below
host_list:
- { fileName: "master.xml", IP: {{ get_current_Execution_ip }} }
- { fileName: "slave.xml", IP: {{ get_current_Execution_ip }} }
I have a task which will do some logic in the jinja2 template based on value get from IP. My question is how can i use this {{ get_current_Execution_ip }} to get the current IP the file being constructed via jinja2 template based on respective server. For instance, if the file contructing in 123.23.23.21, the IP value should be 123.23.23.21, if contructing in server 123.23.23.22, the IP value should be 123.23.23.22
With your inventory set up like that, you can just access the variable inventory_hostname. If you change your inventory to look something like
[host_group_1]
host1 ansible_host=123.23.23.21
host2 ansible_host=123.23.23.22
then you can instead access the IP with the ansible_host variable (which is also available in the first case).
If you change your inventory to not list IP addresses at all (instead relying on resolving the name through DNS), then you probably want to look at some of the answers to this question.
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.
I would like to fetch IP of a host from another group in inventory file based on variable name.
Example inventory file:
[master]
master-0.example.io
master-1.example.io
master-2.example.io
[replica]
replica-0.example.io master_host=master-0.example.io
replica-1.example.io master_host=master-1.example.io
replica-2.example.io master_host=master-2.example.io
Later in playbook I want to run a command, but for that one I require IP of master (FQDN does not work). I have tried following, but this is not working:
- name: Test run
shell: "echo {{ ansible_default_ipv4.address }}:{{ port }} {{ hostvars['master_host']['ansible_default_ipv4', 'address'] }}:{{ port }}"
Any idea if there is a way to get this covered? Or if there is any other way to do this - basically what I need is to match master-0 with replica-0 etc. and I do not want to put IPs into inventory file.
hostvars is a hash. Each top entry in the hash is the name of your host in the inventory. To address that hash, your are using the string 'master_host' which does not exist. What you want, is to use the value of the variable master_host for the particular host your are executing the command on.
The 2 possible correct syntaxes are:
{{ hostvars[master_host].ansbible_default_ipv4.address }}
{{ hostvars[master_host]['ansbible_default_ipv4']['address'] }}
... or any mix between the dot or bracket syntax.
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 }}
I have the following problem:
I'm writing playbook for setting IP address on the command line in Ansible. Lets say 10.10.10.x. I need to get the last part of my public IP lets say x.x.x.15 and assign it to the private: 10.10.10.15. Is there a variable for this? Can i capture some? I've tried to use something like:
shell: "ip addr show | grep inet ...."
register: host_ip
But it is not what i need. It works, but only for a limited number of servers.
The whole thing should be like that:
"shell: /dir/script --options 10.10.10.{{ var }}"
and {{ var }} should be the host part of the public IP.
Edit:
Thank you! Here's my final solution:
- name: Get the host part of the IP
shell: host {{ ansible_fqdn }} | awk '{print $4}'
register: host_ip
And
{{ host_ip.stdout.split('.')[3] }}
For using it later in the playbook.
As mentioned by jarv this can be obtained by using facts.
This can be done in the following ways:
For a list of all ipv4 addresses:
{{ ansible_all_ipv4_addresses }}
For the default ipv4 address:
{{ ansible_default_ipv4.address }}
If you know the ip address is on the eth0 interface:
{{ ansible_eth0.ipv4.address }}
You can then append the .split('.')[3] method to the variable to get the appropriate output, for example {{ ansible_default_ipv4.address.split('.')[3] }}
Instead of using a system utility you can use ansible facts though you will find that interface names will vary from server to server.
You specifically mentioned the last part of my public IP
If you really mean your public IP you will need to use an external service to get it since your server may behind a NAT. Here is one option
shell: wget -qO- http://ipecho.net/plain ; echo
register: host_ip
That will give your public IP, next to get the last octet you could do something like:
{{ host_ip.stdout.split('.')[3] }}
This is an similar way to get it:
- name: Get the local IP
local_action:
module: uri
url: http://checkip.amazonaws.com/
return_content: yes
register: ip_lookup
- set_fact:
local_ip: "{{ ip_lookup.content | regex_replace('\n','') }}"
- debug: var=local_ip