Skip to content

Instantly share code, notes, and snippets.

@BruceZu
Created December 14, 2016 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BruceZu/33491c8645bcceba4f6db6e645fa9dff to your computer and use it in GitHub Desktop.
Save BruceZu/33491c8645bcceba4f6db6e645fa9dff to your computer and use it in GitHub Desktop.
ansible cannot ping the control machine itself
@BruceZu
Copy link
Author

BruceZu commented Dec 14, 2016

[root@k8s-09 .ssh]# cat /etc/ansible/hosts
localhost:22
....

issue: can not access control machine itself

$ ansible all -m ping 
localhost | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
    "unreachable": true
}

resolved:

// create ssh key

$ssh-keygen  
...
$ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts

// "Your public SSH key should be located in authorized_keys on those systems" here make sure it is in itself authorized_keys 👍
// ansible link
[root@k8s-09 ansible]# cd ~/.ssh/
[root@k8s-09 .ssh]# ls
id_rsa id_rsa.pub known_hosts
[root@k8s-09 .ssh]# touch authorized_keys
// copy
[root@k8s-09 .ssh]# vim id_rsa.pub
// paste
[root@k8s-09 .ssh]# vim authorized_keys

test

]#  ansible all -m ping
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is bc:e8:17:14:3c:8a:8a:68:a6:bf:42:9c:13:80:2f:43.
Are you sure you want to continue connecting (yes/no)? yes
localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
]#  ansible all -m ping -u root  -b --become-user root
localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
#  ansible all -a "/bin/echo hello"
localhost | SUCCESS | rc=0 >>
hello
[root@k8s-09 .ssh]# ansible all -a ' echo hello'
localhost | SUCCESS | rc=0 >>
hello

[root@k8s-09 .ssh]# ansible all -a ' echo hello'  --check
localhost | SKIPPED
[root@k8s-09 .ssh]#
[root@k8s-09 ~]# ansible 127.0.0.1 -a "/bin/echo hello"
localhost | SUCCESS | rc=0 >>
hello

[root@k8s-09 ~]# ansible localhost -a "/bin/echo hello"
localhost | SUCCESS | rc=0 >>
hello
[root@k8s-09 ~]# ansible all -m shell -a 'whoami'  -u root -b --become-user redadm
localhost | SUCCESS | rc=0 >>
redadm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment