我尝试使用跨主机的 ansible 角色并行运行任务,并根据要运行的特定任务的主机类型。
但是由于“何时”条件,任务被跳过。有没有办法让它在多个主机上并行工作。
以下是步骤,例如:
inventory.ini
-------------
[dev]
10.0.0.1
10.0.0.2
$ ansible-playbook -i inventory.ini test.yml --limit dev --tags apache,nginx
File: ./test.yml <==
- name: test play
hosts: all
become: True
roles:
- test
File: ./roles/test/tasks/main.yml <==
---
- name: get stats for apache node
stat:
path: "/etc/apache2/apache2.conf"
register: apache_node
- name: get stats for nginx node
stat:
path: "/etc/nginx/nginx.conf"
register: nginx_node
- name: Prepare apache node
include: test_apache.yml
when: apache_node.stat.isdir|d(False)
- name: Prepare nginx node
include: test_nginx.yml
when: nginx_node.stat.isdir|d(False)
File: ./roles/test/tasks/test_apache.yml <==
---
- name: finding apache directory
find:
paths: "/etc/apache2/apache2.conf"
file_type: file
register: file_found
tags:
- never
- apache
Output:
PLAY [test play] *************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************
[WARNING]: error loading fact - please check content
ok: [10.0.0.1]
TASK [finding apache directory] ************************************************************************************************************************************************************************************************************
skipping: [10.0.0.1]
"changed": false,
"skip_reason": "Conditional result was False"
PLAY RECAP *******************************************************************************************************************************************************************************************************************************
10.0.0.1 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
添加标签“始终”解决了问题
- name: get stats for apache node
stat:
path: "/etc/apache2/apache2.conf"
register: apache_node
tags: always
- name: get stats for nginx node
stat:
path: "/etc/nginx/nginx.conf"
register: nginx_node
tags: always