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

2019-04-07 00:12:00 +08:00
 fanne

在直接使用 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 中直接使用的。

3662 次点击
所在节点    Ansible
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/552597

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX