- Install Ansible
brew install ansible
- Check Ansible version
ansible --version
- Update the apt package index
sudo apt update
sudo apt upgrade
- Install Ansible
sudo apt install ansible
- Check Ansible version
ansible --version
- Create a directory for Ansible
mkdir ansible
cd ansible
- Create a file named
playbook.yml
using the following command
touch playbook.yml
- Copy the following code and paste it in the file
playbook.yml
- name: Install and configure web server
hosts: localhost
connection: local
become: true
tasks:
- name: Install Apache web server
apt:
name: apache2
state: present
- name: Create index.html file
template:
src: index.html.j2
dest: /var/www/html/index.html
- name: Set ownership and permissions on index.html
file:
path: /var/www/html/index.html
owner: www-data
group: www-data
mode: "0644"
handlers:
- name: restart apache2
service:
name: apache2
state: restarted
- Create a file named
index.html.j2
using the following command
touch index.html.j2
- Copy the following code and paste it in the file
index.html.j2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ansible</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
- Run the playbook using the following command
ansible-playbook playbook.yml
-
Open the browser and type
localhost
Link in the address bar and hit enter -
You will see the following output