当条件在ansible中失败时
when condition fails in ansible
我有一个简单的剧本,它根据从 jenkins 输入的 DEPLOY_TO
环境变量将数据加载到不同的实例,该变量的值为 dev
和 test
。通过使用 when
条件
将 {{ lookup('env','DEPLOY_TO') }}
变量与 dev
或 test
进行比较
- name: load loadBase files into target mysql dev
shell: mysql -h {{ lookup('env','db_host_dev') }} -u {{ lookup('env','db_user_dev') }} --password={{ lookup('env','db_root_password_dev') }} {{ lookup('env','db_name') }} < /tmp/{{ item | basename }}
with_items: "{{ data.loadBase }}"
no_log: false
when: "{{ lookup('env','DEPLOY_TO') }} == dev"
- name: load loadBase files into target mysql test
shell: mysql -h {{ lookup('env','db_host_test') }} -u {{ lookup('env','db_user_test') }} --password={{ lookup('env','db_root_password_test') }} {{ lookup('env','db_name') }} < /tmp/{{ item | basename }}
with_items: "{{ data.loadBase }}"
no_log: false
when: "{{ lookup('env','DEPLOY_TO') }} == test"
但是当 运行 到 DEPLOY_TO
开发或测试的管道时,它失败并出现以下错误
18:34:15 [WARNING]: conditional statements should not include jinja2 templating
18:34:15 delimiters such as {{ }} or {% %}. Found: {{ lookup('env','DEPLOY_TO') }} ==
18:34:15 dev
18:34:15 fatal: [10.0.1.78]: FAILED! => {
18:34:15 "msg": "The conditional check '{{ lookup('env','DEPLOY_TO') }} == dev' failed. The error was: error while evaluating conditional ({{ lookup('env','DEPLOY_TO') }} == dev): 'dev' is undefined\n\nThe error appears to be in '/home/deploy/jenkins/workspace/infrastructure/deploy.yml': line 77, column 11, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: load loadBase files into target mysql dev\n ^ here\n"
请问我做错了什么吗?
您有两个问题,都在错误消息中指出。第一个是:
conditional statements should not include jinja2 templating
when
条件的值已经在隐式模板上下文中计算;您不需要(也不应该添加){{...}}
表达式周围(或内部任何地方)的标记。这让你:
when: "lookup('env','DEPLOY_TO') == dev"
这会让您遇到第二个错误:
The error was: error while evaluating conditional ({{ lookup('env','DEPLOY_TO') }} == dev): 'dev' is undefined
您正在将 lookup
的结果与名为 dev
的 变量 进行比较。如果你想将它与文字字符串进行比较,你需要引用它(就像在 lookup
的参数中一样):
when: "lookup('env','DEPLOY_TO') == 'dev'"
我有一个简单的剧本,它根据从 jenkins 输入的 DEPLOY_TO
环境变量将数据加载到不同的实例,该变量的值为 dev
和 test
。通过使用 when
条件
{{ lookup('env','DEPLOY_TO') }}
变量与 dev
或 test
进行比较
- name: load loadBase files into target mysql dev
shell: mysql -h {{ lookup('env','db_host_dev') }} -u {{ lookup('env','db_user_dev') }} --password={{ lookup('env','db_root_password_dev') }} {{ lookup('env','db_name') }} < /tmp/{{ item | basename }}
with_items: "{{ data.loadBase }}"
no_log: false
when: "{{ lookup('env','DEPLOY_TO') }} == dev"
- name: load loadBase files into target mysql test
shell: mysql -h {{ lookup('env','db_host_test') }} -u {{ lookup('env','db_user_test') }} --password={{ lookup('env','db_root_password_test') }} {{ lookup('env','db_name') }} < /tmp/{{ item | basename }}
with_items: "{{ data.loadBase }}"
no_log: false
when: "{{ lookup('env','DEPLOY_TO') }} == test"
但是当 运行 到 DEPLOY_TO
开发或测试的管道时,它失败并出现以下错误
18:34:15 [WARNING]: conditional statements should not include jinja2 templating
18:34:15 delimiters such as {{ }} or {% %}. Found: {{ lookup('env','DEPLOY_TO') }} ==
18:34:15 dev
18:34:15 fatal: [10.0.1.78]: FAILED! => {
18:34:15 "msg": "The conditional check '{{ lookup('env','DEPLOY_TO') }} == dev' failed. The error was: error while evaluating conditional ({{ lookup('env','DEPLOY_TO') }} == dev): 'dev' is undefined\n\nThe error appears to be in '/home/deploy/jenkins/workspace/infrastructure/deploy.yml': line 77, column 11, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: load loadBase files into target mysql dev\n ^ here\n"
请问我做错了什么吗?
您有两个问题,都在错误消息中指出。第一个是:
conditional statements should not include jinja2 templating
when
条件的值已经在隐式模板上下文中计算;您不需要(也不应该添加){{...}}
表达式周围(或内部任何地方)的标记。这让你:
when: "lookup('env','DEPLOY_TO') == dev"
这会让您遇到第二个错误:
The error was: error while evaluating conditional ({{ lookup('env','DEPLOY_TO') }} == dev): 'dev' is undefined
您正在将 lookup
的结果与名为 dev
的 变量 进行比较。如果你想将它与文字字符串进行比较,你需要引用它(就像在 lookup
的参数中一样):
when: "lookup('env','DEPLOY_TO') == 'dev'"