- Published on
Website Under Construction2
- Authors
- Name
- Owais Abbasi
Explanation of the Notes:
Editing the
ansible.cfg
File:- You edited the
ansible.cfg
file usingvim ansible.cfg
. - Line edited:
host_key_checking = False
- Purpose: This disables SSH host key checking when Ansible connects to remote machines. By setting
host_key_checking
toFalse
, Ansible will not ask for confirmation if it has not connected to the target machine before. This makes it easier to manage connections but slightly less secure.
- You edited the
Testing Connection with Ansible Ping:
- Command used:
ansible all -m ping
- Purpose: This tests connectivity between the Ansible master and all the nodes listed in the inventory (
hosts
file). Theping
module ensures that the target machines can be reached over SSH and that Python is installed on them.
- Command used:
SSH Key Configuration:
- SSH Copy Command:
ssh-copy-id 192.168.1.14
- This command copies your public SSH key to the target machine (192.168.1.14), allowing password-less SSH login.
- SSH Key Generation:
ssh-keygen
- Used if no SSH keys exist. You can create a new key pair. When asked to overwrite the existing key, you choose yes if you want to replace the current key.
- SSH Login without Password:
ssh aadmin@192.168.1.14
- After copying the SSH key, this command allows password-less login to the node.
- SSH Copy Command:
Ansible Directory Structure:
- Example directory structure:
. |-- ansible.cfg |-- hosts `-- roles
ansible.cfg
: Configuration file for Ansible.hosts
: Inventory file that lists all the nodes you want to manage with Ansible.roles
: A directory used to organize playbooks into reusable roles (collections of tasks, handlers, variables, and other components).
- Example directory structure:
Checking Ansible Version and Config File Location:
Command used:
ansible --version
Displays the version of Ansible and the current configuration file (
ansible.cfg
) being used. The configuration file can be found in different locations, such as:- The
ANSIBLE_CONFIG
environment variable (if set). - The
ansible.cfg
file in the current directory. - The
ansible.cfg
file in the home directory. - The default
/etc/ansible/ansible.cfg
.
- The
Changing Configuration Path:
export ANSIBLE_CONFIG=/home/aadmin/ansible
- Changes the configuration file being used by Ansible to
/home/aadmin/ansible/ansible.cfg
via the environment variable.
- Changes the configuration file being used by Ansible to
Using the Inventory File and Ping:
Inventory Location:
inventory = /home/aadmin/ansible/hosts
- This specifies the location of the inventory file that contains the IPs or hostnames of the nodes.
Ping using Inventory:
ansible all -i /home/aadmin/inv -m ping
- You manually specify the inventory file to check the connection to all hosts listed.
Testing Connections to Specific IPs:
Ping specific hosts:
ansible 192.168.1.14 -m ping
- This command checks the connection to a single host (
192.168.1.14
) using Ansible.
ansible 192.168.1.14:192.168.1.15 -m ping
- Checks multiple hosts separated by colons (
:
).
- This command checks the connection to a single host (
Managing Groups of Hosts:
Inventory Groups:
[docker] 192.168.1.14 [was] 192.168.1.15 192.168.1.16 192.168.1.17
- The
hosts
file defines groups of hosts. You can group hosts by categories likedocker
andwas
.
- The
Ping Specific Groups:
ansible docker -m ping ansible was -m ping
- These commands ping all the nodes in the
docker
andwas
groups, respectively.
- These commands ping all the nodes in the
Common Issues:
- Some nodes, like
192.168.1.15
,192.168.1.16
, and192.168.1.17
, are unreachable. This is typically caused by issues such as:- SSH keys not being copied properly.
- SSH service not running or misconfigured on the nodes.
- Firewall rules preventing the connection.
- Some nodes, like
Commands on Master and Node Machines:
Master Machine Commands:
- Edit
ansible.cfg
:vim /etc/ansible/ansible.cfg
- Generate SSH Keys (if needed):
ssh-keygen
- Copy SSH Key to Nodes:
ssh-copy-id 192.168.1.14 ssh-copy-id 192.168.1.15 ssh-copy-id 192.168.1.16 ssh-copy-id 192.168.1.17
- Ping Nodes via Ansible:
ansible all -m ping ansible docker -m ping ansible was -m ping
- Edit
Node Machine Commands:
- On each node, ensure SSH is configured correctly:
- Check SSH service is running:
sudo systemctl status ssh
- Ensure SSH port (22) is open in the firewall.
- Verify SSH keys have been copied correctly:
ls ~/.ssh/authorized_keys
- Check SSH service is running:
- On each node, ensure SSH is configured correctly:
These commands will help you configure and troubleshoot Ansible and SSH connectivity between the master and nodes.---
AI Driven Secure Cloud DevOps - Your source for everything DevOps, cloud, and security.