Configure SSH Connetion

To Connect Linux host, Create the ansible inventory file like below

sample inventory file for Linux servers

Add the follow line into the hosts file. This is sample Kubernetes nodes, we use master node as control server.

[master]
localhost              ansible_connection=local

[workers]
worker01     ansible_connection=ssh        ansible_user=root
worker02     ansible_connection=ssh        ansible_user=root

[cluster:children]
master
workers

Generate the ssh key in the master server and copy the public key in the work node root user. Also enable the root user ssh access for the worker nodes.

ssh-keygen -t rsa -b 4096  -N '' -f ~/.ssh/id_rsa

ssh-copy-id  -i ~/.ssh/id_rsa.pub user@hostname

Test the ssh connection

ansible -i hosts workers  -m ping

ansible -m command -a "df -h" servers

ansible -i hosts  all -m ping -u <username>

ansible -i hosts all -m ping -u <username> -b

To connect with docker continer

Start the docker container like this CentOS8 with SSH

create the hosts file like below:

[nodes]
node01 ansible_host=192.168.16.7    ansible_connection=ssh        ansible_user=root ansible_port=2222 ansible_ssh_private_key_file=~/.ssh/id_ed25519

Test the SSH connection

ansible -i hosts nodes -m ping

node01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}


Copyright © 2004, Software Groups