1. polling and asynchronous operation
By default, the script of the task remains open until the task is completed on each node.
This can cause blocking and timeouts, so we can use asynchronous mode, run all the tasks, then poll until they are completed.
Multithreading to operate a plurality of host nodes Ansible itself is employed, can be used -P asynchronous operation.
Now called asynchronous operation and the polling is for a single task for the.
(1) time-out
If you want to avoid the problems caused connection timeout task fails, you can specify the number of poll to poll,
so that will not last even on the error once did.
--- - the hosts: All Tasks: - name: " default long-running operations, wait up to 45 seconds 5 times poll " Command: / bin / SLEEP 15 the async: 45 poll: 5
async is asynchronous long, if that is not specified synchronization.
poll representative of the number of polls, the default value is 15.
Results of the:
(2) Concurrent
If the poll is 0, the task will start immediately after Ansible go to the next task, so the task will be executed concurrently.
By specifying poll is 0, the task can be run asynchronously.
If you use --forks to perform asynchronous tasks more efficient.
If you want asynchronous tasks and check later, it can be
- name: 'YUM - async task' yum: name: docker-io state: present async: 1000 poll: 0 register: yum_sleeper - name: 'YUM - check on async task' async_status: jid: "{{ yum_sleeper.ansible_job_id }}" register: job_result until: job_result.finished retries: 30
2. Scroll update
You now have 100 nodes, you want to operate in batches, such as one operation 10, you can use the serial parameters to customize.
Now I have two nodes:
Conventional way to perform the task:
serial1.yaml
--- - hosts: all gather_facts: False tasks: - name: task one command: hostname - name: task two command: hostname
Results of the:
Once, perform the two tasks.
Now add serial = 1, in this view the results:
We find that now is performed twice.
Not only can we not specify the number of batch execution, you can also specify the ratio.
Serial : "30%"
may also specify a list:
Serial :
- 1
- 5
- 10
of the first station 1, the second batch execution units 5, the cycle to the.
Of course, the percentage may be used in a plurality of batches listed:
Serial :
- "10%"
- "% 20 is"
- "100%"
mixed definitions are possible:
Serial :
- . 1
- . 5
- "% 20 is"
What is this function with, and now there are 10 tasks, it is common practice for all the machines after the execution of task1, then perform task2,
if there is a problem task2, so these operations are not set aside, and if the first one is executed only, then immediately task2 task detected anomaly.
This can greatly improve efficiency.
3. The maximum failure rate
By default, as long as the host is not a failure exists in the batch, Ansible will continue.
We can specify a failure by max_fail_percentage parameters.
max_fail_percentage : 30
If 10 stations have three problems, the operation is finished.
4. Running time
In some cases, you may only need to run a task on a number of hosts, can be achieved by run_one parameters.
run_once : to true
the task execution instruction task force on the first batch of the current host, then the result will be synchronized to all other hosts in the same batch.
5. Local Script
If the script to be executed on the machine, only:
ansible-PlayBook playbook.yml = - Connection local
or specified in the script:
- the hosts : 127.0.0.1
Connection : local