- Install ansible on the control node(In this case I used an Ubuntu 20.04 VM created with vagrant on my windows machine).
- Create ssh key pairs.
- Import the public key to AWS.
- Creat an EC2 instance using Ubuntu 22.04 (target machine)
- Connect with the target machine through ssh then exit.
- Edit the /etc/ansible/hosts file adding the IP address of the target machine.
- Ping the ansible inventory to be sure all configurations are set properly
- Create a directory for the ansible.
- Cd into the ansible directory.
- Create a playbook to install apache, set the time zone to Africa/lagos and install php then run a check on the playbook.
- Execute the playbook file.
- Check the result in the target machine to see if the installations were deployed properly, and check the apache service to confirm it is working fine.
- Create and edit an index.php file.
- Using ansible playbook, copy the index.php file from the localhost to the remote server.
- Edit the apache config file on the remote server.
- Check the rendered page on your browser.
To Install ansible on the control node (In this case I used an Ubuntu 20.04 VM created with vagrant).
$ sudo apt update
$ sudo apt install -y software-properties-common python-apt
$ sudo apt install -y ansible
$ ansible --version
$ ssh-keygen -m rsa PEM
$ mv /home/vagrant/.ssh/vagrantkeys /home/vagrant/.ssh/vagrantkeys.pem
Scroll down to Amazon machine images (AMI). Select ubuntu. Leave it at the free tier eligible. (If you are on free-tier, preferable choose machines that have free tier eligibility to save cost.)
Scroll down to summary.Enter the number of instances you want to create. Review your choices. Click on Launch instance.
Paste the command on your terminal. This allows you to connect with your EC2 instance using ssh connection.
Note: do this at the ssh path where you have your keypairs stored so that the connecton can discover the keys.
$ sudo nano /etc/ansible/hosts
$ ansible all -m ping
Create an ansible playbook to install apache and php on the remote VM. (using ppa:ondrej/php repository)
$ nano playbook.yml
Run a check on the playbook to check if the syntax is written correctly. Run this check at the path where you created the playbook.
$ ansible-playbook playbook.yml --check
$ ansible-playbook playbook.yml
$ touch index.php
$ nano index.php
With ansible playbook, copy the index.php file from localhost(control node) to remote server(target machine). First create ansible playbook. then run a check to see if your playbook sytax is correct before executing the playbook.
$ touch indexphp.yml
#To create an index.php file
$ nano indexphp.yml
#To edit the file
$ ansible-playbook indexphp.yml --check
#To check if the playbook syntax is correct.
$ ansible-playbook indexphp.yml
#To execute the playbook
When the playbook has been executed, check from your control node if it was deployed properly on the target machine.
$ ansible all -m shell -a "ls -al /var/www/html/" -b
$ php index.php
After executing all the playbooks (For the apache2 server ,PHP and index.php file), Copy the IP address of your remote server (Ubuntu AWS AMI) and paste it on your browser. You should see the default index.html file hosted on the server.
$ sudo nano /etc/apache2/mods-enabled/dir.conf
$ sudo systemctl restart apache2
$ systemctl status apache2
- Website - Bukola Testimony
- Twitter - @BukolaTestimony