V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Ansible
Ansible Documentation
fanne
V2EX  ›  Ansible

ansible api 里面的 ad-hoc 和 playbook 怎么做异步

  •  
  •   fanne · 2019-04-07 00:12:00 +08:00 · 3647 次点击
    这是一个创建于 1840 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在直接使用 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 怎么启用这个异步任务的,不然页面有时候要卡好久在那的。

    # create play with tasks
    play_source =  dict(
        name = "Ansible Play",
        hosts = 'all',   # 这里指定 all
        gather_facts = 'no',
        tasks = [
            dict(action=dict(module='shell', args='ls'), register='shell_out'),
            dict(action=dict(module='debug', args=dict(msg='')))
            ]
      )
    play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
    
    # actually run it
    tqm = None
    try:
        tqm = TaskQueueManager(
                inventory=inventory,
                variable_manager=variable_manager,
                loader=loader,
                options=options,
                passwords=passwords,
                stdout_callback=results_callback,  # Use our custom   callback instead of the ``default`` callback plugin
          )
        result = tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()
    

    之前在 django 中会使用 celery 做一些异步任务的工作。

    现在想了解 ansible 在使用 api 时候,有没自己的一些异步方式的,我能在 django 中直接使用的。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1842 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:24 · PVG 00:24 · LAX 09:24 · JFK 12:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.