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