Difference between forks and serial in ansible
FORKS
The maximum number of simultaneous connections Ansible made on each Task.
Use: When you need to manage how many nodes should get affected simultaneously
Let us see an example.
Suppose we have 4 nodes (nodeA, nodeB, nodeC, nodeD) in inventory, 2 tasks in playbook, forks =5 (by default)
First task process on all 4 nodes (nodeA, nodeB, nodeC, nodeD)simultaneously, then Second task process on all 4 nodes (nodeA, nodeB, nodeC, nodeD) simultaneously. If we assume processing time for each task is 5 seconds.
Time take for 1st task = 5s (nodeA, nodeB, nodeC, nodeD)
Time take for 2nd task = 5s (nodeA, nodeB, nodeC, nodeD)
Total time taken for playbook = 10s
That’s the way Ansible works by default (By default Ansible processes a maximum of 5 nodes simultaneously). It was written inside /etc/ansible/ansible.cfg as forks =5. we are free to change forks value but remember increasing it may put a heavy load on Ansible Control Node so make sure not to overload Control Nodes resources.
SERIAL
Decides the number of nodes process in each task in a single run
Suppose we have 4 nodes (nodeA, nodeB, nodeC, nodeD) in inventory, 2 tasks in playbook, forks =5 and serial = 2.
First task process on 2 nodes (nodeA, nodeB) simultaneously (should process on 4 nodes, but due to serial configuration it processes on 2 nodes only) and then jump into the Second task. Second task process on 2 nodes (nodeA, nodeB) simultaneously. Once both tasks are completed, it again runs the playbook for the rest of the 2 nodes.
Next run of playbook process on 2 nodes (nodeC, nodeD) simultaneously, then Second task process on 2 nodes (nodeC, nodeD) simultaneously. After that playbook run gets completed.
If we assume processing time for each task is 5 seconds
First run, Time taken for 1st task = 5s (nodeA, nodeB)
First run, Time taken for 2nd task = 5s (nodeA, nodeB)
Second run, Time taken for 1st task = 5s (nodeC, nodeD)
Second run, Time taken for 2nd task = 5s (nodeC, nodeD)
Total time taken for playbook = 20s
Let's get another set of nodes to explain the serial scenario.
Suppose you have 3 nodes (nodeA, nodeB, nodeC) in inventory, 2 tasks in playbook, forks =5 and serial = 2.
The first task process on 2 nodes (nodeA, nodeB) simultaneously and then jumps into the Second task. Second task process on 2 nodes (nodeA, nodeB) simultaneously. Once both tasks are completed, it again runs the playbook for the rest of the 2 nodes.
Next run of playbook process on 1 node (node), then Second task process on 1 node (nodeC). After that playbook run get completed
If we assume processing time for each task is 5 seconds;
First run, Time taken for 1st task = 5s (nodeA, nodeB)
First run, Time taken for 2nd task = 5s (nodeA, nodeB)
Second run, Time taken for 1st task = 5s (nodeC)
Second run, Time taken for 2nd task = 5s (nodeC)
Total time taken for playbook = 20s
since execution times for Story 2 and Story, 3 were the same. Think about 2 tasks, 10 nodes with forks =5 & 2 tasks 10 nodes with forks=5 and serial =4.
2 Tasks, 10 nodes with forks =5.
Single run,
1st task on first 5 nodes = 5s (node1, node2, node3, node4, node5)
1st task on second 5 nodes = 5s (node6, node7, node8, node9, node10)
2nd task on first 5 nodes = 5s (node1, node2, node3, node4, node5)
2nd task on second 5 nodes = 5s (node6, node7, node8, node9, node10)
Total time taken for playbook = 20s
2 Tasks, 10 nodes with forks=5 and serial =4
First run, 1st task = 5s (node1, node2, node3, node4)
First run, 2nd task = 5s (node1, node2, node3, node4)
Second run, 1st task = 5s (node5, node6, node7, node8)
Second run, 2nd task = 5s (node5, node6, node7, node8)
Third run, 1st task = 5s (node9, node10)
Third run, 2nd task = 5s (node9, node10)
Recent Comments
No comments
Leave a Comment
We will be happy to hear what you think about this post