Q: "fakehost in the second task is just a placeholder, right?"
A: Yes. It's just an alias (placeholder).
Run the playbook with -vvvv to enable connection debugging
. You'll see
verbosity: 4
connection: smart
Connection plugins says nothing about smart
. It's necessary to see the source.
ansible/lib/ansible/config/base.yml says
DEFAULT_TRANSPORT:
name: Connection plugin
default: smart
description: "Default connection plugin to use, the 'smart' option will toggle
between 'ssh' and 'paramiko' depending on controller OS and ssh
versions"
The next step would be to dig the source and find out how ssh and paramiko handle localhost. (Wild guess, do nothing and use connection local
).
Connection plugin local says
"The remote user is ignored, the user with which the ansible CLI was executed is used instead."
Let's add a remote_user
to the playbook
shell> cat pb.yml
- hosts: localhost
gather_facts: no
remote_user: admin
tasks:
- name: localhost without explicit connection
debug:
msg:
- "{{ inventory_hostname }}"
- "{{ ansible_user }}"
- hosts: fakehost
gather_facts: no
connection: local
remote_user: admin
tasks:
- name: runner host using local connection
debug:
msg:
- "{{ inventory_hostname }}"
- "{{ ansible_user }}"
gives
shell> whoami
vlado
shell> ansible-playbook -i hosts pb.yml
ok: [localhost] =>
msg:
- localhost
- vlado
ok: [fakehost] =>
msg:
- fakehost
- vlado
The inventory was
shell> cat hosts
fakehost
I'd conclude that Ansible is "smart" enough to use the local
connection plugin in both cases.