Ansible 1-2: Initial setup, copying ssh keys to nodes

# Create an ansible directory, then create a virtualenv
# Activate the virtualenv
jdoe@ubuntu-c:~$ mkdir ansible
jdoe@ubuntu-c:~$ cd ansible
jdoe@ubuntu-c:~$ virtualenv venv27
jdoe@ubuntu-c:~$ source venv27/activate
jdoe@ubuntu-c:~$ source venv27/bin/activate

(venv27) jdoe@ubuntu-c:~/ansible$

=====================================
# Create ssh key

(venv27) jdoe@ubuntu-c:~/ansible$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jdoe/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/jdoe/.ssh/id_rsa.
Your public key has been saved in /home/jdoe/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:G/UJexelyg6ONh0QYrTuI45P/m7wVkb7ipO1izjCd/g jdoe@ubuntu-c
The key's randomart image is:
+---[RSA 2048]----+
|     .+ .       .|
|     . o .     o |
|      . o .   o  |
|     . . + +.. . |
|      . S *.+..  |
|     .  .* ==.   |
|     .. B+o=.o   |
|      oB++Bo. .  |
|      .+*OE.oo   |
+----[SHA256]-----+

(venv27) jdoe@ubuntu-c:~/ansible$ ssh centos1
jdoe@centos1's password:
Last login: Mon Aug 30 21:21:29 2021 from 192.168.56.41
[jdoe@centos1 ~]$ ls .ssh
ls: cannot access .ssh: No such file or directory

[jdoe@centos1 ~]$ exit
logout
Connection to centos1 closed.
(venv27) jdoe@ubuntu-c:~/ansible$ ssh-copy-id centos1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pu
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now i
jdoe@centos1's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'centos1'"
and check to make sure that only the key(s) you wanted were added.

(venv27) jdoe@ubuntu-c:~/ansible$ ssh centos1
Last login: Mon Aug 30 21:22:35 2021 from 192.168.56.41
[jdoe@centos1 ~]$ ls .ssh
authorized_keys
[jdoe@centos1 ~]$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDkPFDK4MzMTLEEU3XmBEnFdtEnPvEhxN4kwI+bTRCGTwQMupFzG661DHnd/FK91Ni0paEmtr0KV9n+63RoWiq9NHR5jdTWFwwStSkelZDmE9OhyL9Cfd/xoS4YRfH3jDltnFgsDtxZR+2KAlV1RHi2f+e1xgpsOTh26LxQnIfJBuMJ6Y9FjBrFCdUuh+xUuty2A2d+uFG9yQyY8EcqQWIY1F61t31g00bg8MmvBxLO3hJ jdoe@ubuntu-c
[jdoe@centos1 ~]$ ls -altrh .ssh
total 4.0K
drwx------. 3 jdoe jdoe  95 Aug 30 21:23 ..
drwx------. 2 jdoe jdoe  29 Aug 30 21:23 .
-rw-------. 1 jdoe jdoe 395 Aug 30 21:23 authorized_keys
[jdoe@centos1 ~]$ exit
logout
Connection to centos1 closed.

=====================================
(venv27) jdoe@ubuntu-c:~/ansible$ ssh centos1
Last login: Mon Aug 30 21:23:36 2021 from 192.168.56.41
[jdoe@centos1 ~]$ exit
logout
Connection to centos1 closed.

=====================================
(venv27) jdoe@ubuntu-c:~/ansible$ ansible all -m ping
centos1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

=====================================
(venv27) jdoe@ubuntu-c:~/ansible$ ansible all -i centos1, -m ping
centos1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

=====================================
(venv27) jdoe@ubuntu-c:~/ansible$ ansible all -m debug
centos1 | SUCCESS => {
    "msg": "Hello world!"
}

=====================================
(venv27) jdoe@ubuntu-c:~/ansible$ ansible-doc debug
> DEBUG    (/usr/lib/python2.7/dist-packages/ansible/modules/utilities/logic/debug.py)

        This module prints statements during execution and can be useful for debugging v
        or expressions without necessarily halting the playbook. Useful for debugging to
        with the 'when:' directive. This module is also supported for Windows targets.

  * This module is maintained by The Ansible Core Team
  * note: This module has a corresponding action plugin.

OPTIONS (= is mandatory):

- msg
        The customized message that is printed. If omitted, prints a generic message.
        [Default: Hello world!]
        type: str

- var
        A variable name to debug.
:
[1]+  Stopped                 ansible-doc debug

=====================================
(venv27) jdoe@ubuntu-c:~/ansible$ ansible all -m debug --args='msg="This is a custom mes
centos1 | SUCCESS => {
    "msg": "This is a custom message"
}
(venv27) jdoe@ubuntu-c:~/ansible$ ansible all -m debug --args='msg="This is a custom mes
centos1 | SKIPPED
(venv27) jdoe@ubuntu-c:~/ansible$ ansible all -vvv -m debug --args='msg="This is a custo
ansible 2.9.24
  config file = /home/jdoe/ansible/ansible.cfg
  configured module search path = [u'/home/jdoe/.ansible/plugins/modules', u'/usr/share/
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Mar  1 2021, 11:38:31) [GCC 5.4.0 20160609]
Using /home/jdoe/ansible/ansible.cfg as config file
host_list declined parsing /home/jdoe/ansible/hosts as it did not pass its verify_file()
script declined parsing /home/jdoe/ansible/hosts as it did not pass its verify_file() me
auto declined parsing /home/jdoe/ansible/hosts as it did not pass its verify_file() meth
Parsed /home/jdoe/ansible/hosts inventory source with ini plugin
Skipping callback 'actionable', as we already have a stdout callback.
Skipping callback 'counter_enabled', as we already have a stdout callback.
Skipping callback 'debug', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'full_skip', as we already have a stdout callback.
Skipping callback 'json', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'null', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
Skipping callback 'selective', as we already have a stdout callback.
Skipping callback 'skippy', as we already have a stdout callback.
Skipping callback 'stderr', as we already have a stdout callback.
Skipping callback 'unixy', as we already have a stdout callback.
Skipping callback 'yaml', as we already have a stdout callback.
META: ran handlers
centos1 | SUCCESS => {
    "msg": "This is a custom message"
}
META: ran handlers
META: ran handlers

=====================================
(venv27) jdoe@ubuntu-c:~/ansible$ nano hosts


=====================================
(venv27) jdoe@ubuntu-c:~/ansible$ ansible centos -m ping
centos3 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added 'centos3,192.168.56.47' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
centos2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added 'centos2,192.168.56.46' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
centos1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

=====================================
(venv27) jdoe@ubuntu-c:~/ansible$ ansible all -m ping
ubuntu1 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added 'ubuntu1,192.168.56.42' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).",
    "unreachable": true
}
centos3 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
ubuntu2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added 'ubuntu2,192.168.56.43' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).",
    "unreachable": true
}
centos2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
ubuntu3 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added 'ubuntu3,192.168.56.44' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).",
    "unreachable": true
}
centos1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

=====================================
(venv27) jdoe@ubuntu-c:~/ansible$ ansible '*' -m ping
centos2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
centos3 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
ubuntu1 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
    "unreachable": true
}
ubuntu2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
    "unreachable": true
}
ubuntu3 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
    "unreachable": true
}
centos1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

=====================================
# add ssh keys to rest of the nodes
# Using a simple bash shell script

(venv27) jdoe@ubuntu-c:~/ansible$ for host in ubuntu1 ubuntu2 ubuntu3 centos2 centos3
> do
> ssh-copy-id ${host}
> done

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jdoe@ubuntu1's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'ubuntu1'"
and check to make sure that only the key(s) you wanted were added.

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jdoe@ubuntu2's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'ubuntu2'"
and check to make sure that only the key(s) you wanted were added.

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jdoe@ubuntu3's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'ubuntu3'"
and check to make sure that only the key(s) you wanted were added.

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jdoe@centos2's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'centos2'"
and check to make sure that only the key(s) you wanted were added.

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jdoe@centos3's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'centos3'"
and check to make sure that only the key(s) you wanted were added.

=====================================
# Check the connectivity now using -o option to condense the output
(venv27) jdoe@ubuntu-c:~/ansible$ ansible all -m ping -o
ubuntu2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "deprecations": [{"msg": "Distribution Ubuntu 16.04 on host ubuntu2 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information", "version": "2.12"}], "ping": "pong"}
ubuntu1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "deprecations": [{"msg": "Distribution Ubuntu 16.04 on host ubuntu1 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information", "version": "2.12"}], "ping": "pong"}
centos1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
centos3 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
centos2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
ubuntu3 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "deprecations": [{"msg": "Distribution Ubuntu 16.04 on host ubuntu3 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information", "version": "2.12"}], "ping": "pong"}

=====================================
# --list-hosts command
(venv27) jdoe@ubuntu-c:~/ansible$ ansible centos --list-hosts
  hosts (3):
    centos1
    centos2
    centos3
(venv27) jdoe@ubuntu-c:~/ansible$ ansible ubuntus --list-hosts
  hosts (3):
    ubuntu1
    ubuntu2
    ubuntu3
(venv27) jdoe@ubuntu-c:~/ansible$ ansible centos1 --list-hosts
  hosts (1):
    centos1
(venv27) jdoe@ubuntu-c:~/ansible$ ansible ~.*3 --list-hosts
  hosts (2):
    ubuntu3
    centos3
=====================================

The following is on the server itself, not using virtualenv.

# Automatically add the host keys

jdoe@ubuntu-c:~$ ANSIBLE_HOST_KEY_CHECKING=False ansible all -m ping
centos1 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
jdoe@ubuntu-c:~$ nano ansible.cfg
jdoe@ubuntu-c:~$ cat ansible.cfg
[defaults]
inventory=hosts
host_key_checking = false


# Generate ssh key

jdoe@ubuntu-c:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jdoe/.ssh/id_rsa):
/home/jdoe/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/jdoe/.ssh/id_rsa.
Your public key has been saved in /home/jdoe/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:uZLavOA0vrN9XllYLItFIr4v0V7S8FaLN3uTK9vBNNo jdoe@ubuntu-c
The key's randomart image is:
+---[RSA 2048]----+
|      . . .      |
|     . . o .     |
|      . . o +    |
|       o B B .   |
|      o S O = o  |
|       = = + B o |
|    + + + o o E  |
|   +.B o..  .o + |
|    *+=+.   .oo  |
+----[SHA256]-----+


Add the ssh key to centos1

jdoe@ubuntu-c:~$ ssh-copy-id centos1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jdoe@centos1's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'centos1'"
and check to make sure that only the key(s) you wanted were added.

jdoe@ubuntu-c:~$ ssh centos1
Last login: Sun Aug 29 21:27:04 2021 from 192.168.56.41
[jdoe@centos1 ~]$ ls .ssh
authorized_keys
[jdoe@centos1 ~]$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWo2jNZos/xlxWDrD5HfZwK4HReq2JxlT+DWa0AEpeBSQFwCV1AGamfWilPCIzczQmwKvg4tOYGT0vfFLXPwmMY3N1VP8DzVbgTSGf1g/MnTo6bNr/ipjiwDMk1GHV5lxiohIPS4cIM2kR1FiSbvdVgmzhpNUtyHkrk3fA3cb2unR9Gw7hB1EZXEtI1Pt71LIUPYNdL3oyX6oFxFXzQwt9Gxn0bsaQo7VToUvJMu1FUYRHFrfJe6RNARKgHpSPbo1HRaE99IdLkzPQbUIcweB+Gg55d2cCaSaLCUopUoCuGyOWU+nhQkflWCdt/bwaoc5uRhGh1sODImz0F8iPl2HH jdoe@ubuntu-c
[jdoe@centos1 ~]$ ls -altrh .ssh
total 4.0K
drwx------. 3 jdoe jdoe  95 Aug 29 21:28 ..
-rw-------. 1 jdoe jdoe 395 Aug 29 21:28 authorized_keys
drwx------. 2 jdoe jdoe  29 Aug 29 21:28 .
[jdoe@centos1 ~]$ exit

#######Do a ping
jdoe@ubuntu-c:~$ ansible all -m ping
centos1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

