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

发布于 2022-09-11 18:58:01 字数 2149 浏览 15 评论 0

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文