My ansible host definition looks like
[elasticclient]
192.168.10.2
192.168.10.3
I want to use the first host in the group to be used in a variable. My playbook is
- hosts: kibana
roles:
- kibana
vars:
kibana_elasticsearch_url: http://{{ groups[['elasticclient'][0]] }}:9200
When I run this, my file contains
http://[u'192.168.10.2']:9200
How do I change it to
http://192.168.10.2:9200
The group_vars in Ansible are a convenient way to apply variables to multiple hosts at once. Group_vars is an Ansible-specific folder as part of the repository structure. This folder contains YAML files created to have data models, and these data models apply to all the devices listed in the hosts. ini file.
Using the --limit parameter of the ansible-playbook command is the easiest option to limit the execution of the code to only one host. The advantage is that you don't need to edit the Ansible Playbook code before executing to only one host.
Devices or hosts are referenced by their DNS name or with the ansible inventory ip address variable. Hosts can be defined either by IP address or DNS name or if DNS is not resolvable using the ansible_host command. Multiple servers or network devices can be referenced in a playbook by referencing the group name.
ansible_host. The name of the host to connect to, if different from the alias you wish to give to it. ansible_port. The connection port number, if not the default (22 for ssh) ansible_user.
I figured it out, it was a list and I had to index it again.
kibana_elasticsearch_url: http://{{ groups['elasticproxy'][0] }}:9200
Or you could use what ansible provides as default:
hostvars[groups['elasticsearch'][0]]['ansible_eth0']['ipv4']['address']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With