on
OpenStack
- Get link
- Other Apps
- Opensource
- Automation tool
- Agentless
- Đứng từ 01 server để cấu hình cho hàng trăm server cùng lúc
- Tương tự cho việc cài đặt gói hay thực hiện các lệnh shell bất kỳ
- Backup cấu hình tập trung từ các server về 01 server
- Triển khai các dịch vụ khác nhau lên các server với quy hoạch cho trước bằng 1 câu lệnh duy nhất
- ....nhiều lắm....
- Thay vì SSH bằng tay tới các server cấu hình thì tôi dùng Ansible
- Local machine: nơi cài đặt Ansible và thực hiện câu lệnh ở đó
- Remote machine: nơi cần SSH đến và tác động
Trong loạt bài về Ansible này, mình sẽ sử dụng Ubuntu 18.04, các bạn nếu cài đặt trên môi trường khác ví dụ CentOS thì vui lòng google từ khóa của bước đó + trên CentOS
Chuẩn bị
Note
01 phút cài đặt ansible
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible sshpass
02 phút để chạy ansible đầu tiên
cat << EOF > inventory
[group1]
192.168.2.106 ansible_port=32768 ansible_user=root ansible_password=mypass
EOF
cat << EOF > ls-la.yml
---
- hosts: group1
gather_facts: false
tasks:
- shell: ls -la
register: output
- debug:
msg: "{{ output.stdout_lines }}"
EOF
$ ansible-playbook -i inventory ls-la.yml
[WARNING] Ansible is in a world writable directory (/tmp), ignoring it as an ansible.cfg source.
PLAY [group1] ****************************************************************************************************************************************
TASK [shell] *****************************************************************************************************************************************
changed: [192.168.2.106]
TASK [debug] *****************************************************************************************************************************************
ok: [192.168.2.106] => {
"msg": [
"total 28",
"drwx------ 1 root root 4096 Oct 17 16:40 .",
"drwxr-xr-x 1 root root 4096 Oct 17 16:25 ..",
"drwx------ 3 root root 4096 Oct 17 16:40 .ansible",
"-rw------- 1 root root 7 Oct 17 16:28 .bash_history",
"-rw-r--r-- 1 root root 3106 Feb 20 2014 .bashrc",
"-rw-r--r-- 1 root root 140 Feb 20 2014 .profile",
"drwxr-xr-x 2 root root 4096 May 20 2018 .ssh"
]
}
PLAY RECAP *******************************************************************************************************************************************
192.168.2.106 : ok=2 changed=1 unreachable=0 failed=0
cat << EOF > free-m.yml
---
- hosts: group1
gather_facts: false
tasks:
- shell: free -m
register: output
- debug:
msg: "{{ output.stdout_lines }}"
EOF
$ ansible-playbook -i inventory free-m.yml
[WARNING] Ansible is in a world writable directory (/tmp), ignoring it as an ansible.cfg source.
PLAY [group1] ****************************************************************************************************************************************
TASK [shell] *****************************************************************************************************************************************
changed: [192.168.2.106]
TASK [debug] *****************************************************************************************************************************************
ok: [192.168.2.106] => {
"msg": [
" total used free shared buffers cached",
"Mem: 5822 5242 580 308 234 2644",
"-/+ buffers/cache: 2363 3458",
"Swap: 3960 0 3960"
]
}
PLAY RECAP *******************************************************************************************************************************************
192.168.2.106 : ok=2 changed=1 unreachable=0 failed=0
2 phút để sáng tạo và chạy các ansible playbook của riêng bạn
Bây giờ bạn có thể làm gì tiếp?
cat << EOF > inventory
[group1]
192.168.2.106 ansible_port=32768 ansible_user=root ansible_password=mypass
192.168.2.107 ansible_port=22 ansible_user=root ansible_password=mypass
EOF
Tuyệt cú mèo! Bạn đã chạy thành công ansible!
Trong các bài tiếp theo mình sẽ lần lượt giới thiệu các khái niệm trong Ansible và cách sử dụng chúng ^^
Comments
Post a Comment