Discussion:
[ansible-project] delegate_to does not work for array of hosts passed in to role
Barry Kaplan
2015-06-02 03:37:38 UTC
Permalink
I posted this as a bug (https://github.com/ansible/ansible/issues/11102).
But it was closed saying it was a discussion. Not sure why, seems like a
clear bug to me. But I'm open to be dissuaded.

----

Ansible 1.9.1 running from OSX.
in playbook

- role: mongodb
mongodb_replica_set_name: "{{mongodb_rs_name}}"
mongodb_replica_set_hosts: "{{groups.mongod_rs_member}}"

mongodb role

- set_fact:
mongodb_candidate_master: "{{mongodb_replica_set_hosts[0]}}"
run_once: true

- shell: echo "HELLO groups.mongod_rs_member +++++++++++++++++"
delegate_to: "{{groups.mongod_rs_member[0]}}"
run_once: true

- shell: echo "HELLO mongodb_candidate_master +++++++++++++++++"
delegate_to: "{{mongodb_candidate_master}}"
run_once: true

- shell: echo "HELLO mongodb_replica_set_hosts +++++++++++++++++"
delegate_to: "{{mongodb_replica_set_hosts[0]}}"
run_once: true

output

TASK: [mongodb | shell echo "HELLO groups.mongod_rs_member +++++++++++++++++"] ***
<10.0.139.56> ESTABLISH CONNECTION FOR USER: ubuntu
<10.0.139.56> REMOTE_MODULE command echo "HELLO groups.mongod_rs_member +++++++++++++++++" #USE_SHELL
<10.0.139.56> EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=30m -o ControlPath="/Users/bkaplan/.ansible/cp/ansible-ssh-%h-%p-%r" -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 10.0.139.56 /bin/sh -c 'sudo -k && sudo -H -S -p "[sudo via ansible, key=enrekuxbmqjtjdgsxvlukuvqlhnudrkw] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-enrekuxbmqjtjdgsxvlukuvqlhnudrkw; LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python'"'"''
changed: [10.0.139.56 -> 10.0.139.56] => {"changed": true, "cmd": "echo \"HELLO groups.mongod_rs_member +++++++++++++++++\"", "delta": "0:00:00.005216", "end": "2015-05-31 04:13:32.378158", "rc": 0, "start": "2015-05-31 04:13:32.372942", "stderr": "", "stdout": "HELLO groups.mongod_rs_member +++++++++++++++++", "warnings": []}

TASK: [mongodb | shell echo "HELLO mongodb_candidate_master +++++++++++++++++"] ***
<10.0.139.56> ESTABLISH CONNECTION FOR USER: ubuntu
<10.0.139.56> REMOTE_MODULE command echo "HELLO mongodb_candidate_master +++++++++++++++++" #USE_SHELL
<10.0.139.56> EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=30m -o ControlPath="/Users/bkaplan/.ansible/cp/ansible-ssh-%h-%p-%r" -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 10.0.139.56 /bin/sh -c 'sudo -k && sudo -H -S -p "[sudo via ansible, key=xnkqbskjckeetqglyftpndfusnsyhoyq] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-xnkqbskjckeetqglyftpndfusnsyhoyq; LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python'"'"''
changed: [10.0.139.56 -> 10.0.139.56] => {"changed": true, "cmd": "echo \"HELLO mongodb_candidate_master +++++++++++++++++\"", "delta": "0:00:00.005059", "end": "2015-05-31 04:13:33.277391", "rc": 0, "start": "2015-05-31 04:13:33.272332", "stderr": "", "stdout": "HELLO mongodb_candidate_master +++++++++++++++++", "warnings": []}

TASK: [mongodb | shell echo "HELLO mongodb_replica_set_hosts +++++++++++++++++"] ***
<{> ESTABLISH CONNECTION FOR USER: ubuntu
<{> REMOTE_MODULE command echo "HELLO mongodb_replica_set_hosts +++++++++++++++++" #USE_SHELL
<{> EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=30m -o ControlPath="/Users/bkaplan/.ansible/cp/ansible-ssh-%h-%p-%r" -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 { /bin/sh -c 'sudo -k && sudo -H -S -p "[sudo via ansible, key=mqgojxehsqcpahfvfcdimgoedvygtvvb] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-mqgojxehsqcpahfvfcdimgoedvygtvvb; LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python'"'"''
fatal: [10.0.139.56 -> {] => SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9287a30a-c20b-4896-a76d-bcdd21a27319%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-06-02 13:26:32 UTC
Permalink
From the output of the 3rd task it seems that
mongodb_replica_set_hosts[0] is being interpreted as '{' which, of
course, does not resolve. This does not seem to be an array issue but
of too many nested templating attempts.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8kS4C1Zvgpo3_z__43n5D77CNqZNZdBaLZK%3DZbQEa%3D0qg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Barry Kaplan
2015-06-02 16:16:31 UTC
Permalink
What do you think would be causing the nested templating? Something deep in
variable resolution?

The third echo is the direct version of first setting the value to a fact,
which works.
Post by Brian Coca
From the output of the 3rd task it seems that
mongodb_replica_set_hosts[0] is being interpreted as '{' which, of
course, does not resolve. This does not seem to be an array issue but
of too many nested templating attempts.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a747482f-5ed6-4ac2-8dc3-752a0f9adf9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-06-02 16:34:01 UTC
Permalink
not sure what 'works' means in this context
Post by Barry Kaplan
What do you think would be causing the nested templating? Something deep in
variable resolution?
The third echo is the direct version of first setting the value to a fact,
which works.
Post by Brian Coca
From the output of the 3rd task it seems that
mongodb_replica_set_hosts[0] is being interpreted as '{' which, of
course, does not resolve. This does not seem to be an array issue but
of too many nested templating attempts.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/a747482f-5ed6-4ac2-8dc3-752a0f9adf9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8khYus7LXJ%3DrYPbR%2BDgug38hF%2B%3Dr-i4q33Vb3LWW1351g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Barry Kaplan
2015-06-02 16:43:08 UTC
Permalink
Post by Brian Coca
not sure what 'works' means in this context
"works" is that the third task delegates to the same host as the second
task.

Don't you expect the exact same output for the last two tasks? Are you
seeing something that I am missing?
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/79164104-61fa-466c-8ced-fae92d8ba19b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-06-02 16:54:47 UTC
Permalink
the task fails with e 'fatal', what looks the same is the verbose
output of what will be executed
Post by Barry Kaplan
Post by Brian Coca
not sure what 'works' means in this context
"works" is that the third task delegates to the same host as the second
task.
Don't you expect the exact same output for the last two tasks? Are you
seeing something that I am missing?
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/79164104-61fa-466c-8ced-fae92d8ba19b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8mMJ8ZSajUN074rabTvm-Zk1_8HOy8uUV7OX78hgPNSbQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Barry Kaplan
2015-06-03 01:49:44 UTC
Permalink
2) ssh ... -o ConnectTimeout=10 10.0.139.56 /bin/sh -c ...
3) ssh ... -o ConnectTimeout=10 { /bin/sh -c ...

So you don't think that the ssh ip in the 3 task should not be the same as
2?

I'm sorry Brian, but I am not all following what the miscommunication is
here.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/39fb66d6-bcb3-4f05-be8e-0f3c1267f589%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-06-03 03:24:02 UTC
Permalink
the ssh ip in task 3 is '{' which indicates that the [0] is applied
to the 'template string' and not to the resulting array you expect.
This makes the 3rd task fail as it cannot ssh to '{'.
Post by Barry Kaplan
2) ssh ... -o ConnectTimeout=10 10.0.139.56 /bin/sh -c ...
3) ssh ... -o ConnectTimeout=10 { /bin/sh -c ...
So you don't think that the ssh ip in the 3 task should not be the same as
2?
I'm sorry Brian, but I am not all following what the miscommunication is
here.
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/39fb66d6-bcb3-4f05-be8e-0f3c1267f589%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8nDEaDuTPNcmfsqrB7sspoENR7bfWsiC-5C3JePHDDdew%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Barry Kaplan
2015-06-03 03:55:58 UTC
Permalink
Post by Brian Coca
the ssh ip in task 3 is '{' which indicates that the [0] is applied
to the 'template string' and not to the resulting array you expect.
This makes the 3rd task fail as it cannot ssh to '{'.
Exactly. Is that a mistake I am making? Or a bug? I filed the bug because I
don't see a mistake on my part.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8c07227d-d1e3-46c8-80e4-a5e78dc01e15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-06-03 04:01:17 UTC
Permalink
can you show that variable in a debug during execution? it does not
look like it is being set properly.
Post by Barry Kaplan
Post by Brian Coca
the ssh ip in task 3 is '{' which indicates that the [0] is applied
to the 'template string' and not to the resulting array you expect.
This makes the 3rd task fail as it cannot ssh to '{'.
Exactly. Is that a mistake I am making? Or a bug? I filed the bug because I
don't see a mistake on my part.
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/8c07227d-d1e3-46c8-80e4-a5e78dc01e15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8kWbexMwWT%2BTwR%2B44AfRLW5mYqt1coE7o6SyOsWavbJXw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Barry Kaplan
2015-06-04 12:02:04 UTC
Permalink
Here's the new test:

- set_fact:
mongodb_candidate_master: "{{mongodb_replica_set_hosts[0]}}"
run_once: true


- name: var groups.mongod_rs_member[0]
debug: var=groups.mongod_rs_member[0]
run_once: true


- shell: echo "HELLO groups.mongod_rs_member +++++++++++++++++"
delegate_to: "{{groups.mongod_rs_member[0]}}"
run_once: true


- name: var mongodb_candidate_master
debug: var=mongodb_candidate_master
run_once: true


- shell: echo "HELLO mongodb_candidate_master +++++++++++++++++"
delegate_to: "{{mongodb_candidate_master}}"
run_once: true


- name: var mongodb_replica_set_hosts[0]
debug: var=mongodb_replica_set_hosts[0]
run_once: true


- name: msg mongodb_replica_set_hosts[0]
debug: msg="{{mongodb_replica_set_hosts[0]}}"
run_once: true


- debug: msg="{{mongodb_replica_set_hosts[0]}}"
run_once: true


- shell: echo "HELLO mongodb_replica_set_hosts +++++++++++++++++"
delegate_to: "{{mongodb_replica_set_hosts[0]}}"
run_once: true


And the output:

ansible❯ ansible-playbook -i inventory/aws/staging playbooks/configure/
mongod.yml --skip-tags base
ops/git/master !+


PLAY [Base ansible configuration (packages)]
**********************************


GATHERING FACTS
***************************************************************
ok: [10.0.137.189]
ok: [10.0.139.51]
ok: [10.0.136.46]


PLAY [Base ansible configuration (configure)]
*********************************


GATHERING FACTS
***************************************************************
ok: [10.0.137.189]
ok: [10.0.139.51]
ok: [10.0.136.46]


PLAY [Base ansible configuration (users)]
*************************************


GATHERING FACTS
***************************************************************
ok: [10.0.137.189]
ok: [10.0.139.51]
ok: [10.0.136.46]


PLAY [Configure mongodb replicaset cluster]
***********************************


GATHERING FACTS
***************************************************************
ok: [10.0.136.46]
ok: [10.0.137.189]
ok: [10.0.139.51]


TASK: [debug var=groups.mongod_rs_member]
*************************************
ok: [10.0.136.46] => {
"var": {
"groups.mongod_rs_member": [
"10.0.136.46",
"10.0.139.51",
"10.0.137.189"
]
}
}
ok: [10.0.139.51] => {
"var": {
"groups.mongod_rs_member": [
"10.0.136.46",
"10.0.139.51",
"10.0.137.189"
]
}
}
ok: [10.0.137.189] => {
"var": {
"groups.mongod_rs_member": [
"10.0.136.46",
"10.0.139.51",
"10.0.137.189"
]
}
}


TASK: [mongodb | set_fact ]
***************************************************
ok: [10.0.136.46]


TASK: [mongodb | var groups.mongod_rs_member[0]]
******************************
ok: [10.0.136.46] => {
"var": {
"groups.mongod_rs_member[0]": "10.0.136.46"
}
}


TASK: [mongodb | shell echo "HELLO groups.mongod_rs_member
+++++++++++++++++"] ***
changed: [10.0.136.46 -> 10.0.136.46]


TASK: [mongodb | var mongodb_candidate_master]
********************************
ok: [10.0.136.46] => {
"var": {
"mongodb_candidate_master": "10.0.136.46"
}
}


TASK: [mongodb | shell echo "HELLO mongodb_candidate_master
+++++++++++++++++"] ***
changed: [10.0.136.46 -> 10.0.136.46]


TASK: [mongodb | var mongodb_replica_set_hosts[0]]
****************************
ok: [10.0.136.46] => {
"var": {
"mongodb_replica_set_hosts[0]": "10.0.136.46"
}
}


TASK: [mongodb | msg mongodb_replica_set_hosts[0]]
****************************
ok: [10.0.136.46] => {
"msg": "10.0.136.46"
}


TASK: [mongodb | debug msg="{"]
***********************************************
ok: [10.0.136.46] => {
"msg": "10.0.136.46"
}


TASK: [mongodb | shell echo "HELLO mongodb_replica_set_hosts
+++++++++++++++++"] ***
fatal: [10.0.136.46 -> {] => SSH Error: data could not be sent to the
remote host. Make sure this host can be reached over ssh


FATAL: all hosts have already failed -- aborting


The last debug is interesting. I does not have a -name and it seems to
render the value as '{' just like the delegate_to.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/bebb3253-db43-452d-83b5-f9e5fc8f72f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Barry Kaplan
2015-07-16 11:08:14 UTC
Permalink
Is it still unclear Brian?

I also seem to be seeing a related problem. Consider:

- name: Provision Ops RDS instance
hosts: localhost
connection: local
gather_facts: no
tags: [control]


tasks:
...
- shell: |
echo {{groups.consul_server[0]}} && hostname -i && curl localhost:
8500/v1/catalog/services
register: __catalog
delegate_to: groups.consul_server[0]

Here the shell commands runs on localhost, not the host specified. The
output is

TASK: [shell echo {{groups.consul_server[0]}} && hostname -i && curl
localhost:8500/v1/catalog/services
] ***
<groups.consul_server[0]> REMOTE_MODULE command echo 10.0.196.116 &&
hostname -i && curl localhost:8500/v1/catalog/services #USE_SHELL
<groups.consul_server[0]> EXEC ['/bin/sh', '-c', 'mkdir -p
$HOME/.ansible/tmp/ansible-tmp-1437044596.27-54518627543713 && chmod a+rx
$HOME/.ansible/tmp/ansible-tmp-1437044596.27-54518627543713 && echo
$HOME/.ansible/tmp/ansible-tmp-1437044596.27-54518627543713']
<groups.consul_server[0]> PUT /tmp/tmpTGBlM3 TO /home/bkaplan/.ansible/tmp/
ansible-tmp-1437044596.27-54518627543713/command
<groups.consul_server[0]> EXEC ['/bin/sh', '-c', u'LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8 /usr/bin/python
/home/bkaplan/.ansible/tmp/ansible-tmp-1437044596.27-54518627543713/command;
rm -rf /home/bkaplan/.ansible/tmp/ansible-tmp-1437044596.27-54518627543713/
/dev/null 2>&1']
failed: [localhost -> groups.consul_server[0]] => {"changed": true, "cmd": "echo
10.0.196.116 && hostname -i && curl localhost:8500/v1/catalog/services",
"delta": "0:00:00.012358", "end": "2015-07-16 16:33:16.332372", "rc": 7,
"start": "2015-07-16 16:33:16.320014", "warnings": []}
stderr: % Total % Received % Xferd Average Speed Time Time
Time Current
Dload Upload Total Spent Left
Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:--
0curl: (7) Failed to connect to localhost port 8500: Connection refused
stdout: 10.0.196.116
127.0.1.1


Where '10.0.196.116' is groups.consul_server[0]. So the variable resolves
correctly, but delegate to does not seem to use it. This is not specific to
shell, it has the same behavior for other modules (eg, uri).
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/55a50ab9-e411-4d2a-9a6a-ec79eee7fe16%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-17 00:02:28 UTC
Permalink
you need moustaches:

delegate_to: "{{groups.consul_server[0]}}"

only conditionals (when: ) do not need templating, with_ has a
haphazard support for it which we plan to deprecate.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8m27nEjF6Jja2w%2B1biwU3HQNJJy82Nf16Sve_AcG5P5xA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Barry Kaplan
2015-07-17 03:11:33 UTC
Permalink
Hmm, thought I tried that, but will check. Thanks Brian.

I wonder whether it would be better for it to be error if delegate_to does
not resolve rather than silently fall back to localhost... Depending on the
task fallback to localhost could do some serious damage.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/76675027-d744-47ce-b4e2-0d85c498c16d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Barry Kaplan
2015-07-17 04:53:37 UTC
Permalink
I see the same behavior with the moustaches:

- name: Test delegate to
shell: echo "{{ groups.consul_server[0] }}" && hostname -i
register: __hostname

delegate_to: "{{ groups.consul_server[0] }}"
- debug: var=__hostname


TASK: [Test delegate to] *********************************************
<10.0.196.116> REMOTE_MODULE command echo "10.0.196.116" && hostname -i
#USE_SHELL
<10.0.196.116> EXEC ['/bin/sh', '-c', 'mkdir -p
$HOME/.ansible/tmp/ansible-tmp-1437108695.64-26819269533917 && chmod a+rx
$HOME/.ansible/tmp/ansible-tmp-1437108695.64-26819269533917 && echo
$HOME/.ansible/tmp/ansible-tmp-1437108695.64-26819269533917']
<10.0.196.116> PUT /tmp/tmpDXWJFH TO /home/bkaplan/.ansible/tmp/ansible-tmp-
1437108695.64-26819269533917/command
<10.0.196.116> EXEC ['/bin/sh', '-c', u'LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8 /usr/bin/python
/home/bkaplan/.ansible/tmp/ansible-tmp-1437108695.64-26819269533917/command;
rm -rf /home/bkaplan/.ansible/tmp/ansible-tmp-1437108695.64-26819269533917/
/dev/null 2>&1']
changed: [localhost -> 10.0.196.116] => {"changed": true, "cmd": "echo
\"10.0.196.116\" && hostname -i", "delta": "0:00:00.004458", "end": "2015-07-17
10:21:35.693136", "rc": 0, "start": "2015-07-17 10:21:35.688678", "stderr":
"", "stdout": "10.0.196.116\n127.0.1.1", "warnings": []}


TASK: [debug var=__hostname]
**************************************************
ok: [localhost] => {
"var": {
"__hostname": {
"changed": true,
"cmd": "echo \"10.0.196.116\" && hostname -i",
"delta": "0:00:00.004458",
"end": "2015-07-17 10:21:35.693136",
"invocation": {
"module_args": "echo \"10.0.196.116\" && hostname -i",
"module_name": "shell"
},
"rc": 0,
"start": "2015-07-17 10:21:35.688678",
"stderr": "",
"stdout": "10.0.196.116\n127.0.1.1",
"stdout_lines": [
"10.0.196.116",
"127.0.1.1"
],
"warnings": []
}
}
}
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c2e3db4a-3f70-4804-99af-cda0032e9f47%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-17 14:22:49 UTC
Permalink
well, the first difference is that you are not delegating to a host
named 'groups.consul_server[0]' but to 10.0.196.116

Sorry for not noticing before, but the delegate_to on debug is
irrelevant, debug always executes on 'the master' or machine you
execute ansible on, it does not need to connect to any machine, i'm
not sure what you are expecting there.

delegate_to on shell or any module that executes remotely goes to the
delegated server, but things that operate on 'master' like debug,
prompt, add_host, group_by make no sense to delegate.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8mN1LJ1bZ-Qy4HQseRvGbWZrNn87JnhEJGm3EKtm-Vs9A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Barry Kaplan
2015-07-18 02:49:37 UTC
Permalink
Brian, I'm not following you at all. It was the shell command that uses the
delegate_to. The debug is only showing the register from the shell.

- name: Test delegate to
shell: echo "{{ groups.consul_server[0] }}" && hostname -i
register: __hostname
delegate_to: "{{ groups.consul_server[0] }}"

- debug: var=__hostname


"stdout_lines": [
"10.0.196.116", <--- echo "{{ groups.consul_server[0] }}"
"127.0.1.1" <--- hostname -i
],
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/dd207cfa-9bbf-4569-8bde-81bc16511940%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-18 02:52:01 UTC
Permalink
ah, formatting of previous email threw me off, the only reason i can
think of for this happening is that you have connection=local for the
task (from play or command line) and the host does not have it
specified through ansible_connection.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8kn21FRapwvCG6GjeTnYsDNWRuEDQJgOrbcYJFucNHyag%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Barry Kaplan
2015-07-20 05:05:48 UTC
Permalink
All my hosts come from the ec2 inventory plugin. I can access the hosts in
all other ways without having to explicitly specify an ansible_connection.

So then is it better to specify the target host in the play and use
local_action? I kinda thought these
two were inverses of each other.

-barry
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8ab09d1b-7e25-4fa5-8665-fddd2ee2230d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Barry Kaplan
2015-07-24 05:46:02 UTC
Permalink
Perfect! I have removed the connection=local from the play and now all
works as expected. I guess I don't fully understand why connection=local
should be used. When I was first starting with ansible plays that I studied
that did ec2 stuff used it, so I did too.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/83cf0da5-10ea-42ac-8277-848445bfcef1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-24 13:24:26 UTC
Permalink
The connection: local is used when you want a plugin to execute on
master that normally would execute on the target. You normally want to
do this with cloud modules or modules that use some web api. Some
modules always execute on the 'master' as they are meant to just
change the data in the play like, debug, set_fact, add_host, group_by
so these don't need any connection settings.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8nfHkPGDN8Bi8U7e6okt0ERG%3DdRw-wzyiqp3s9qycE05g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...