0

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 @mikequentel 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}

1 Answer 1

1

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

  1. Do vagrant ssh-config > vagrant-ssh
  2. 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
  1. 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
5
  • thanks again I'm now getting connection refused error instead of permission denied :/ Commented Jun 1, 2021 at 20:45
  • @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 Commented Jun 1, 2021 at 21:22
  • when I ssh worker1 I get ssh: Could not resolve hostname worker1: Temporary failure in name resolution so I can't Commented Jun 1, 2021 at 21:51
  • 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 Commented Jun 1, 2021 at 21:55
  • yh that is what I have done still no luck I edit the files in the question to show how they look now. Commented Jun 1, 2021 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.