Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the first host in group

Tags:

ansible

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
like image 755
user1191140 Avatar asked Feb 10 '17 23:02

user1191140


People also ask

What is the use of group host Vars?

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.

How do I control ansible playbook only on specific hosts?

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.

How do you mention hosts in ansible?

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.

What is Ansible_host?

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.


2 Answers

I figured it out, it was a list and I had to index it again.

kibana_elasticsearch_url: http://{{ groups['elasticproxy'][0] }}:9200
like image 176
user1191140 Avatar answered Oct 11 '22 14:10

user1191140


Or you could use what ansible provides as default: hostvars[groups['elasticsearch'][0]]['ansible_eth0']['ipv4']['address']

like image 30
ttavi Avatar answered Oct 11 '22 14:10

ttavi