<p>Do the following for each Vagrant machine you want Ansible to work with...in this example, using the machine <code>master1</code>:</p>
<ol>
<li>Do <code>vagrant ssh-config &gt; vagrant-ssh</code></li>
<li>Enter content from <code>vagrant-ssh</code> into your <code>~/.ssh/config</code>, modifying it as needed, like this example for some machine named <code>master1</code>:</li>
</ol>
<pre><code>Host master1
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/myname/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL
</code></pre>
<ol start="3">
<li>Then, in your hosts (inventory) file, include the login information needed by Ansible, for each machine:</li>
</ol>
<pre><code>master1 ansible_host=master1 ansible_port=2222 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant ansible_become_pass=vagrant
</code></pre>
<p>or also use IP address for <code>ansible_host</code>:</p>
<pre><code>master1 ansible_host=192.168.56.2 ansible_port=2222 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant ansible_become_pass=vagrant
</code></pre>

CC BY-SA 4.0
2021-06-01T20:45:49.450Z
thanks again I'm now getting connection refused error instead of permission denied :/
user2152012
CC BY-SA 4.0
2021-06-01T21:22:27.340Z
@user2152012 are you able to ssh into each machine you want to reach with Ansible? Each would need SSH daemon running (if these are *nix machines). Like, should be able to do `ssh machine1`
mikequentel
CC BY-SA 4.0
2021-06-01T21:51:42.693Z
when I ```ssh worker1``` I get ```ssh: Could not resolve hostname worker1: Temporary failure in name resolution``` so I can't
user2152012
CC BY-SA 4.0
2021-06-01T21:55:48.603Z
Maybe need to specify the IP address for `worker1`, so: `worker1 ansible_host=192.168.56.3 ansible_port=<this might be different than master1?> ansible_ssh_user=vagrant ansible_ssh_pass=vagrant ansible_become_pass=vagrant`
mikequentel
CC BY-SA 4.0
2021-06-01T22:15:02.330Z
yh that is what I have done still no luck I edit the files in the question to show how they look now.
user2152012
CC BY-SA 4.0
2021-06-01T18:44:56.640Z
2021-06-01T21:27:31.957Z
2021-06-01T21:27:31.957Z
mikequentel
<p>I'm completely new to ansible and I'm having a hard time with it. I have an inventory.yml file which is as the following:</p>
<p><strong>EDIT: changed my inventory.yml now looks as follows</strong>
inventory.yml:</p>
<pre><code>[master]
master1 ansible_host=192.168.56.2 ansible_port=2222 ansible_user=deploy ansible_ssh_pass=vagrant ansible_become_pass=vagrant apt_key_master=https://packages.cloud.google.com/apt/doc/apt-key.gpg

[worker]
worker1 ansible_host=192.168.56.3 ansible_port=2200 ansible_user=deploy ansible_ssh_pass=vagrant ansible_become_pass=vagrant apt_key_worker=https://packages.cloud.google.com/apt/doc/apt-key.gpg
</code></pre>
<p>also I have a playbook called kube-repos.yml which is as follows:</p>
<p><strong>EDIT: changed kube-repos.yml now looks as follows:</strong>
kube-repos.yml:</p>
<pre><code>- hosts: all
  become: yes
  tasks:

  - name: add Kubernetes apt-key
    apt_key:
      url: &quot;{{ apt_key_master }}&quot;
      state: present

  - name: add Kubernetes' APT repository
    apt_repository:
      repo: deb http://apt.kubernetes.io/ kubernetes-xenial main
      state: present
</code></pre>
<p>when I apply my ansible playbook using <code>ansible-playbook kube-repos.yml</code> I get the following error:</p>
<pre><code>fatal: [master1]: UNREACHABLE! =&gt; {&quot;changed&quot;: false, &quot;msg&quot;: &quot;Failed to connect to the host via ssh: Permission denied (publickey).&quot;, &quot;unreachable&quot;: true}
fatal: [worker1]: UNREACHABLE! =&gt; {&quot;changed&quot;: false, &quot;msg&quot;: &quot;Failed to connect to the host via ssh: Permission denied (publickey).&quot;, &quot;unreachable&quot;: true}
</code></pre>
<p>I see my problem is getting proper authentication ansible to ssh into the host vm's.
I am able to do <code>vagrant ssh master</code> and <code>vagrant ssh worker</code> and get into my vm's perfectly fine but when I apply the playbook and if I use the command <code>ansible all -m ping</code> I get the UNREACHABLE error. I am trying to setup a simple kubernetes cluster consisting of one master and one worker node.</p>
<p>I am at a loss now because I have read the ansible documentation but still cannot figure out how to correct this error. So I feel like I have no choice but to ask on here. I hope someone is kind enough to help me out and thanks in advance.</p>
<p><strong>EDIT:</strong>
after the changes suggested by @mikequentel I have the following error:</p>
<pre><code>fatal: [worker1]: UNREACHABLE! =&gt; {&quot;changed&quot;: false, &quot;msg&quot;: &quot;Failed to connect to the host via ssh: ssh: connect to host 192.168.56.3 port 2200: Connection refused&quot;, &quot;unreachable&quot;: true}
fatal: [master1]: UNREACHABLE! =&gt; {&quot;changed&quot;: false, &quot;msg&quot;: &quot;Failed to connect to the host via ssh: ssh: connect to host 192.168.56.2 port 2222: Connection refused&quot;, &quot;unreachable&quot;: true}
</code></pre>

