Step-by-Step Guide to Setting Up an Ansible Environment with Virtual Machines

Power Off the Virtual Machine:

  • Power off the virtual machine you want to clone.

Clone the Virtual Machine:

  • Right-click on the powered-off virtual machine and select Clone.
  • In the new virtual machine page, give the clone a name (e.g., Ansible controller for the first clone).
  • Ensure you check the Reset MAC address option to assign a new MAC address to each virtual machine.
  • On the next page, select Linked clone to save space by not cloning the entire virtual machine.
  • Click Clone to create the clone.

Create Additional Clones:

  • Repeat the cloning process for other targets (e.g., Ansible-target1).
  • Again, check the Reset MAC address option and choose Linked clone.

Power On the Virtual Machines:

  • Power on the newly created Ansible controller and target virtual machines.

Log into the Virtual Machines:

  • Log into each virtual machine using the default password (osboxes).
  • Open a terminal and run the ifconfig command to find the IP address of each machine.

Establish SSH Sessions:

  • Establish an SSH session to the Ansible controller using its IP address (e.g., 192.168.1.113).
  • Name the session as ansible-controller.
  • Similarly, establish an SSH session to the Ansible target (e.g., 192.168.1.114) and name it as ansible-target1.

Rename the Hostnames:

  • Edit the /etc/hostname file on each virtual machine to rename them:
    • For the controller, replace osboxes with Ansible controller.
    • For the target, replace osboxes with target1.
  • Save the changes.
  • Edit the /etc/hosts file to reflect the new hostname.

Restart the Systems:

  • Restart each system to apply the changes.

Re-establish SSH Sessions:

  • After the systems restart, log back into them to confirm the hostname changes.

Install Ansible on the Controller:

  • On the Ansible controller, install Ansible using the following command:

yum install ansible

  • Confirm the installation by running:

ansible --version

Test Connectivity Between Ansible and the Target:

  • SSH into the target machine from the controller to ensure connectivity.
  • Confirm the key fingerprint when prompted.

Create a Test Project:

  • On the controller, create a folder named test-project.
  • Inside the folder, create an inventory.txt file with the following content:

 

target1 ansible_host=192.168.1.114 ansible_ssh_pass=osboxes.org

  • Run the following command to test connectivity:

ansible target1 -m ping -i inventory.txt

  • A successful connection will return a pong message.

Create Another Clone:

  • Clone another target machine (e.g., Ansible-target2), following the same steps as before.
  • Ensure the Reset MAC address option is checked, and select Linked clone.

Configure the New Target:

  • Power on the new target machine.
  • Log into it and find the IP address (e.g., 192.168.1.115).
  • SSH into the new target and rename its hostname to target2.
  • Restart the system.

Update the Inventory File:

  • Add the new target to the inventory.txt file:

target2 ansible_host=192.168.1.115 ansible_ssh_pass=osboxes.org

Test Connectivity for Target2:

  • Run the ping test for both target1 and target2.
  • If the ping test for target2 fails, SSH manually into target2 from the controller and accept the key fingerprint.

Disable Host Key Checking (Optional):

  • If you prefer not to SSH manually, you can disable host key checking by editing the Ansible configuration file /etc/ansible/ansible.cfg:
    • Search for the host_key_checking line and set it to False.
  • Save the file and rerun the ping test.

Conclusion:

  • The Ansible controller should now be able to successfully connect to both target machines.
  • For production environments, it is recommended to use SSH keys instead of passwords for better security.

Comments