仅当主机属于清单文件中的特定组时,才有条件在剧本中运行特定任务

时间:2019-11-18 21:24:50

标签: ansible ansible-2.x ansible-inventory ansible-template

我创建了包含6个不同任务的剧本,并且仅当主持人是was855node组(例如bc)的孩子时,才希望运行任务1,2、3 4,5仅在主机是was855dmgr组(例如a)的子级时运行

我的库存文件:

[local-linux:children]
was855

[was855:children]
was855dmgr
was855node

[was855dmgr:children]
hppidc-pps-dmgr
hppndc-pps-dmgr
hppb-pps-dmgr

**hreg1-mnp-dmgr**

[was855node:children]
wppdev3-pps-node
hint1-pps-node

**hreg1-mnp-node**

[hreg1-mnp-dmgr]
a 

[hreg1-mnp-node]
b 
c 

这是正确的方法吗?

- name: Run create-app-logs.py on the Nodes ***(i want to run it on a ONLY)***
  shell: "{{ was_profile_path }}/bin/wsadmin.sh -lang jython -f {{ build_scripts_dir }}/create-app-logs.py"
  register: r
  when: "'was855dmgr' in group_names"
  tags: create_app_logs

- name: Create shared libraries directory ***(i want to run it on b and c ONLY)***
  file: path={{ was_shared_lib_location }} state=directory owner=wasadm group=apps mode=0755
  when: "'was855node' in group_names"

1 个答案:

答案 0 :(得分:0)

通常,您会在同一本剧本中将其拆分为多个单独的剧本,并使用hosts指令来控制每个剧本的执行位置。

例如:

- name: Play 1
  hosts: was855dmgr
  tasks:
    - task1
    - task2
    - task3

- name: Play 2
  hosts: was855node
  tasks:
    - task4
    - task5

- name: Play 2
  hosts: all
  tasks:
    - task6