zoukankan      html  css  js  c++  java
  • ansibleAPI怎么做异步

    在直接使用 ansible 时候有-B -p 参数可以启用异步操作,然后返回一个 job_id 值

    [root@master ansible]# ansible node1 -B 3600 -P 0  -m yum -a "name=ansible" -vv
    Using /etc/ansible/ansible.cfg as config file
    META: ran handlers
    192.168.77.129 | SUCCESS => {
        "ansible_job_id": "23974611070.37468", 
        "changed": true, 
        "finished": 0, 
        "results_file": "/root/.ansible_async/23974611070.37468", 
        "started": 1
    }
    
    [root@master ansible]# ansible node1 -m async_status -a "jid=23974611070.37468"
    192.168.77.129 | SUCCESS => {
        "ansible_job_id": "23974611070.37468", 
        "changed": false, 
        "finished": 1, 
        "msg": "", 
        "rc": 0, 
        "results": [
            "ansible-2.3.1.0-1.el6.noarch providing ansible is already installed"
        ]
    }

    playbook 也是可以指定参数启用异步的。

    # asynctest.yml
    ---
    
    - hosts: node1
      tasks:
       - shell: sleep 100 && hostname
         async: 100
         poll: 0
         register: result
    
       - debug: var=result
    
       - async_status: jid={{ result.ansible_job_id }}
         register: job_result
         until: job_result.finished
         retries: 30

    那么在 ansible api 里对于 ad-hoc 和 playbook 怎么启用这个异步任务的,不然页面有时候要卡好久在那的。

    def run_model(self, module_name, module_args, task_time=None, poll=None):
    """
    run module from andible ad-hoc.
    module_name: ansible module_name
    module_args: ansible module args
    task_time: 这个任务执行时间的上限值。即任务执行所用时间如果超出这个时间,则认为任务失败。这个需要大于0,等于 0 的时候不支持异步(默认值)。
    poll: 任务异步执行时轮询的时间间隔。如果poll为0,就相当于一个不关心结果的任务。
    """
    self.ips_cov_str()

    if task_time and poll:
    play_source = dict(
    name="Ansible Play",
    hosts=self.ips,
    gather_facts='no',
    tasks=[dict(action=dict(module=module_name, args=module_args), async=task_time, poll=poll)]
    )
    else:
    play_source = dict(
    name="Ansible Play",
    hosts=self.ips,
    gather_facts='no',
    tasks=[dict(action=dict(module=module_name, args=module_args))]
    )
    play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
      .......
  • 相关阅读:
    003 Leaflet 第三个demo 地图上的面积测量
    002 Leaflet 第二个demo 地图上的矩形拉框选择
    001 Leaflet 第一个demo 加载天地图
    This关键字,打印花瓣的数量
    Myeclipse8.5 添加Tomcat7
    WGS84经纬度 与 web 墨卡托相互转化 工具类
    java list集合去重复
    response 下载文件
    jquery实现可拖拽的div
    linux 前端环境搭建
  • 原文地址:https://www.cnblogs.com/cherylgi/p/13367109.html
Copyright © 2011-2022 走看看