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.
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.