skip to Main Content

I’m using Ubuntu Linux

I have created an inventory file and I have put my own system IP address there.

I have written a playbook to install the nginx package.

I’m getting the following error:

false, msg" : Failed to connect to the host via ssh: connect to host myip : Connection refused, unreachable=true

How can I solve this?

2

Answers


  1. You could use the hosts keyword with the value localhost

    - name: Install nginx package
      hosts: localhost
    
      tasks:
      - name: Install nginx package
        apt:
          name: nginx
          state: latest
    
    Login or Signup to reply.
  2. Putting your host IP directly in your inventory treats your local machine as any other remote target. Although this can work, ansible will use the ssh connection plugin by default to reach your IP. If an ssh server is not installed/configured/running on your host it will fail (as you have experienced), as well as if you did not configure the needed credentials (ssh keys, etc.).

    You don’t need to (and in most common situations you don’t want to) declare localhost in your inventory to use it as it is implicit by default. The implicit localhost uses the local connection plugin which does not need ssh at all and will use the same user to run the tasks as the one running the playbook.

    For more information on connection plugins, see the current list

    See @gary lopez answer for an example playbook to use localhost as target.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search