Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible local_action on host without local ssh daemon

Tags:

ansible

How can I run a local command on a Ansible control server, if that control server does not have a SSH daemon running?

If I run the following playbook:

- name: Test commands
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Test local action
      local_action: command echo "hello world"

I get the following error:

fatal: [localhost]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host localhost port 22: Connection refused", "unreachable": true}

It seems that local_action is the same as delegate_to: 127.0.0.1, so Ansible tries to ssh to the localhost. However, there is no SSH daemon running on the local controller host (only on the remote machines).

So my immediate question is how to run a specific command from Ansible, without Ansible first trying to SSH to localhost.


Crucial addition, not in the original question:

My host_vars contained the following line:

ansible_connection: ssh
like image 272
MacFreek Avatar asked Apr 18 '20 20:04

MacFreek


People also ask

Can Ansible work without SSH?

Ansible can use a variety of connection methods beyond SSH. You can select any connection plugin, including managing things locally and managing chroot, lxc, and jail containers.

What is Local_action in Ansible?

The local_action feature of Ansible is a powerful one, especially when we think of Orchestration. This feature allows you to run certain tasks locally on the machine that runs Ansible. Consider the following situations: Spawning a new machine or creating a JIRA ticket.


1 Answers

how to run a specific command from Ansible, without Ansible first trying to SSH to localhost.

connection: local is sufficient to make the tasks run in the controller without using SSH.

Try,

- name: Test commands
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Test local action
      command: echo "hello world"
like image 151
franklinsijo Avatar answered Sep 19 '22 15:09

franklinsijo