sks
sks

Reputation: 146

Delay in invocation of ansible task on multiple hosts though `async` being used

I am trying to invoke a REST API on multiple hosts from ansible playbook and don't want this to be blocking operation, so using async and poll.

  - name: Perform operation on Multiple Nodes
    uri:
       url: "http://{{item.ipAddress}}:{{item.port}}/api/internal/operations"
       method: POST
       body: "{{jobData}}"
       headers:
         Accept: "******/*******.{{resource}}.{{resourceVersion}}+json"
       status_code: 200
       body_format: json
    when: (item.ipAddress is defined) and (item.port is defined)
    with_items: '{{nodeData}}'
    async: '{{connectionTimeout | int}}'
    poll: 1
    register: instances
    ignore_errors: True

I am assuming that invocation of the task should be done without any huge amount of delay. But from the log I see there is a delay of ~2-3 sec between each invocation

2018-05-28 *11:26:51* ,530 p=7744 u=ops |  TASK [Perform operation on Multiple Nodes] *************************************
2018-05-28 *11:26:53* ,871 p=7744 u=ops |  ok: [localhost] => (item={u'a': u'host0', u'ipAddress':', u'port': u''})
2018-05-28 *11:26:56* ,095 p=7744 u=ops |  ok: [localhost] => (item={u'a': u'host1', u'ipAddress': u'', u'': u''})
2018-05-28 *11:26:58* ,324 p=7744 u=ops |  ok: [localhost] => (item={u'a': u'host2', u'ipAddress': u'', u'port': u''})

How can I reduce the delay in invocation?

Upvotes: 0

Views: 1422

Answers (1)

Ignacio Millán
Ignacio Millán

Reputation: 8056

First of all, async is used to allow Ansible to continue with other tasks without waiting current task to finish. It doesn't mean that ansible will execute this task on all hosts at the same time, only affects following tasks.

For this, you can use the --forks option when running your playbook. This option set the amount of parallel connections (or processes) that ansible is allowed to set. With --forks=3, you tell ansible to execute 3 hosts processes in parallel.

Anyway the default value for forks is 5 so unless you changed it, that shouldn't be the problem.

If you need more speed, check that ssh pipelining and multiplexing is enabled. Ensure this lines in your ansible configuration file:

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
pipelining = true

Docs about this options here:

Upvotes: 0

Related Questions