首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确使用Ansible中的when条件

如何正确使用Ansible中的when条件
EN

Stack Overflow用户
提问于 2021-02-04 17:18:39
回答 1查看 37关注 0票数 0
代码语言:javascript
复制
---
- name: test
  hosts: localhost
  tasks:
  - name: Checking project list
    shell: |
      argocd proj list | grep xcore

    register: argoProj
  - debug: var=argoProj.stdout

  - name: create a project if not exits
    shell: |
      argocd proj create xcore
      # when: argoProj.stdout != "xcore"
    when: argoProj.stdout.find('xcore')!=-1

错误:

代码语言:javascript
复制
''
TASK [create a project if not exits] *****************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "argocd proj create xcore\n     \n# when: argoProj.stdout != \"xcore\"\n", "delta": "0:00:00.807940", "end": "2021-02-04 09:11:24.257123", "msg": "non-zero return code", "rc": 20, "start": "2021-02-04 09:11:23.449183", "stderr": "time=\"2021-02-04T09:11:24Z\" level=fatal msg=\"rpc error: code = InvalidArgument desc = existing project spec is different, use upsert flag to force update\"", "stderr_lines": ["time=\"2021-02-04T09:11:24Z\" level=fatal msg=\"rpc error: code = InvalidArgument desc = existing project spec is different, use upsert flag to force update\""], "stdout": "EE: 0/[]", "stdout_lines": ["EE: 0/[]"]}

注意:当已经创建了xcore项目时,我想跳过最后一个任务。当没有同名的项目时,需要执行最后一个任务。

shell命令的实际输出:

代码语言:javascript
复制
#argocd proj list | grep xcore
xcore                  gke_ec2nf-256816_us-central1_gke-devops,production  <none>                                           <none>                      <none>                        <none>          disabled
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-04 17:33:51

代码语言:javascript
复制
---
- name: test
  hosts: localhost
  tasks:
    - name: Checking project list
      shell: argocd proj list | grep xcore
      register: argoProj

    - debug: var=argoProj.stdout

    - name: create a project if not exits
      shell: argocd proj create xcore
      when: "'xcore' not in argoProj.stdout"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66042624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档