How to get host alias name in a variable? - ansible

In our ansible scripts we are having a variable
set_fact: Obj_prefix=smoke-{{ ansible_host }}
this fact obj_prefix long as our ansible host name is like machinename.labname.domainname .
In our inventory file , i have captured the host name as
[swarmcluster]
smaster ansible_host=swarmmaster.vlab.mycompany.com
Instead of ansible_host if i can take alias name ,then the obj_prefix variable would be short. how to get the alias name variable? Is there any variable like ansible_hostalias ?

The variable you are looking for is inventory_hostname_short: see the documentation.

Related

Ansible jinja2 template get current IP file being constructed

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.

Ansible fetch host IP from another group based on defined variable

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.

Ansible: Read variables from properties file

I am trying to translate some shell scripts to ansible playbooks.
In the shell scripts there are a lot of variables that are read from a separate properties file in which these variables are defined.
Is there some module or some other way that I can do this. For example,
if in the properties file I have
$WORKDIR=/opt/app defined and in the ansible playbook I want to call some module for example,
copy:
src: /tmp/app
dest: $WORKDIR/
Is there a way to do this?
Thanks
Use lookup (doc)
Example:
{{ lookup('ini', 'user.name type=properties file=user.properties') }}
user.name, property key you want to read
user.properties, property file you want to read from
Yes, in ansible the inventory is the file where we define the hostname or ips along with variables.
Whenever we execute ansible playbook we pass a file using --inventory inventory ----> this is the default way of passing an inventory file
The playbook will source this file and all the variables defined inside it can be used.
In ansible the variable substitution happens using {{ WORKDIR }}
There are other ways of passing variable to playbook.
Such as
-- include_vars
-- using roles
-- defining vars inside playbook.
-- extra vars

how to have a variable inside a variable in ansible?

I am trying to push a property file template like the below
schema.registry.url=http://{{ schemaregis_host{{
play_hosts.index(inventory_hostname) }} }}:8081
I pass extra variables like these in the ansible input command
schemaregis0=host1
schemaregis1=host2
[inventory-hosts]
server1
server2
I want server1 property file to have the schemaregis0=host1 and server2 to have the other.
Ansible doesn't like the variable within a variable. How can i get around this?
You can access the variables with dynamic name by accessing the vars dict:
schema.registry.url=http://{{ vars['schemaregis' + play_hosts.index(inventory_hostname)|string] }}
But if you want to set different values for each host, the best way is to use host_vars configuration folder

Defining host as variable in Ansible hosts file

I have a rather simple hosts file
[clients]
qas0062
[dbs_server]
qas0063
For the users of the project we don't want them to modify hosts file but rather we have a separate user.config.yml file that contain various user-configurable parameters. There we have entry such as
dbs_server: qas0065
So the question is: is it possible to use a variable in the hosts file that would use a value defined in user.config.yml? And what would be the format?
Pretty sure you can't templatize the actual host key entry in the inventory, but you can templatize the value of its ansible_host connection var to achieve roughly the same effect, eg:
[clients]
clienthost ansible_host="{{ clienthost_var }}"
[dbs_server]
dbsserver ansible_host="{{ dbsserver_var }}"
then set the value of those vars from external vars before the play starts executing (eg, with the vars_files directive or -e).
There is another way to do the same thing. We can simply refer to values in the hosts (inventory) file by using the following syntax in our playbook
host={{ groups['dbs_server'][0] }}
This works well when you have one entry in the group (db_server in this specific case)

Resources