2016-12-05 76 views
0

當且僅當自定義變量(來自,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文件的變量的條件示例。

回答

0

好吧,因爲我仍然有我在做它的方式的問題(從Konstantin's answer檢查意見),我已經結束了改變我和另一個做的方式,不是最漂亮的,解決方案。 。這是我所做的(並且我確信有更好的方法可以做到這一點):

我已經在defaults/main.yml中刪除了與此問題相關的任何內容,並將與此相關的任務移至實際的手冊yaml文件。

這意味着現在我有3個主要的手冊yaml文件可供選擇。

下面是其中之一(其他的人是相似的):

nexus-sync.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: '/', 
     become: yes} 
    tasks: 
    - name: Remove all contents of configuration dir 
     become: yes 
     shell: rm -rf {{ nexus_working_dir }}/conf 
     tags: 
     - ansible-nexus 
    - name: Unpack Nexus sync configurations 
     become: yes 
     unarchive: 
     src="{{ nexus_sync_configurations }}" 
     dest="{{ nexus_working_dir }}" 
     creates="{{ nexus_working_dir }}/conf" 
     force=no 
     copy=false 
     owner={{ nexus_os_user }} 
     group={{ nexus_os_group }} 
     mode="0755" 
     recursive: true 
     notify: 
     - 'restart nexus' 
     tags: 
     - unpack 
     - ansible-nexus 

如果有人張貼另一個工作答案那是比這更好的,我會選擇,作爲解決答案,因爲這個工程可行,但不是那裏最好的解決方案。

2

的第一個例子中有使用可變ansible_os_family

- name: "shut down Debian flavored systems" 
    command: /sbin/shutdown -t now 
    when: ansible_os_family == "Debian" 

注意這個報價:

這是很容易在Ansible做的時候子句,它包含原始Jinja2的表達沒有雙曲花括號

所以你只需要刪除它們(以及那些奇怪的| True過濾器)。

when: nexus_sync_select_configuration 
+0

所以'當:nexus_sync_select_configuration'意味着它是真的嗎?我不需要'當:nexus_sync_select_configuration == True'嗎? – Fadi

+0

嗯,所以在兩個方面(你的回答和我的評論)..它現在每次都跳過這些步驟.. – Fadi

+0

'nexus_sync:false',nexus_sync_select_configuration:{{nexus_sync | bool}}''也是如此,所以你的when-condition總是否定的,儘管任務總是跳過。 –

相關問題