如果自定义变量设置为 true,则执行任务
Do task if custom variable is set to true
当且仅当自定义变量(来自 main,playbook.yml)设置为 true 时,我愿意执行任务。我已经尝试了几次,但 none 成功了。
这是我最近的尝试:
在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 文件中的变量的条件示例。
那里的第一个例子使用变量 ansible_os_family
:
- name: "shut down Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_os_family == "Debian"
注意这句话:
This is easy to do in Ansible with the when clause, which contains a raw Jinja2 expression without double curly braces.
所以您只需要删除它们(以及那些奇怪的 | True
过滤器)。
when: nexus_sync_select_configuration
好吧,因为我仍然对我做事的方式有疑问(查看 的评论),我最终改变了我做事的方式,而不是最漂亮的,解决方案.. 这就是我所做的(我相信有更好的方法来做到这一点):
我已经删除了 defaults/main.yml
中与此问题相关的所有内容,并将与此相关的任务移至实际的 playbook 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
如果有人发布了另一个比这个更好的有效答案,我会选择它作为已解决的答案,因为这个有效但不是最好的解决方案。
当且仅当自定义变量(来自 main,playbook.yml)设置为 true 时,我愿意执行任务。我已经尝试了几次,但 none 成功了。
这是我最近的尝试:
在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 文件中的变量的条件示例。
那里的第一个例子使用变量 ansible_os_family
:
- name: "shut down Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_os_family == "Debian"
注意这句话:
This is easy to do in Ansible with the when clause, which contains a raw Jinja2 expression without double curly braces.
所以您只需要删除它们(以及那些奇怪的 | True
过滤器)。
when: nexus_sync_select_configuration
好吧,因为我仍然对我做事的方式有疑问(查看
我已经删除了 defaults/main.yml
中与此问题相关的所有内容,并将与此相关的任务移至实际的 playbook 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
如果有人发布了另一个比这个更好的有效答案,我会选择它作为已解决的答案,因为这个有效但不是最好的解决方案。