在 Ansible 中,可以使用“async”和“poll”选项来实现在主机上并行执行多个本地任务的能力。以下是示例代码:
name: 多任务并行执行 hosts: localhost gather_facts: no
tasks:
name: 任务1 command: /path/to/command1 arg1 arg2 async: 120 poll: 0
name: 任务2 command: /path/to/command2 arg1 arg2 async: 120 poll: 0
name: 任务3 command: /path/to/command3 arg1 arg2 async: 120 poll: 0
在这个示例中,我们定义了三个不同的本地任务,并使用“async”和“poll”选项来指定异步执行时间和轮询间隔。这将使 Ansible 在主机上并行执行这三个任务。
当然,这只是一个简单的示例。在实际的 Ansible playbook 中,您可能需要更复杂的逻辑来确保正确、安全和可靠地并行执行多个任务。但是,上述示例可以帮助您开始了解 Ansible 中本地任务的并行执行。