0

I've got an inventory file, which looks like:

[swarm_master]
ubuntu01

my main.yml file looks like:

---
- debug: msg="{{ item }}" 
  with_items:
    - "{{ groups['swarm_master'] }}"

- shell: echo {{ groups['swarm_master'] }}
  register: result

- debug: var=result.cmd

- set_fact:
    advertise_address: "{{ groups['swarm_master'] }}"

- shell: echo {{ advertise_address }}
  register: result2

- debug: var=result2.cmd

- command: "echo {{ groups['swarm_master'] }}"
  register: result3

- debug: var=result3.cmd

Ansible playbook gives the result:

PLAY [Apply docker role] ***********************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [ubuntu01]

TASK [swarm : debug] ***************************************************************************************************************************************************************************************
ok: [ubuntu01] => (item=ubuntu01) => {
    "msg": "ubuntu01"
}

TASK [swarm : shell] ***************************************************************************************************************************************************************************************
changed: [ubuntu01]

TASK [swarm : debug] ***************************************************************************************************************************************************************************************
ok: [ubuntu01] => {
    "result.cmd": "echo [u'ubuntu01']"
}

TASK [swarm : set_fact] ************************************************************************************************************************************************************************************
ok: [ubuntu01]

TASK [swarm : shell] ***************************************************************************************************************************************************************************************
changed: [ubuntu01]

TASK [swarm : debug] ***************************************************************************************************************************************************************************************
ok: [ubuntu01] => {
    "result2.cmd": "echo [u'ubuntu01']"
}

TASK [swarm : command] *************************************************************************************************************************************************************************************
changed: [ubuntu01]

TASK [swarm : debug] ***************************************************************************************************************************************************************************************
ok: [ubuntu01] => {
    "result3.cmd": [
        "echo", 
        "[uubuntu01]"
    ]
}

PLAY RECAP *************************************************************************************************************************************************************************************************
ubuntu01                   : ok=9    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

Why ansible adds brackets and 'u' letter to passed variable? It is shown in debug results. Iteration throuh groups with with_items keyword shows proper values.

bartbak
  • 15
  • 8

1 Answers1

0

This is happening because when you pass in that group, even with one item, its still a list, so you are getting that list formatting along with the value. If you know that there is only going to be 1 item in the group, you could pass it in like this:

- shell: echo {{ groups['swarm_master'][0] }}
  register: result

If don't know how many there are going to be, then you can use the with_items module like you did with your debug.

ebrewer
  • 474
  • 5
  • 11
  • I would have said that too, but then the OP would have ended with `[ubuntu]` and not `[u'ubuntu']`. But from what I see on [here](https://stackoverflow.com/questions/2464959/whats-the-u-prefix-in-a-python-string) it looks like it is a question of unicode string. No clue how the OP ends up with that unicode string, though. – β.εηοιτ.βε Oct 11 '19 at 20:27