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
Related
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.
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
I'm using Ansible 2.1.1.0. In Ansible, is there a way to define a variable in group_vars/all file with a dynamic variable set as an extra var in ansible-playbook command?
I have my play.sh file as follows.
ansible-playbook site.yml -i hosts -e "home_dir=$1"
Then I want to use it as a global variable in group_vars/all file as follows.
my_dir: {{ home_dir }}/sub_dir/my_dir
I know the syntax above is wrong for the all file, and this can be done by using the variable directly in the task yaml file. Is there any way I can use a dynamic 'ansible-playbook extra variable' for defining a 'global variable'?
I don't know about global_vars existence in Ansible...
If you want to make my_dir available to all hosts, you can define group variable for a special group all like:
file ./group_vars/all:
my_dir: "{{ home_dir }}/sub_dir/my_dir"
This way my_dir will be constructed based on home_dir extra variable and be available as group variable for all hosts.
I have the following version installed: ansible 2.3.0 (devel 2131eaba0c)
I want to specify my host variable as external variable and then use it in the playbook similar to this:
hosts: "{{integration}}"
In my group_vars/all file I have the following defined variable:
integration: "int60"
The host file looks like this:
[int60]
hostA
[int61]
hostB
Unfortunately this does not work. I also tried to define the host var in the following way:
[integration]
127.0.0.1 ansible_host="{{ integration_env }}"
and have the integration_env specified in my group_vars/all file. In this case it seemed like it ran the tasks locally and not in the desired environment.
Is it possible to do something like this? I'd be open to whole new ways of doing this. The main goal is simply to define the host variable in a var file.
This will work if you pass integration variable as extra variable:
ansible-playbook -e integration=int60 myplaybook.yml
Any variables used in play "header", should be defined before Ansible parses playbook.
In your example you define integration as host facts. Facts are only defined on task level, not play level.
Update: and you can use other ways of passing variables, not only extra vars.
For example:
- hosts: "{{ lookup('env','DYN_HOSTS') }}"
will also work.
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)