1

I am running ansible on centos machine

[ansadmin@ansible docker]$ ls
Dockerfile  hosts  simple-devops-image.yml  webapp.war
[ansadmin@ansible docker]$ cat hosts
localhost

simple-devops-image.yml

---
- hosts: all
  become: true

  tasks:
  - name: stop current running container
    command: docker stop simple-devops-container
    ignore_errors: yes

  - name: remove stopped container
    command: docker rm simple-devops-container
    ignore_errors: yes

  - name: remove docker image
    command: docker rmi simple-devops-image
    ignore_errors: yes

  - name: build docker image using war
    command: docker build -t simple-devops-image .
    args:
      chdir: /opt/docker
  - name: create container using simple image
    command: docker run -d --name simple-devops-container -p 8080:8080 simple-devops-image

Even on localhost I am getting permission denied.The user is already with sudo rights.

ansible-playbook -i hosts simple-devops-image.yml --check

PLAY [all] *************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
fatal: [localhost]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ansadmin@localhost: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}

PLAY RECAP *************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

ping is working.

[ansadmin@ansible docker]$ ping localhost
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.024 ms
64 bytes from localhost (::1): icmp_seq=2 ttl=64 time=0.045 ms
64 bytes from localhost (::1): icmp_seq=3 ttl=64 time=0.045 ms
Sara June
  • 451
  • 1
  • 9
  • 28

3 Answers3

3

You don't need ssh connection for the localhost.

Just update your hosts file to include ansible_connection=local for localhost

localhost ansible_connection=local

Also, make sure you are not overriding ansible_connection to ssh anywhere else.

AlexD
  • 8,747
  • 2
  • 29
  • 38
2

The reason this failed is that you weren't telling Ansible to ask for a password, and you hadn't yet set up SSH keys.

Your ssh-copy-id command copies your SSH key to the target host (in this case, the box you're on) and installs it so that password-less SSH works.

Another way to get this to work would be to add the correct flags to the playbook command:

ansible-playbook playbook.yml -k

or if you need a sudo password as well:

ansible-playbook playbook.yml -bkK

  • The -k asks for a password ('key') for the SSH user
  • The -b tells ansible to elevate to a privileged user (defaults to using sudo)
  • The -K asks for the password with which to elevate.
shearn89
  • 3,403
  • 2
  • 15
  • 39
1

running below command fixed the issue.

ssh-copy-id ansadmin@localhost
Sara June
  • 451
  • 1
  • 9
  • 28