Ansible Playbook

Feb 23, 2024
Ansible Playbook

Introduction

Thank you for clicking through to my arcticle. I've been a DevOps engineer for 2 years in dev-team of 7 engineers.

My name is MINSEOK, LEE, but I use Unchaptered as an alias on the interenet. So, you can call me anythings "MINSEOK, LEE" or "Unchaptered" to ask something.


 

Topic

  1. Prac 1 : set-up hosts in localhost

  2. Prac 2 : set-up NGINX Sinario

    1. Install NGINX.

    2. Send NGINX configuration file.

    3. Restart NGINX.

Prerequisites

  1. What is the Ansible?

  2. How to configure practice environment of Ansible?

    1. What is the Ansible Core?

  3. Ansible with PUTTY

  4. Ansible Practices

What is the idempotent?

The result is always the same no matter how many times you apply the operation

Prac 1 : Set-up hosts in localhost

  • Reference File

- name : Ansible_Vim
  hosts: localhost
  tasks:
   - name: Add ansible_hosts
     blockinfile:
       path: /etc/ansible/hosts
       block: |
          [bloter]
          192.168.1.13

  1. Create Ansible-Playbook File

echo "- name : Ansible_Vim
  hosts: localhost
  tasks:
   - name: Add ansible_hosts
     blockinfile:
       path: /etc/ansible/hosts
       block: |
          [bloter]
          192.168.1.13" > bloter.yml
  1. Execute Ansible-Playbook

ansible-playbook bloter.yml
  1. Check result of Ansible-Playbook

cat /etc/ansible/hosts

Prac 2 : Set-up NGINX Scenario

  • Reference File

- hosts: server
  remote_user: root
  tasks:
  - name: Install epel-release
    yum: name=epel-release state=latest

  - name: Install nginx web server
    yum: name=nginx state=present

  - name: Start nginx web server
    service: name=nginx state=started

  1. Create nginx.yml file

echo "- hosts: server
  remote_user: root
  tasks:
  - name: Install epel-release
    yum: name=epel-release state=latest

  - name: Install nginx web server
    yum: name=nginx state=present

  - name: Start nginx web server
    service: name=nginx state=started" > nginx.yml

  1. Execute Ansible-Playbook

ansible-playbook nginx.yml -k

  1. Disable default firewall in nginx

ansible server -m shell -a "systemctl stop firewalld"

  1. Download nginx defualt page

curl -o index.html https://www.nginx.com

  1. Change nginx.yml file

echo "- hosts: server
  remote_user: root
  tasks:
  - name: Install epel-release
    yum: name=epel-release state=latest

  - name: Install nginx web server
    yum: name=nginx state=present

  - name: Upload default index.html for webserver
    copy: src=index.html dest=/usr/share/nginx/html mode=0644

  - name: Start nginx web server
    service: name=nginx state=started" > nginx.yml
Share article

Unchaptered