Score:1

Ansible playbook not running for localhost also, how to fix this?

cn flag

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
Score:2
jp flag

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.

Score:1
cn flag

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.
Score:0
cn flag

running below command fixed the issue.

ssh-copy-id ansadmin@localhost
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.