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)
      .......
  • 相关阅读:
    信息安全
    软件体系结构原理、方法与实践总结
    软件项目管理四个核心价值观
    博客园主题修改
    测试
    Java实现人民币大写精讲
    Windows系统性能提升方法
    Oracle系列之游标
    Oracle系列之异常处理
    Oracle系列之权限
  • 原文地址:https://www.cnblogs.com/cherylgi/p/13367109.html
Copyright © 2011-2022 走看看