Ansible 批量分发与批量部署

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
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小宏运维有点菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值