ในการใช้งาน Ansible หลักๆ จะมีอยู่ Command-line ให้ใช้งานอยู่สองแบบ คือ
/usr/bin/ansible
/usr/bin/ansible-playbook
ซึ่งใน Ep. นี้เรามาโฟกัสกันที่ /usr/bin/ansible
กันก่อน ส่วน /usr/bin/ansible-playbook
คืออะไรเดี๋ยวจะมาว่ากันใน Ep. ถัดไป
--- | |
- name: Create Ansible user | |
hosts: all | |
remote_user: root | |
vars: | |
user_name: maprangzth | |
user_keys: https://github.com/maprangzth.keys | |
tasks: | |
- name: Ensure ansible user exists | |
user: |
--- | |
- name: Install Apache Web Server | |
hosts: web-server | |
remote_user: maprangzth | |
tasks: | |
- name: Ensure httpd package is latest | |
yum: | |
name: httpd | |
state: latest | |
notify: Start and enabled httpd service |
--- | |
- name: Install reqiured packages | |
hosts: all | |
remote_user: maprangzth | |
tasks: | |
- name: Ensure EPEL Repo is present | |
yum: | |
name: epel-release | |
state: present | |
become: True |
จาก Ep. ก่อนหน้านี้ที่พูดเกี่ยวกับการใช้งาน Ansible แบบ Ad-Hoc Commands ก็คงพอที่จะทำให้เห็นประโยชน์การนำ Ansible มาใช้งานกันแล้วคร่าวๆ แต่...จะเห็นได้ว่าการเรียกใช้งาน Module บางตัวนั้นจะต้องระบุ Opstions เยอะแยะมากมายวุ่นวายไปหมด ยิ่งไปกว่านั้นสมมุติถ้าเรามี Task ที่ต้องทำหลายๆ Tasks ยกตัวอย่าง เช่น ติดตั้ง Web Server, คอนฟิก Web Server, ติดตั้ง Database Server ทำ Hardening ฯลฯ คำถามคือ...เราต้องมานั่งพิมพ์คำสั่งยาวๆ ทีละ Task งั้นรึ? ดูมันวุ่นวายเนอะ -_-
Ansible จึงมีวิธีจัดการกับเรื่องยุ่งยากซับซ้อนจากที่ต้องรัน Manual แต่ละ Task ก็เกิดวิธีการเขียนเป็น Ansible Script และมีชื่อเรียกอย่างเป็นทางการว่า Playbooks ซึ่งในหัวข้อนี้เราก็จะดูกันว่ามันคืออะไร? และจะมาช่วยให้มันง่ายขึ้นกว่าการใช้งานแบบ Ad-Hoc Commands จริงๆ หรือเปล่า? ชักช้าอยู่ใยไปเริ่มกันเลยดีกว่าครับ
Playbooks คือ **Configuratio
# Kernel sysctl configuration file for Red Hat Linux | |
# | |
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and | |
# sysctl.conf(5) for more details. | |
# Turn on execshield | |
# 0 completely disables ExecShield and Address Space Layout Randomization | |
# 1 enables them ONLY if the application bits for these protections are set to “enable” | |
# 2 enables them by default, except if the application bits are set to “disable” | |
# 3 enables them always, whatever the application bits |
# Rules for transparent proxying | |
iptables -N NO_PROXY -t nat | |
iptables -A NO_PROXY -t nat -d 0.0.0.0/8 -j ACCEPT | |
iptables -A NO_PROXY -t nat -d 10.0.0.0/8 -j ACCEPT | |
iptables -A NO_PROXY -t nat -d 127.0.0.0/8 -j ACCEPT | |
iptables -A NO_PROXY -t nat -d 169.254.0.0/16 -j ACCEPT | |
iptables -A NO_PROXY -t nat -d 172.16.0.0/12 -j ACCEPT | |
iptables -A NO_PROXY -t nat -d 192.168.0.0/16 -j ACCEPT | |
iptables -A NO_PROXY -t nat -d 224.0.0.0/4 -j ACCEPT | |
iptables -A NO_PROXY -t nat -d 240.0.0.0/4 -j ACCEPT |
(still a work-in-progress)
# yum -y install keepalived
# chkconfig keepalived on