0% found this document useful (0 votes)
36 views3 pages

Ansible Certification Exercise 03

anisible quiz

Uploaded by

abdullafahadh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

Ansible Certification Exercise 03

anisible quiz

Uploaded by

abdullafahadh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Ansible Lab Exercise Guide

1. SSH into the Target Hosts (ubuntu1 and centos2):

- Use the following command on ubuntu-c to SSH into each host:

ssh ansible@<target_host_IP>

- Enter the password when prompted.

2. Generate a New SSH Key Pair on ubuntu-c for Ansible:

- On ubuntu-c, generate a new SSH key pair:

ssh-keygen -t rsa -b 2048 -f ~/.ssh/ansible_key

- Distribute the public key to ubuntu1, ubuntu2, and centos1:

ssh-copy-id -i ~/.ssh/ansible_key ansible@ubuntu1

ssh-copy-id -i ~/.ssh/ansible_key ansible@ubuntu2

ssh-copy-id -i ~/.ssh/ansible_key ansible@centos1

- Verify the connectivity using:

ssh -i ~/.ssh/ansible_key ansible@ubuntu1

3. Undo the SSH Keypair Connection:

- Remove the authorized keys from each target host:

ssh ansible@ubuntu1 "rm ~/.ssh/authorized_keys"

ssh ansible@ubuntu2 "rm ~/.ssh/authorized_keys"

ssh ansible@centos1 "rm ~/.ssh/authorized_keys"

4. Create a Shell Script Using sshpass to Connect to Target Hosts:

- Install sshpass on ubuntu-c if it is not already installed:

sudo apt-get install sshpass

- Create a shell script, connect_ansible.sh, with the following content:

#!/bin/bash

HOSTS=("ubuntu1" "ubuntu2" "centos1" "centos2")


for host in "${HOSTS[@]}"; do

sshpass -p 'password' ssh -o StrictHostKeyChecking=no ansible@$host "echo 'Connected to

$host'"

done

5. Update the Script to Allow Root User Access:

- Modify connect_ansible.sh to connect as both ansible and root:

#!/bin/bash

HOSTS=("ubuntu1" "ubuntu2" "centos1" "centos2")

for host in "${HOSTS[@]}"; do

sshpass -p 'ansible_password' ssh -o StrictHostKeyChecking=no ansible@$host "echo

'Connected to $host as ansible'"

sshpass -p 'root_password' ssh -o StrictHostKeyChecking=no root@$host "echo 'Connected

to $host as root'"

done

6. Configure sshd on centos1 to Accept Connections Only on Port 2222:

- SSH into centos1 and execute the command:

ssh ansible@centos1

sudo /utils/update_sshd_ports.sh 2222

7. Undo All Connections from ubuntu-c to centos1:

- Remove the SSH keys or clean up any specific configurations on centos1:

ssh ansible@centos1 "rm ~/.ssh/authorized_keys"

8. Set Up SSH Keypair Connection with centos1:

- On ubuntu-c, generate another SSH key if needed and set it up for centos1:

ssh-keygen -t rsa -b 2048 -f ~/.ssh/centos1_key

ssh-copy-id -i ~/.ssh/centos1_key ansible@centos1


9. Update sshd on centos1 to Accept Connections on Ports 22 and 2222:

- SSH into centos1 and run:

ssh ansible@centos1

sudo /utils/update_sshd_ports.sh 2222 22

You might also like