<kubernetes><ansible-inventory><ssh>
ansible playbook cannot apply, giving host unreachable error
user2152012
# ansible playbook cannot apply, giving host unreachable error

(Asked by: username_0 on 2021-05-31)

I'm completely new to ansible and I'm having a hard time with it. I have an inventory.yml file which is as the following:
EDIT: changed my inventory.yml now looks as follows
inventory.yml:
[master]
master1 ansible_host=192.168.56.2 ansible_port=2222 ansible_user=deploy ansible_ssh_pass=vagrant ansible_become_pass=vagrant apt_key_master=https://packages.cloud.google.com/apt/doc/apt-key.gpg

[worker]
worker1 ansible_host=192.168.56.3 ansible_port=2200 ansible_user=deploy ansible_ssh_pass=vagrant ansible_become_pass=vagrant apt_key_worker=https://packages.cloud.google.com/apt/doc/apt-key.gpg

also I have a playbook called kube-repos.yml which is as follows:
EDIT: changed kube-repos.yml now looks as follows:
kube-repos.yml:
- hosts: all
  become: yes
  tasks:

  - name: add Kubernetes apt-key
    apt_key:
      url: "{{ apt_key_master }}"
      state: present

  - name: add Kubernetes' APT repository
    apt_repository:
      repo: deb http://apt.kubernetes.io/ kubernetes-xenial main
      state: present

when I apply my ansible playbook using ansible-playbook kube-repos.yml I get the following error:
fatal: [master1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey).", "unreachable": true}
fatal: [worker1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey).", "unreachable": true}

I see my problem is getting proper authentication ansible to ssh into the host vm's.
I am able to do vagrant ssh master and vagrant ssh worker and get into my vm's perfectly fine but when I apply the playbook and if I use the command ansible all -m ping I get the UNREACHABLE error. I am trying to setup a simple kubernetes cluster consisting of one master and one worker node.
I am at a loss now because I have read the ansible documentation but still cannot figure out how to correct this error. So I feel like I have no choice but to ask on here. I hope someone is kind enough to help me out and thanks in advance.
EDIT:
after the changes suggested by @username_1 I have the following error:
fatal: [worker1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.56.3 port 2200: Connection refused", "unreachable": true}
fatal: [master1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.56.2 port 2222: Connection refused", "unreachable": true}

**Tags:** kubernetes, ansible-inventory, ssh


---

## 1 answers

### Answer by username_1 on 2021-06-01. Score: 1

Do the following for each Vagrant machine you want Ansible to work with...in this example, using the machine master1:

Do vagrant ssh-config > vagrant-ssh
Enter content from vagrant-ssh into your ~/.ssh/config, modifying it as needed, like this example for some machine named master1:

Host master1
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/myname/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL


Then, in your hosts (inventory) file, include the login information needed by Ansible, for each machine:

master1 ansible_host=master1 ansible_port=2222 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant ansible_become_pass=vagrant

or also use IP address for ansible_host:
master1 ansible_host=192.168.56.2 ansible_port=2222 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant ansible_become_pass=vagrant

> **Comments:**
> **username_0**: thanks again I'm now getting connection refused error instead of permission denied :/  
> **username_1**: @username_0 are you able to ssh into each machine you want to reach with Ansible? Each would need SSH daemon running (if these are *nix machines). Like, should be able to do `ssh machine1`  
> **username_0**: when I ```ssh worker1``` I get ```ssh: Could not resolve hostname worker1: Temporary failure in name resolution``` so I can't  
> **username_1**: Maybe need to specify the IP address for `worker1`, so: `worker1 ansible_host=192.168.56.3 ansible_port=<this might be different than master1?> ansible_ssh_user=vagrant ansible_ssh_pass=vagrant ansible_become_pass=vagrant`  
> **username_0**: yh that is what I have done still no luck I edit the files in the question to show how they look now.  

---

