0
votes

Started learning and working with Ansible. As simple scenario running a Vagrant box in my local and trying run a command using Ansible on that from Host (Mac).

Not able to connect to running vagrant box whilst executing,

  1. tried using password (vagrant)

ansible --ask-pass -i ./host_inventory.yml -m command -a "sudo apt-get install openjdk-6-jdk" 127.0.0.1

  1. Tried using SSH. In this case I copied the public key that was created in host (Mac OS) to my Vagrant Box and added it authorized_keys

ansible -i ./host_inventory.yml -m command -a "sudo apt-get install openjdk-6-jdk" 127.0.0.1

for both getting this error,

   127.0.0.1 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: kex_exchange_identification: Connection closed by remote host",
    "unreachable": true
    }

My environment:

OS: Mac Os Catalina Vagrnt box running: Ubuntu10

Below is the content of host_inventory.yml file,

all:
 hosts: 
   127.0.0.1:8080

In Vagrantfile I have forwarded ports as, config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

But I am able to ssh to the running Vagrant box.

Any suggestions please.

1
127.0.0 is not a valid ipMarcin Orlowski
@MarcinOrlowski 127.0.0.1 is localhost right?!!Srinivasa Poduri
Please read my comment and your post carefully.Marcin Orlowski
:) ... edited to correct IPSrinivasa Poduri

1 Answers

0
votes

Got resolved this.

Change is in Vagrant and inventory host file:

Vagrantfile:

In vagrant file instead of forwarded_port and localhost IP, used private_network IP.

 config.vm.network "private_network", ip: <private IP address>

Ansible Inventory file:

 all:
   hosts:
    local:
       ansible_host: <private IP address>
       ansible_user: vagrant
       ansible_ssh_private_key_file: <path to ssh private key file>


    $ ansible -i host_inventory.yml -m ping local -vvv

local | SUCCESS => {
"ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"invocation": {
    "module_args": {
        "data": "pong"
    }
},
"ping": "pong"}