尝试所有运气但不起作用。我需要使用条件设置环境变量检查,然后在执行包含和使用2个任务时在main.yml上运行播放或任务。
main.yml
- include: createnonprod.yml
when: "{{ environment }}" == 'dev' or "{{ environment }}" == 'qa' or "
{{ environment }}" == 'preprod'
- include: createprod.yml
when: "{{ environment }}" == 'prod'
环境设置在groups_vars变量文件" all" 环境:
"{{ lookup('env','ENVIRONMENT') }}"
但这种检查逻辑失败
(OR)
我需要运行这个逻辑,以便它调用具有条件的任务来检查变量
create.yml
- name: Install all users from IAM of the AWS Account.
shell: "{{ scriptdir }}/install.sh -i {{ iamgroup }},{{ sudogroup }} -s {{ sudogroup }}"
when: "{{ environment }}" == 'dev' or "{{ environment }}" == 'qa' or "{{ environment }}" == 'preprod'
- name: Install all users from IAM of the AWS Account.
shell: "{{ scriptdir }}/install.sh -i {{ iamgroup }},{{ sudogroup }} -s {{ sudogroup }}"
when: "{{ environment }}" == 'prod'
请帮我一个有效的逻辑。我收到这个错误:
fatal: [localhost]: FAILED! => {"failed": true, "reason": "ERROR!
Syntax Error while loading YAML.\n\n\nThe error appears to have been in
'/tmp/ansible/roles/provision/users/tasks/create.yml': line 18, column
22, but may\nbe elsewhere in the file depending on the exact syntax
problem.\n\nThe offending line appears to be:\n\n when:\n - {{
env_type }} == 'dev'\n ^ here\nWe could be wrong,
but this one looks like it might be an issue with\nmissing quotes.
Always quote template expression brackets when they\nstart a value. For
instance:\n\n with_items:\n - {{ foo }}\n\nShould be written
as:\n\n with_items:\n - \"{{ foo }}\"\n"}
答案 0 :(得分:1)
when子句,其中包含原始Jinja2表达式 双花括号
when: environment == 'dev' or environment == 'qa' or environment == 'preprod'
when: environment == 'prod'
答案 1 :(得分:0)
如果您尝试这种方式怎么办。
定义一个变量来处理您的环境:
vars:
env_value: "{{ lookup('env', 'ENVIRONMENT') }}"
,然后在when条件中使用此变量:
when: env_value == "dev"
简单的例子:
- hosts: "localhost"
vars:
env_value: "{{ lookup('env', 'ENVIRONMENT') }}"
tasks:
- shell: echo "I've got {{ env_value }} and am not afraid to use it!"
when: env_value == "prd"
代码示例:
- name: Install all users from IAM of the AWS Account.
shell: "{{ scriptdir }}/install.sh -i {{ iamgroup }},{{ sudogroup }} -s {{ sudogroup }}"
when: env_value == 'dev' or env_value == 'qa' or env_value == 'preprod'
还请注意有关保留Ansible环境变量名称的link。