當且僅當自定義變量(來自,main,playbook.yml)設置爲true時,我纔會執行任務。我曾嘗試過幾次,但都沒有成功。如果自定義變量設置爲true,則執行任務
這裏是我的最新嘗試:
在tasks/main.yml
:
- name: Unpack Nexus main configurations
unarchive:
src="{{ nexus_configs_download_dir }}/{{ nexus_main_configurations }}"
dest="{{ nexus_installation_dir }}"
creates="{{ nexus_installation_dir }}/nexus-professional-{{ nexus_version }}"
force=no
copy=false
owner={{ nexus_os_user }}
group={{ nexus_os_group }}
mode="0755"
when: "{{ nexus_main_select_configuration | True }}"
tags:
- unpack
- ansible-nexus
- name: Unpack Nexus sync configurations
unarchive:
src="{{ nexus_configs_download_dir }}/{{ nexus_sync_configurations }}"
dest="{{ nexus_installation_dir }}"
creates="{{ nexus_installation_dir }}/nexus-professional-{{ nexus_version }}"
force=no
copy=false
owner={{ nexus_os_user }}
group={{ nexus_os_group }}
mode="0755"
when: "{{ nexus_sync_select_configuration | True }}"
tags:
- unpack
- ansible-nexus
- name: Unpack Nexus proxy configurations
unarchive:
src="{{ nexus_configs_download_dir }}/{{ nexus_proxy_configurations }}"
dest="{{ nexus_installation_dir }}"
creates="{{ nexus_installation_dir }}/nexus-professional-{{ nexus_version }}"
force=no
copy=false
owner={{ nexus_os_user }}
group={{ nexus_os_group }}
mode="0755"
when: "{{ nexus_proxy_select_configuration | True }}"
tags:
- unpack
- ansible-nexus
在defaults/main.yml
:
---
# [REDACTED]
nexus_installation_dir: '/usr/share'
nexus_main: false
nexus_sync: false
nexus_proxy: false
nexus_main_select_configuration: "{{ nexus_main | bool }}"
nexus_sync_select_configuration: "{{ nexus_sync | bool }}"
nexus_proxy_select_configuration: "{{ nexus_proxy | bool }}"
在主playbook.yml
:
---
- hosts: 127.0.0.1
connection: local
roles:
- { role: covs.nexus,
nexus_version: '2.14.1-01',
nexus_installation_dir: '/opt',
nexus_port: 8080,
nexus_webapp_context_path: '/',
nexus_proxy: true,
become: yes}
我得到這個最新嘗試以下錯誤:
TASK [covs.nexus : Unpack Nexus main configurations] ***************************
fatal: [127.0.0.1]: FAILED! => {"failed": true, "msg": "The conditional check '{{ nexus_main_select_configuration | True }}' failed. The error was: template error while templating string: no filter named 'True'. String: {{ nexus_main_select_configuration | True }}\n\nThe error appears to have been in '/home/ubuntu/covs-nexus-ansible/tasks/main.yml': line 276, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Unpack Nexus main configurations\n^here\n"}
我試圖從this page得到幫助,但他們的例子是基於運行,則基於該命令的計算機上的命令做一些事情。我無法找到基於來自playbook中另一個yml文件的變量的條件示例。
所以'當:nexus_sync_select_configuration'意味着它是真的嗎?我不需要'當:nexus_sync_select_configuration == True'嗎? – Fadi
嗯,所以在兩個方面(你的回答和我的評論)..它現在每次都跳過這些步驟.. – Fadi
'nexus_sync:false',nexus_sync_select_configuration:{{nexus_sync | bool}}''也是如此,所以你的when-condition總是否定的,儘管任務總是跳過。 –