Deploying a web server using Ansible

You can perform this example either on your local machine or a server. I would recommend to perform this operation on a server, that will take you through all the things required to set up. First launch an EC2 machine in a public subnet and make sure you open the SSH port from wherever you will be running your Ansible playbook.

  1. Create a file in your server from where you will initiate the playbook:
    # vi playbook.yaml 

---

- hosts: servers
sudo: yes
# You should give your remote server user name
remote_user: ec2-user
tasks:
- name: Updating System
yum: name=* state=latest
- name: Install EPEL-repo
yum: name=epel-release state=latest
- name: Install nginx
yum: name=nginx state=latest
- name: start the service
cmd: systemctl start nginx
  1. Now, create the inventory file. You can give any name to this inventory file:
    #vi hosts

[servers]
ansible_ssh_private_key_file="/path/to/your/sshkey.pem" ansible_host="YOURIP"
  1. Now to run the playbook, we can use the following command:
    # ansible-playbook playbook.yml -i hosts
  1. This command will go to the remote server and install Nginx package and start the service of Nginx. After that, hit the public IP, and you will get the Nginx welcome page.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.79.121