Rút gọn playbook
Tạo biến trong file inventory
192.168.100.51 package_php="libapache2-mod-php" package_apache="apache2"
192.168.100.52 package_php="libapache2-mod-php" package_apache="apache2"
192.168.100.53 package_php="libapache2-mod-php" package_apache="apache2"
10.20.0.63 package_php="php" package_apache="httpd"
Chỉnh lại các file playbook
install_apache.yaml
---
- hosts: all
become: true
tasks:
- name: install apache and php
package:
update_cache: true
name:
- "{{ package_php }}"
- "{{ package_apache }}"
state: latest
Giải thích:
| "{{ package_php }}" | Sử dụng variable đặt trong file inventory. Được đặt trong {{ }} theo kiểu gọi variable của jinja2. Ansible dùng jinja2. Đặt trong "" vì là string |
| package |
Là generic package của ansible. Tùy theo OS (yum, dnf, apt,...) https://docs.ansible.com/ansible/latest/collections/ansible/builtin/package_module.html |
remove_apache.yaml
---
- hosts: all
become: true
tasks:
- name: remove apache and php
package:
name:
- "{{ package_php }}"
- "{{ package_apache }}"
state: absent
No Comments