COMP 315: Cloud Computing for E-Commerce Lab 2: Ansible
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Lab 2: Ansible
COMP 315: Cloud Computing for E-Commerce
February 20, 2024
Introduction
Note: Do not attempt to use sudo on the university servers.
This lab is focused on Ansible, specifically on writing playbooks for managing files on your local machine. The topics covered in this lab are included in the required reading for this week’s lecture. If you need a refresher, you can refer to the Ansible Playbook Introduction.
Exercise 1: First Steps with Ansible
1. Create an inventory file named hosts .ini and set it up to include only the host machine. The content of the file should look like this:
[local]
localhost ansible_connection=local
2. Test the setup by running the following command:
ansible -i hosts .ini local -m ping
The command should return a success message indicating that Ansible is able to communicate with the host machine.
localhost | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
3. Create a file named ping-playbook.yml and paste the following into it:
---
- name: Ping all hosts
hosts: all tasks:
- name: Check connectivity ping:
loop: "{{ range(1, 4) | list }}" loop_control:
label: "Ping number {{ item }}"
Here, we’re using the ping module to check connectivity to all hosts in the inventory. We’re also using a loop to run the task three times, each time with a different label. (There’s no reason to run ping three times, except to demonstrate how loops are created in Ansible.)
Run this script with the following command:
ansible-playbook -i hosts.ini ping-playbook.yml
Exercise 2: A Longer Playbook
1. Create a playbook named create-dirs-and-scripts.yml that does the following:
● Creates the directories dir 1 to dir 10.
● In each directory, creates a shell script named hello {directory number} .sh that echoes ”Hello from directory {number}” .
● Once all directories and scripts have been created, executes all the scripts.
2. Run your playbook.
Hints:
● Use the file module to create directories.
● Use the copy module to create the shell scripts. Remember to set the correct permissions for the scripts to be executable.
● Use the command module to execute the scripts.
● Use the loop keyword to iterate over a range of numbers from 1 to 10.
● Use the register keyword to capture the output of a command, and the debug module to print it.
2024-07-08