Ansible任务委派

491次阅读
没有评论

共计 2913 个字符,预计需要花费 8 分钟才能阅读完成。

Ansible任务委派

ansible的task的执行对象是主机清单中的对象,如对于清单内的主机进行批量任务处理是非常合适的,但是如果对于一些特定操作在某些主机时或该任务执行对象不在清单内时。我们可能又需要创建一个剧本,有点类似roles的味道。如果我们就要在一个剧本中指定不同的执行对象,可以使用delegate_to参数

delegate_to

该参数用于将任务委派到指定机器上运行,例如博主的一个lnmp项目下线场景:

  1. 云平台主控机上运行dns记录下线脚本:清理www.xxx.cn记录
  2. 自建LB节点移除负载项目配置www.xxx.cn.lb-conf
  3. 每台web节点上nginx需要下线项目的配置www.xxx.cn.conf
  4. 每台web节点上备份web数据到备份服务器
  5. 查看备份服务器上的备份数据

可能有的读者会使用roles去区分不同主机执行不同剧本,又或者用when配和inventory_hostname做判断,此处不涉及所以忽略~~~,博主这里限定在一个剧本中执行上述操作。

playbook主机清单为

[root@manager project-backup]# cat hosts
[web]
10.100.235.224
10.100.235.245
10.100.235.199
10.100.235.194
10.100.235.195
10.100.235.202

其中第一步移除dns记录,将任务委派给主机cloud-controller-manager(该主机记录自行在ansible主控机上做好hosts记录),此时剧本如下

---
- hosts: web
  vars: 
    project_name: www.xxx.cn
    project_id: HCxxxxxxx
  tasks:
  - name: clear dns A 
    shell: /usr/local/scripts/dns_clear.py {{ project_name }}
    delegate_to: cloud-controller-manager

执行结果则是主机清单web组每个节点遇到这个任务都会委派给cloud-controller-manager这台服务器操作。而现实中我们这个任务只需要执行一次就可以,所以还需要加上run_once参数来指定该任务只需要跑一次即可,即不管web组内有多少台节点,只有一个节点会运行该任务

---
- hosts: web
  vars: 
    project_name: www.xxx.cn
    project_id: HCxxxxxxx
  tasks:
  - name: clear dns A 
    shell: /usr/local/scripts/dns_clear.py {{ project_name }}
    delegate_to: cloud-controller-manager
    run_once: true

最终大致任务的剧本为如下

---
- hosts: web
  vars: 
    project_name: www.xxx.cn
    project_id: HCxxxxxxx
  tasks:
  - name: clear dns A 
    script: /usr/local/scripts/dns_clear.py {{ project_name }}
    args: 
      creates: /tmp/dns_controller/dns-clear-{{ project_name }}.txt
    delegate_to: cloud-controller-manager
    run_once: true

  - name: clear lb config
    script: /usr/local/scripts/lb_conf_clear.sh {{ project_name }}
    args: 
      creates: /tmp/lb_controller/lb-clear-{{ project_name }}.txt
    delegate_to: lb-controller-manager
    run_once: true

  - name: clear nginx config
    script: /usr/local/scripts/nginx_conf_clear.sh {{ project_name }}
    args: 
      creates: /tmp/nginx_controller/nginx-clear-{{ project_name }}.txt

  - name: backup web dir
    script: /usr/local/scripts/backup_web_dir.sh {{ project_name + inventory_hostname ---
- hosts: web
  vars: 
    project_name: www.xxx.cn
    project_id: HCxxxxxxx
  tasks:
  - name: clear dns A 
    script: /usr/local/scripts/dns_clear.py {{ project_name }}
    args: 
      creates: /tmp/dns_controller/dns-clear-{{ project_name }}.txt
    delegate_to: cloud-controller-manager
    run_once: true

  - name: clear lb config
    script: /usr/local/scripts/lb_conf_clear.sh {{ project_name }}
    args: 
      creates: /tmp/lb_controller/lb-clear-{{ project_name }}.txt
    delegate_to: lb-controller-manager
    run_once: true

  - name: clear nginx config
    script: /usr/local/scripts/nginx_conf_clear.sh {{ project_name }}
    args: 
      creates: /tmp/nginx_controller/nginx-clear-{{ project_name }}.txt

  - name: backup web dir
    script: /usr/local/scripts/backup_web_dir.sh {{ project_name }}
    args: 
      creates: /tmp/backup_controller/backup-webdir-{{ project_name }}.txt

  - name: backup web dir
    shell: }}
    args: 
      creates: /tmp/backup_controller/backup-webdir-{{ project_name }}.txt

  - name: list project backup
    shell: ls -l /data/backup/project-down/{{ project_name }}
    register: backup_pkg_info
    delegate_to: backup-controller-manager
    run_once: true

  - name: output backup_pkg_info
    debug: 
      msg: "{{ backup_pkg_info }}"
    delegate_to: backup-controller-manager
    run_once: true

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2019-06-02发表,共计2913字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)