如果自定义变量设置为true,请执行任务

时间:2016-12-05 16:21:18

标签: ansible yaml ansible-playbook

当且仅当自定义变量(来自,main,playbook.yml)设置为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获得帮助,但他们的示例基于在机器上运行命令然后基于该命令执行某些操作。我找不到基于剧本中另一个yml文件的变量的条件示例。

2 个答案:

答案 0 :(得分:2)

第一个示例使用变量ansible_os_family

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

注意这句话:

  

在Ansible中使用when子句很容易,其中包含原始Jinja2表达式没有双花括号

所以你只需要删除它们(以及那些奇怪的| True过滤器)。

when: nexus_sync_select_configuration

答案 1 :(得分:0)

好吧,因为我仍然遇到了问题(检查来自Konstantin's answer的评论),我最终改变了我用另一种方式,而不是最漂亮的解决方案。这就是我所做的(我确信有更好的方法):

我已在defaults/main.yml中删除了与此问题相关的任何内容,并将与此相关的任务移至实际的playbook yaml文件。

这意味着现在我有3个主要的playbook 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

如果有人发布了比这更好的其他工作答案,我会选择那个作为已解决的答案,因为这个有效,但不是那里的最佳解决方案。