Ansible 使用';无法按预期在数组中循环
我正在尝试创建一个新的Ansible playbook来管理一些应用程序服务 我的剧本是:Ansible 使用';无法按预期在数组中循环,ansible,Ansible,我正在尝试创建一个新的Ansible playbook来管理一些应用程序服务 我的剧本是: - hosts: MyHost gather_facts: no vars: Services: [] tasks: - name: "Search for service to manage" find: path: "/data/mgwroot/applications" file_type: direct
- hosts: MyHost
gather_facts: no
vars:
Services: []
tasks:
- name: "Search for service to manage"
find:
path: "/data/mgwroot/applications"
file_type: directory
patterns: '*{{ ELD }}*'
register: FoundServices
- name: "Add found services to array"
no_log: true
set_fact:
Services: "{{ Services + [item.path | basename] }}"
with_items: "{{ FoundServices.files }}"
- name: "Display array content"
debug:
msg: "{{ Services }}"
- name: "- TASK A -"
shell: "echo {{ item.0 }} {{ item.1 }}"
with_nested:
- [ 'stop', 'start' ]
- [ "{{ Services }}" ]
- name: "- TASK B -"
shell: "echo {{ item.0 }} and {{ item.1 }}"
with_nested:
- ['stop', 'start']
- ['serviceA', 'serviceB']
PLAY [MyHost] *************************************************************************************************************************************************************************
TASK [Search for service to manage] ******************************************************************************************************************************************************
Thursday 19 November 2020 13:25:46 +0100 (0:00:00.109) 0:00:00.109 *****
ok: [MyHost]
TASK [Add found service to array] ********************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:01.260) 0:00:01.370 *****
ok: [MyHost] => (item=None)
ok: [MyHost] => (item=None)
ok: [MyHost]
TASK [Display array content] *************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:00.058) 0:00:01.429 *****
ok: [MyHost] => {
"msg": [
"PREPRO_MVZV14_batch",
"PREPRO_MVZV14_tp"
]
}
TASK [- TASK A -] ************************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:00.038) 0:00:01.467 *****
changed: [MyHost] => (item=[u'stop', u'PREPRO_MVZV14_batch', u'PREPRO_MVZV14_tp'])
changed: [MyHost] => (item=[u'start', u'PREPRO_MVZV14_batch', u'PREPRO_MVZV14_tp'])
TASK [- TASK B -] ************************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:49 +0100 (0:00:01.116) 0:00:02.583 *****
changed: [MyHost] => (item=[u'stop', u'serviceA'])
changed: [MyHost] => (item=[u'stop', u'serviceB'])
changed: [MyHost] => (item=[u'start', u'serviceA'])
changed: [MyHost] => (item=[u'start', u'serviceB'])
- 查找要管理的服务
- 将这些服务添加到阵列中
- 循环此阵列以停止和启动我的不同服务
- hosts: MyHost
gather_facts: no
vars:
Services: []
tasks:
- name: "Search for service to manage"
find:
path: "/data/mgwroot/applications"
file_type: directory
patterns: '*{{ ELD }}*'
register: FoundServices
- name: "Add found services to array"
no_log: true
set_fact:
Services: "{{ Services + [item.path | basename] }}"
with_items: "{{ FoundServices.files }}"
- name: "Display array content"
debug:
msg: "{{ Services }}"
- name: "- TASK A -"
shell: "echo {{ item.0 }} {{ item.1 }}"
with_nested:
- [ 'stop', 'start' ]
- [ "{{ Services }}" ]
- name: "- TASK B -"
shell: "echo {{ item.0 }} and {{ item.1 }}"
with_nested:
- ['stop', 'start']
- ['serviceA', 'serviceB']
PLAY [MyHost] *************************************************************************************************************************************************************************
TASK [Search for service to manage] ******************************************************************************************************************************************************
Thursday 19 November 2020 13:25:46 +0100 (0:00:00.109) 0:00:00.109 *****
ok: [MyHost]
TASK [Add found service to array] ********************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:01.260) 0:00:01.370 *****
ok: [MyHost] => (item=None)
ok: [MyHost] => (item=None)
ok: [MyHost]
TASK [Display array content] *************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:00.058) 0:00:01.429 *****
ok: [MyHost] => {
"msg": [
"PREPRO_MVZV14_batch",
"PREPRO_MVZV14_tp"
]
}
TASK [- TASK A -] ************************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:00.038) 0:00:01.467 *****
changed: [MyHost] => (item=[u'stop', u'PREPRO_MVZV14_batch', u'PREPRO_MVZV14_tp'])
changed: [MyHost] => (item=[u'start', u'PREPRO_MVZV14_batch', u'PREPRO_MVZV14_tp'])
TASK [- TASK B -] ************************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:49 +0100 (0:00:01.116) 0:00:02.583 *****
changed: [MyHost] => (item=[u'stop', u'serviceA'])
changed: [MyHost] => (item=[u'stop', u'serviceB'])
changed: [MyHost] => (item=[u'start', u'serviceA'])
changed: [MyHost] => (item=[u'start', u'serviceB'])
结果并不像预期的那样工作:-(:
- “-TASK B-”,静态列表中的哪个循环工作正常,但
- “-TASK A-”,在我的dynamics数组上的循环无效
- hosts: MyHost
gather_facts: no
vars:
Services: []
tasks:
- name: "Search for service to manage"
find:
path: "/data/mgwroot/applications"
file_type: directory
patterns: '*{{ ELD }}*'
register: FoundServices
- name: "Add found services to array"
no_log: true
set_fact:
Services: "{{ Services + [item.path | basename] }}"
with_items: "{{ FoundServices.files }}"
- name: "Display array content"
debug:
msg: "{{ Services }}"
- name: "- TASK A -"
shell: "echo {{ item.0 }} {{ item.1 }}"
with_nested:
- [ 'stop', 'start' ]
- [ "{{ Services }}" ]
- name: "- TASK B -"
shell: "echo {{ item.0 }} and {{ item.1 }}"
with_nested:
- ['stop', 'start']
- ['serviceA', 'serviceB']
PLAY [MyHost] *************************************************************************************************************************************************************************
TASK [Search for service to manage] ******************************************************************************************************************************************************
Thursday 19 November 2020 13:25:46 +0100 (0:00:00.109) 0:00:00.109 *****
ok: [MyHost]
TASK [Add found service to array] ********************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:01.260) 0:00:01.370 *****
ok: [MyHost] => (item=None)
ok: [MyHost] => (item=None)
ok: [MyHost]
TASK [Display array content] *************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:00.058) 0:00:01.429 *****
ok: [MyHost] => {
"msg": [
"PREPRO_MVZV14_batch",
"PREPRO_MVZV14_tp"
]
}
TASK [- TASK A -] ************************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:00.038) 0:00:01.467 *****
changed: [MyHost] => (item=[u'stop', u'PREPRO_MVZV14_batch', u'PREPRO_MVZV14_tp'])
changed: [MyHost] => (item=[u'start', u'PREPRO_MVZV14_batch', u'PREPRO_MVZV14_tp'])
TASK [- TASK B -] ************************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:49 +0100 (0:00:01.116) 0:00:02.583 *****
changed: [MyHost] => (item=[u'stop', u'serviceA'])
changed: [MyHost] => (item=[u'stop', u'serviceB'])
changed: [MyHost] => (item=[u'start', u'serviceA'])
changed: [MyHost] => (item=[u'start', u'serviceB'])
预期结果:
- hosts: MyHost
gather_facts: no
vars:
Services: []
tasks:
- name: "Search for service to manage"
find:
path: "/data/mgwroot/applications"
file_type: directory
patterns: '*{{ ELD }}*'
register: FoundServices
- name: "Add found services to array"
no_log: true
set_fact:
Services: "{{ Services + [item.path | basename] }}"
with_items: "{{ FoundServices.files }}"
- name: "Display array content"
debug:
msg: "{{ Services }}"
- name: "- TASK A -"
shell: "echo {{ item.0 }} {{ item.1 }}"
with_nested:
- [ 'stop', 'start' ]
- [ "{{ Services }}" ]
- name: "- TASK B -"
shell: "echo {{ item.0 }} and {{ item.1 }}"
with_nested:
- ['stop', 'start']
- ['serviceA', 'serviceB']
PLAY [MyHost] *************************************************************************************************************************************************************************
TASK [Search for service to manage] ******************************************************************************************************************************************************
Thursday 19 November 2020 13:25:46 +0100 (0:00:00.109) 0:00:00.109 *****
ok: [MyHost]
TASK [Add found service to array] ********************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:01.260) 0:00:01.370 *****
ok: [MyHost] => (item=None)
ok: [MyHost] => (item=None)
ok: [MyHost]
TASK [Display array content] *************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:00.058) 0:00:01.429 *****
ok: [MyHost] => {
"msg": [
"PREPRO_MVZV14_batch",
"PREPRO_MVZV14_tp"
]
}
TASK [- TASK A -] ************************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:48 +0100 (0:00:00.038) 0:00:01.467 *****
changed: [MyHost] => (item=[u'stop', u'PREPRO_MVZV14_batch', u'PREPRO_MVZV14_tp'])
changed: [MyHost] => (item=[u'start', u'PREPRO_MVZV14_batch', u'PREPRO_MVZV14_tp'])
TASK [- TASK B -] ************************************************************************************************************************************************************************
Thursday 19 November 2020 13:25:49 +0100 (0:00:01.116) 0:00:02.583 *****
changed: [MyHost] => (item=[u'stop', u'serviceA'])
changed: [MyHost] => (item=[u'stop', u'serviceB'])
changed: [MyHost] => (item=[u'start', u'serviceA'])
changed: [MyHost] => (item=[u'start', u'serviceB'])
- 停止预处理MVZV14\U批次
- 开始预处理MVZV14\U批次
- 停止预处理MVZV14\U tp
- 启动预处理MVZV14\U tp