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
You don't need
ssh
connection for thelocalhost
.Just update your
hosts
file to includeansible_connection=local
forlocalhost
Also, make sure you are not overriding
ansible_connection
tossh
anywhere else.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
-k
asks for a password ('key') for the SSH user-b
tells ansible to elevate to a privileged user (defaults to using sudo)-K
asks for the password with which to elevate.running below command fixed the issue.