首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >根据Ansible中的列表检查变量的“正确方法”

根据Ansible中的列表检查变量的“正确方法”
EN

Stack Overflow用户
提问于 2019-04-05 09:22:11
回答 1查看 78关注 0票数 0

下面的Ansible剧本代码可以工作,但它依赖于egrep的外壳。我想知道做这件事的“正确方法”(使用Ansible模块、Jinja2过滤器等,但没有外部二进制文件),以促进我自己的个人成长。

我尝试了几件事,但找不到一个有效的“原生”解决方案。(我尝试使用ini过滤器的次数越少,越好!)

代码语言:javascript
运行
复制
  - name: "Check for {{exclude_file}}"
    stat:
          path: "{{playbook_dir}}/{{exclude_file}}"
          follow: yes
    register: exclude_file_stat
    delegate_to: localhost

  - name: "Check if {{username}} is in {{exclude_file}} (if exists)"
    shell: "egrep '^[ \t]*{{username}}[ \t]*$' {{playbook_dir}}/{{exclude_file}} || true"
    changed_when: false
    register: exclude_file_egrep
    delegate_to: localhost
    when: exclude_file_stat.stat.exists == true

  - name: "Fail if {{exclude_file}} exists and {{username}} found"
    fail:
          msg: "{{username}} found in {{exclude_file}}"
    when: exclude_file_stat.stat.exists == true and exclude_file_egrep.stdout != ""
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-05 22:31:29

下面这出戏做你要找的东西吗?

代码语言:javascript
运行
复制
- name: "Fail if {{ exclude_file }} exists and {{ username }} found"
  fail:
    msg: "{{ username }} found in {{ exclude_file }}"
  when:
    - exclude_file is exists
    - lookup('file', exclude_file) is search(username)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55532117

复制
相关文章

相似问题

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