Ansible 设置变量的条件可解性
试过所有的运气,但都不起作用。我需要使用条件设置环境变量check,然后在执行include和使用2个任务时在main.yml上运行play或task main.ymlAnsible 设置变量的条件可解性,ansible,Ansible,试过所有的运气,但都不起作用。我需要使用条件设置环境变量check,然后在执行include和使用2个任务时在main.yml上运行play或task main.yml - include: createnonprod.yml when: "{{ environment }}" == 'dev' or "{{ environment }}" == 'qa' or " {{ environment }}" == 'preprod' - include: createprod.yml when:
- include: createnonprod.yml
when: "{{ environment }}" == 'dev' or "{{ environment }}" == 'qa' or "
{{ environment }}" == 'preprod'
- include: createprod.yml
when: "{{ environment }}" == 'prod'
在组变量文件“all”上设置环境
环境:
"{{ lookup('env','ENVIRONMENT') }}"
vars:
env_value: "{{ lookup('env', 'ENVIRONMENT') }}"
但这种检查逻辑失败了
(或)
我需要运行这个逻辑,以便它调用带有条件的任务来检查变量
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"}
when子句,其中包含原始Jinja2表达式,不带
双花括号
如果你试着这样做会怎么样 定义一个变量以处理环境的查找:
"{{ lookup('env','ENVIRONMENT') }}"
vars:
env_value: "{{ lookup('env', 'ENVIRONMENT') }}"
然后在以下情况下使用此变量:
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环境变量名被保留的例子