# Another way to ping a single node
jdoe@ubuntu-c:~$ ansible all -i centos1, -m ping
centos1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

# How to print a Hello World in Ansible

jdoe@ubuntu-c:~$ ansible all -m debug
centos1 | SUCCESS => {
    "msg": "Hello world!"
}

# Print a custom message using debug module
jdoe@ubuntu-c:~$ ansible all -m debug --args='msg="This is a custom debug message."'
centos1 | SUCCESS => {
    "msg": "This is a custom debug message."
}


# Debugging a message using verbosity

jdoe@ubuntu-c:~$ ansible -vvv all -m debug --args='msg="This is a custom debug message." verbosity=3'
ansible 2.9.24
  config file = /home/jdoe/ansible.cfg
  configured module search path = [u'/home/jdoe/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Mar  1 2021, 11:38:31) [GCC 5.4.0 20160609]
Using /home/jdoe/ansible.cfg as config file
host_list declined parsing /home/jdoe/hosts as it did not pass its verify_file() method
script declined parsing /home/jdoe/hosts as it did not pass its verify_file() method
auto declined parsing /home/jdoe/hosts as it did not pass its verify_file() method
Parsed /home/jdoe/hosts inventory source with ini plugin
Skipping callback 'actionable', as we already have a stdout callback.
Skipping callback 'counter_enabled', as we already have a stdout callback.
Skipping callback 'debug', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'full_skip', as we already have a stdout callback.
Skipping callback 'json', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'null', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
Skipping callback 'selective', as we already have a stdout callback.
Skipping callback 'skippy', as we already have a stdout callback.
Skipping callback 'stderr', as we already have a stdout callback.
Skipping callback 'unixy', as we already have a stdout callback.
Skipping callback 'yaml', as we already have a stdout callback.
META: ran handlers
centos1 | SUCCESS => {
    "msg": "This is a custom debug message."
}
META: ran handlers
META: ran handlers


# Now modify inventory and send test pings

jdoe@ubuntu-c:~$ cat hosts
[centos]
centos1
centos2
centos3

[ubuntu]
ubuntu1
ubuntu2
ubuntu3
jdoe@ubuntu-c:~$ ansible all -m ping
centos2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
centos3 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
ubuntu1 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
    "unreachable": true
}
ubuntu2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
    "unreachable": true
}
ubuntu3 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
    "unreachable": true
}
centos1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
jdoe@ubuntu-c:~$ ansible '*' -m ping
centos3 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
centos2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
ubuntu1 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
    "unreachable": true
}
ubuntu2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
    "unreachable": true
}
ubuntu3 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
    "unreachable": true
}
centos1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}


# Now add the ssh-keys to all other hosts

jdoe@ubuntu-c:~$ for host in ubuntu1 ubuntu2 ubuntu3 centos2 centos3
> do
> ssh-copy-id ${host}
> done
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jdoe@ubuntu1's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'ubuntu1'"
and check to make sure that only the key(s) you wanted were added.

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jdoe@ubuntu2's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'ubuntu2'"
and check to make sure that only the key(s) you wanted were added.

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jdoe@ubuntu3's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'ubuntu3'"
and check to make sure that only the key(s) you wanted were added.

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jdoe@centos2's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'centos2'"
and check to make sure that only the key(s) you wanted were added.

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jdoe/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jdoe@centos3's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'centos3'"
and check to make sure that only the key(s) you wanted were added.




# Finally, Check the connection using the ping module

jdoe@ubuntu-c:~$ ansible all -m ping -o
ubuntu1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "deprecations": [{"msg": "Distribution Ubuntu 16.04 on host ubuntu1 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information", "version": "2.12"}], "ping": "pong"}
ubuntu2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "deprecations": [{"msg": "Distribution Ubuntu 16.04 on host ubuntu2 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information", "version": "2.12"}], "ping": "pong"}
centos1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
centos3 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
centos2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
ubuntu3 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "deprecations": [{"msg": "Distribution Ubuntu 16.04 on host ubuntu3 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information", "version": "2.12"}], "ping": "pong"}

Leave a comment