1、编写 Ansible Playbook 实现批量分发文件和批量部署应用程序的任务。
[root@ubuntu:~]# cat /etc/ansible/hosts
[ubuntu]
10.0.0.219
[ubuntu:vars]
ansible_ssh_password=123456
[root@ubuntu:~]# cat install_nginx.yaml
---
- hosts: ubuntu
gather_facts: no
tasks:
- name: install nginx
apt:
name: nginx
state: present
- name: set page
copy:
src: ./test.html
dest: /var/www/html/index.html
- name: start service
service: name=nginx state=started enabled=yes
2、使用 Ansible Ad-hoc 命令,验证和测试文件分发和应用部署的任务。
[root@ubuntu:~]# ansible ubuntu -m ping
10.0.0.219 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
3、利用 Ansible Playbook 实现文件分发任务,确保文件的安全传输和目标主机的一致性。
[root@ubuntu:~]# ansible-playbook install_nginx.yaml
PLAY [ubuntu] *********************************************************************************************************************************************************
TASK [install nginx] **************************************************************************************************************************************************
ok: [10.0.0.219]
TASK [set page] *******************************************************************************************************************************************************
ok: [10.0.0.219]
PLAY RECAP ************************************************************************************************************************************************************
10.0.0.219 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4、利用 Ansible Playbook 实现应用程序部署任务,包括软件安装、配置文件管理和服务启动等。
[root@ubuntu]# systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2024-04-20 21:20:26 CST; 1s ago
Docs: man:nginx(8)
Process: 4612 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 4613 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 4614 (nginx)
Tasks: 2 (limit: 2177)
Memory: 2.5M
CPU: 23ms
CGroup: /system.slice/nginx.service
├─4614 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
└─4615 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
Apr 20 21:20:26 ubuntu systemd[1]: nginx.service: Deactivated successfully.
Apr 20 21:20:26 ubuntu systemd[1]: Stopped A high performance web server and a reverse proxy server.
Apr 20 21:20:26 ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 20 21:20:26 ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.
5、notify 与 headless 的实现
notify 配合 handlers ,可以实现在特定条件下触发某些操作,特别适用于类似于服务重启
[root@ubuntu:~]# cat install_nginx.yaml
---
- hosts: ubuntu
gather_facts: no
tasks:
- name: install nginx
apt:
name: nginx
state: present
- name: set page
copy:
src: ./test.html
dest: /var/www/html/index.html
notify: restart service #配置文件不修改,目标主机nginx就不会重启
handlers:
- name: restart service
service: name=nginx state=restarted