Ansible Demo Ansible Pull Galaxy Playbook

Demo về cách sử dụng Ansible

Table of Contents

Demo về cách sử dụng Ansible

Yêu cầu

Đã setup Ansible để master và client có thể connect được với nhau theo cách Dynamic Inventory trong link sau: Setup Ansible Dynamic Inventory Aws

Cách làm

1. Change owner of /etc/ansilbe

cd /etc/ansible
sudo chown -R ec2-user:ec2-user . 

2. Tạo playbook define các role /etc/ansible/lamp.yml

cd /etc/ansible
nano /etc/ansible/lamp.yml

Nội dung file /etc/ansible/lamp.yml như sau:

---
- hosts: all
  become: yes
  roles:
    - apache
    - php

Ctr+X rồi Yes-Enter để save file

3. Tạo folder chứa các roles

cd /etc/ansible
mkdir roles

4. Tạo role php bằng ansible-galaxy

cd /etc/ansible/roles
ansible-galaxy init php
cd php
ls -lsa

5. Tạo task cho role

cd tasks
ls -lsa
nano main.yml

Nội dung file /etc/ansible/roles/php/tasks/main.yml như sau:

---
- name: Install php 7 with the common packages
  yum:
    name: "{{ item }}"
    state: present
    lock_timeout: 60
  with_items:
    - php70
    - php70-gd
    - php70-imap
    - php70-mbstring
  notify: restart Apache
- name: Upload index.php file to remote web dir
  copy:
    src: index.php
    dest: /var/www/html
    owner: ec2-user
    group: ec2-user

Ctr+X rồi Yes-Enter để save file

6. Tạo file index.php để hiển thị

cd /etc/ansible/roles/php/files/
nano index.php

Nội dung file /etc/ansible/roles/php/files/index.php:

<!DOCTYPE html>
<html>
<head>
  <title>Welcome to devops on AWS</title>
</head>
<body>
<h1><?php echo "This text was generated by php7"?></h1>
</body>
</html>

Ctr+X rồi Yes-Enter để save file

7. Tạo file trong folder handlers

cd /etc/ansible/roles/php/handlers/
nano main.yml

Nội dung file /etc/ansible/roles/php/handlers/main.yml:

---
- name: restart Apache
  service:
      name: httpd
      state: restarted

Ctr+X rồi Yes-Enter để save file

8. Tạo role apache bằng ansible-galaxy

cd /etc/ansible/roles
ansible-galaxy init apache
cd /etc/ansible/roles/apache/tasks
nano main.yml

Nội dung file /etc/ansible/roles/apache/tasks/main.yml

---
- name: Ensure that the httpd package is not installed in system
  yum:
    name: httpd
    state: absent
    lock_timeout: 60
- name: Ensure that the httpd-tool package is not installed in system
  yum:
    name: httpd-tools
    state: absent
    lock_timeout: 60

Ctr+X rồi Yes-Enter để save file

9. Sửa denpendencies trong /etc/ansible/roles/php/meta

cd /etc/ansible/roles/php/meta
nano main.yml

Tìm đến dòng denpendencies và sửa như sau:

dependencies: 
 - apache

Ctr+X rồi Yes-Enter để save file

10. Run ansible-playbook

cd /etc/ansible
ansible-playbook lamp.yml

Check nếu kết quả ok hết và failed = 0 là được.

Vào check public ip của 2 con ec2 nếu hiển thị là OK.

11. Giới thiệu Ansible Pull

Use case:

Trường hợp bạn sửa 1 dòng trong ansible-master và muốn apply trên hàng chục server con khác.

Trước tiên cần chuẩn bị 1 github repository.

Tạo 1 github repo (giả sử https://github.com/hoangmnsd/ansible-pull)

SSH vào con ansible-master

11.a. Commit các file cần thiết lên github

cd /etc/ansible/
git init
git add ec2.py ec2.ini
git commit -m "Config ansible to work with dynamic inventory file"
git add roles/ lamp.yml
git commit -m "Add playbook and role for apache&php7"

11.b. Push file trong folder ansible lên github

cd /etc/ansible/
git remote add origin https://github.com/hoangmnsd/ansible-pull.git
git push origin master 

11.c. Install git và ansible trên tất cả các client

ansible --become -m yum -a "name=git enablerepo=epel state=installed" all
ansible --become -m pip -a "name=ansible state=present" all

11.d. Add file localhost

cd /etc/ansible
nano localhost

Nội dung file /etc/ansible/localhost như sau

[localhost]
localhost ansible_connection=local

Ctr+X rồi Yes-Enter để save file

11.e. Push file /etc/ansible/localhost lên github

git add localhost
git commit -m "add localhost inventory file"
git push origin master 

11.f. Tạo cron job để EC2 tự run câu lệnh ansible-pull 5 phút 1 lần

ansible -m cron -a 'name=ansible-pull minute="*/5" job="/usr/local/bin/ansible-pull -U https://github.com/hoangmnsd/ansible-pull lamp.yml -i localhost"' all

11.g. Test cron job chạy đúng chưa

Giờ sửa nội dung file /etc/ansible/roles/php/files/index.phpnhư sau:

cd /etc/ansible/roles/php/files/
nano index.php

Nội dung file như sau:

<!DOCTYPE html>
<html>
<head>
  <title>Welcome to devops on AWS</title>
</head>
<body>
<h1><?php echo "This text was generated by php7"?></h1>
<h1><?php echo "This change reflect Ansible pull cron job"?></h1>
</body>
</html>

Push thay đổi đó lên github:

git add .
git commit -m "Test ansible pull"
git push origin master 

Chờ khoảng 5 phút sau vào public IP của 2 con ansible-masteransible-client xem đã xuất hiện dòng This change reflect Ansible pull cron job thì là OK

*Note:

Tại thời điểm mình viết bài này thì mình đang sử dụng ansible 2.8

ansible 2.8 đang có 1 bug là “yum lockfile is held by another user” như link sau: https://unix.stackexchange.com/questions/522246/yum-lockfile-is-held-by-another-user

Cần fix workaround là với những task nào có lệnh yum thì phải thêm lock_timeout: 180 vào

-> Thế nên cả apache và php task mình đều đã thêm dòng này.

Thank You!

Your comment has been submitted. It will appear on this page shortly! OK

Yikes, Sorry!

Error occured. Couldn't submit your comment. Please try again. Thank You! OK

Only 1 comment on Ansible Demo Ansible Pull Galaxy Playbook

    Leave a comment