'Using the first host in group

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


Solution 1:[1]

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

kibana_elasticsearch_url: http://{{ groups['elasticproxy'][0] }}:9200

Solution 2:[2]

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

Solution 3:[3]

groups['elasticproxy'] | first

or

groups['elasticproxy'][0]

will both do the trick.

Solution 4:[4]

Found the answer cause someone on reddit helped me, i had te make some functions and had to recheck the position off the joystick:

import time
import analogio
import board
import digitalio
import usb_hid
from adafruit_hid.mouse import Mouse
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

kbd = Keyboard(usb_hid.devices)
mouse = Mouse(usb_hid.devices)
x_axis = analogio.AnalogIn(board.A3)
y_axis = analogio.AnalogIn(board.A2)
select = digitalio.DigitalInOut(board.A1)
select.direction = digitalio.Direction.INPUT
select.pull = digitalio.Pull.UP

pot_min = 0.00
pot_max = 3.29
step = (pot_max - pot_min) / 20.0

def get_voltage(pin):
    return (pin.value * 5) / 65536

def steps(axis):
    """ Maps the potentiometer voltage range to 0-20 """
    return round((axis - pot_min) / step)

def is_joystick_active( x, y ):
    """Checks whether the joystick is away from the center position"""
    return steps(x) > 21.0 or steps(x) < 9.0 or steps(x) > 25.0 or steps(x) < 1.0 or steps(y) > 21.0 or steps(y) < 9.0 or steps(y) > 25.0 or steps(y) < 1.0

def get_mouse_move( x, y ):
    """Translates x,y voltages into how far to move the mouse on each step"""
    mouse_x = 0;
    mouse_y = 0;
    if steps(x) > 21.0:
        # print(steps(x))
        mouse_x+=1

    if steps(x) < 9.0:
        # print(steps(x))
        mouse_x-=1

    if steps(x) > 25.0:
        # print(steps(x))
        mouse_x+=8

    if steps(x) < 1.0:
        # print(steps(x))
        mouse_x-=8

    if steps(y) > 21.0:
        # print(steps(y))
        mouse_y+=1

    if steps(y) < 9.0:
        # print(steps(y))
        mouse_y-=1

    if steps(y) > 25.0:
        # print(steps(y))
        mouse_y+=8

    if steps(y) < 1.0:
        # print(steps(y))
        mouse_y-=8
    return mouse_x, mouse_y

while True:
    #print(str(digitalio.DigitalInOut.value))
    x = get_voltage(x_axis)
    y = get_voltage(y_axis)

    print("x=" + str(steps(x)))
    print("y=" + str(steps(y)))

    time.sleep(0.5)

    if is_joystick_active( x, y ):
        kbd.press(Keycode.SHIFT)
        mouse.press(Mouse.MIDDLE_BUTTON)

        while is_joystick_active( x, y ):
            mouse_x, mouse_y = get_mouse_move( x, y )
            mouse.move(x=mouse_x, y=mouse_y)
            # Wait for a bit
            time.sleep(0.1)
            # Check the joystick position again
            x = get_voltage(x_axis)
            y = get_voltage(y_axis)
        else:
            kbd.release(Keycode.SHIFT)
            mouse.release(Mouse.MIDDLE_BUTTON)```

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Morgan Christiansson
Solution 2 ttavi
Solution 3 stackprotector
Solution 4 FaultyDaantje