Ansible: Quick Start - Run from Mac to Install Apache on AWS/EC2

In this post, I am going to use Ansible on mac to install apache web server on an EC2 instance.


what is Ansible?

Ansible is a configuration management tool of servers.

Similar technologies include Chef and Puppet. Two biggest different from these tools are:

  1. Ansible is in yaml vs Chef & Puppet are in Ruby
  2. Ansible is agentless and executed over ssh


install

brew install ansible


create test server

You can use Terraform to create an EC2 instance. https://web-quickstart.blogspot.com/2021/04/terraform-ec2-instance-with-ssh-setup.html

Make sure you can ssh into the server using the private key, like:

ssh -i test ec2-user@54.88.121.94


hello world

Prepare three files:

hosts

[webservers]
54.88.121.94

ansible.cfg

[defaults]
inventory = hosts
private_key_file = test
remote_user = ec2-user

playbook.yml 

---
- name: update web servers
  hosts: webservers

  tasks:
  - name: test message
    debug:
      msg: "Hello, World!"

 and run the ansible-playbook command to display the message:

% ansible-playbook playbook.yml 

PLAY [update web servers] ********************************************************************************************************


TASK [Gathering Facts] ***********************************************************************************************************

[WARNING]: Platform linux on host 54.88.121.94 is using the discovered Python interpreter at /usr/bin/python, but future

installation of another Python interpreter could change the meaning of that path. See

https://docs.ansible.com/ansible/2.10/reference_appendices/interpreter_discovery.html for more information.

ok: [54.88.121.94]


TASK [test message] **************************************************************************************************************

ok: [54.88.121.94] => {

    "msg": "Hello, World!"

}


PLAY RECAP ***********************************************************************************************************************

54.88.121.94               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


install apache

Modify the playbook to run sudo yum install httpd.

playbook.yml

---

- name: update web servers

  hosts: webservers

  become: yes


  tasks:

  - name: test message

    debug:

      msg: "Hello, World!"

  - name: 

    yum:

      name: httpd

      state: present

ansible-playbook playbook.yml

PLAY [update web servers] ********************************************************************************************************


TASK [Gathering Facts] ***********************************************************************************************************

[WARNING]: Platform linux on host 54.88.121.94 is using the discovered Python interpreter at /usr/bin/python, but future

installation of another Python interpreter could change the meaning of that path. See

https://docs.ansible.com/ansible/2.10/reference_appendices/interpreter_discovery.html for more information.

ok: [54.88.121.94]


TASK [test message] **************************************************************************************************************

ok: [54.88.121.94] => {

    "msg": "Hello, World!"

}


TASK [yum] ***********************************************************************************************************************

changed: [54.88.121.94]


PLAY RECAP ***********************************************************************************************************************

54.88.121.94               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Comments

Popular posts from this blog

Minikube Installation for M1 Mac

Selenide: Quick Start

PyCharm: Quick Start