[Ansible] 05-1. Playbook 개선
카테고리: ANSIBLE
태그: ansible
[Ansible] 05-1. Playbook 개선
🔔 자세한 모듈 내용은 아래 문서를 참고
모듈 내용 : https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#modules
예제 Github : https://github.com/revenge1005/Ansible_study
📜 개선 전
$ cat <<EOF > install_apache5.yml
---
- hosts: all
become: true
vars:
source_file: ./files/index.html
destin_file: /var/www/html/
tasks:
- name: install apache2 package for Ubuntu
apt:
name: apache2
state: latest
update_cache: yes
when: ansible_distribution == "Ubuntu"
- name: apache2 Service Start & enable for Ubuntu
service:
name: apache2
state: started
enabled: yes
when: ansible_distribution == "Ubuntu"
- name: Copy HomePage file to WEB Server for Ubuntu
copy:
src: "{{ source_file }}"
dest: "{{ destin_file }}"
mode: 0555
when: ansible_distribution == "Ubuntu"
notify: Restart Apache Debian
- name: install httpd packge for Rocky
dnf:
name: httpd
state: latest
update_cache: yes
when: ansible_distribution == "Rocky"
- name: httpd Service Start & enable for Rocky
service:
name: httpd
state: started
enabled: yes
when: ansible_distribution == "Rocky"
- name: Copy HomePage file to WEB Server for Rocky
copy:
src: "{{ source_file }}"
dest: "{{ destin_file }}"
mode: 0555
when: ansible_distribution == "Rocky"
notify: Restart Apache RedHat
handlers:
- name: Restart Apache RedHat
service:
name: httpd
state: restarted
- name: Restart Apache Debian
service:
name: apache2
state: restarted
EOF
$ ansible-playbook --ask-become-pass install_apache3.yml

📜 개선 후
$ cat <<EOF > install_apache5.yml
---
- hosts: all
become: true
tasks:
- name: install Apache package
package:
name: "{{ apache_package }}{% raw %}"
state: latest
update_cache: yes
- name: Apache Service Start & enable
service:
name: "{% raw %}{{ apache_package }}"
state: started
enabled: yes
- name: Copy HomePage file to WEB Server
copy:
src: "{{ source_file }}"
dest: "{{ destin_file }}"
mode: 0555
notify: Restart Apache
handlers:
- name: Restart Apache
service:
name: "{{ apache_package }}"
state: restarted
EOF
$ cat <<EOF > inventory
[apache]
node01 apache_package=httpd
node02 apache_package=httpd
node03 apache_package=apache2
[apache:vars]
source_file="./files/index.html"
destin_file="/var/www/html/"
EOF
$ ansible-playbook --ask-become-pass install_apache5.yml

댓글 남기기