账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    ansible api 里面的 ad-hoc 和 playbook 怎么做异步
    94
    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 中直接使用的。

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 漫步あ雨中 普通会员 1楼

      在 Ansible API 中,Ad-Hoc(自定义发现)和 Playbook 之间可以通过使用 asyncwith_async 模块来实现异步。

      1. Ad-Hoc: 在 Ansible 的 ad_hoc 模块中,你可以使用 asyncwith_async 来定义一个自定义发现过程。例如:

      ```yaml

      • hosts: localhost gather_facts: False

      vars: ad_hoc_process: "ad-hoc"

      tasks: - name: Run ad-hoc process async ad_hoc_process: with_async: True host: "{{ inventory_hostname }}" username: "{{ inventory_user }}" password: "{{ inventory_password }}" command: "some command" output: path: "{{ ad_hoc_output }}" ```

      在这个例子中,我们定义了一个名为 ad_hoc_process 的任务,该任务使用 async 模块来执行自定义发现过程。在这个过程中,我们使用 with_async: True 来指定这个任务应该使用异步执行。

      1. Playbook: 在 Ansible 的 playbook 模块中,你可以使用 asyncwith_async 来定义一个自定义查找过程。例如:

      ```yaml

      • hosts: localhost gather_facts: False

      vars: playbook_process: "playbook"

      tasks: - name: Run playbook process async playbook_process: with_async: True host: "{{ inventory_hostname }}" username: "{{ inventory_user }}" password: "{{ inventory_password }}" command: "playbook" output: path: "{{ playbook_output }}" ```

      在这个例子中,我们定义了一个名为 playbook_process 的任务,该任务使用 async 模块来执行自定义查找过程。在这个过程中,我们使用 with_async: True 来指定这个任务应该使用异步执行。

      需要注意的是,asyncwith_async 模块只能在 Ansible 2.8 及更高版本中使用。如果你使用的是 Ansible 2.7 或更低版本,你需要使用 sync 模块来实现异步。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部