Difference between hosts: localhost and connection: local

Running the task on the localhost can be achieved in different ways.

I do not understand if there is a difference between the way the two tasks are executed below or if they are exactly the same thing.

If so, fakehost in the second task is just a placeholder, right?

- hosts: localhost gather_facts: no tasks: - name: localhost without explicit connection boto3_facts:
- hosts: fakehost connection: local gather_facts: no tasks: - name: runner host using local connection boto3_facts:

1 Answer

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like