local_action tries to ssh to localhost

I have the following task as part of a playbook which needs to be run on localhost. This tasks is for checking if the ansible host is able to ssh to remote server.

`

  • name: Verify-User
    local_action: shell ssh -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no -i {{ role_path }}/files/authorized_keys.ansible -o ConnectTimeout=10 {{ createuser }}@{{ node }} “echo success”
    register: user_enabled
    ignore_errors: yes

`

This task is called from a playbook using the following command. I need the extra vars to connect to server if ssh fails and create a new ansible user.
`

ansible-playbook -i “1.0.0.0,” setup-host.yml --extra-vars “node=1.0.0.0, ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=test”

`

Running the playbook gives,

`

`

`

hosts": {

“1.0.0.0”: {

“changed”: false,

“msg”: “Failed to connect to the host via ssh: ssh: connect to host localhost port 22: Connection refused\r\n”,

“unreachable”: true

}

}

`

`

`

If i remove ansible_connection=ssh from the command, this gives a different error, but atleast it wasnt trying to ssh.

My question is

  • why is local_action trying to ssh? If its due to ansible_connection=ssh, is there a way this can be overriden for a specific task? “connection: local” dint work.
  • Shouldnt the hosts for this tasks say “localhost” ? Why is it showing the hosts as the ip of remote server. Does this mean local_action did not take effect?

Thanks.

This is precedence issue, caused by extra var ansible_connection=ssh,
'local_action' is the same as 'delegate_to: localhost', but the extra
var setting is overriding localhosts normal 'connection=local'

Thanks. I was curious, Is there a way to override the extra vars on a specific task? I want just one task to have connection: local.

no, extra_vars have the highest precedence, they are really the 'big hammer'

Thanks, Brian