首页 > 解决方案 > 来自列表项的 Ansible 子字符串

问题描述

为了简单起见,我有一个白名单目录,一个目录被传入。我需要确保“/tmp/dir1/dir2”的完整目录失败,其中“/local/web/dir1”的目录成功。

这段代码,总是说它很好。不管我通过什么。我错过了什么?

编辑 首先传入 {{ dir }},它都是在 AaaS 下运行的。传入的数据如下所示:

应该成功:

/local/web/test1/dir1

应该失败:

/home/test

ansible-playbook-yml

---
- name: Generate Directory Structure and by list.
  hosts: target_hosts
  vars:
    dir: {{ dir }}
    whitelist_dir:
      - "/local/web"

  tasks:
    - name: Validate Search {{ dir }}
      debug:
        msg: "directory is good!"
      when: item is search(dir)
      with_items:
        - "{{ whitelist_dir }}"

标签: ansible

解决方案


在您的编辑和我最后的评论之后,我看到的唯一真正的问题是您在 where 子句中颠倒了您的参数(尽管它没有解释 IMO 为什么它总是会成功......)。

如果我以正确的顺序检查参数,我会得到您期望的结果。我什至在以下 MCVE 中添加了第二个白名单路径,以确保您的循环正常工作。下面的test.yml剧本

---
- name: Check if directory is in whitelist path
  hosts: localhost
  gather_facts: false

  vars_prompt:
    - name: dir
      prompt: Type in full path you want to check
      private: no

  vars:
    whitelist_dir:
      - "/local/web"
      - "/toto/pipo"

  tasks:
    - name: Validate Search {{ dir }}
      debug:
        msg: "directory is good!"
      when: dir is search(item)
      with_items:
        - "{{ whitelist_dir }}"

给出(3个不同的测试)

$ ansible-playbook test.yml 
Type in full path you want to check: /local/web/test1/dir1

PLAY [Check if directory is in whitelist path] *****************************************************************************************************************************************************************************************

TASK [Validate Search /local/web/test1/dir1] *******************************************************************************************************************************************************************************************
ok: [localhost] => (item=/local/web) => {
    "msg": "directory is good!"
}
skipping: [localhost] => (item=/toto/pipo) 

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

$ ansible-playbook test.yml 
Type in full path you want to check: /home/test

PLAY [Check if directory is in whitelist path] *****************************************************************************************************************************************************************************************

TASK [Validate Search /home/test] ******************************************************************************************************************************************************************************************************
skipping: [localhost] => (item=/local/web) 
skipping: [localhost] => (item=/toto/pipo) 
skipping: [localhost]

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

$ ansible-playbook test.yml 
Type in full path you want to check: /toto/pipo/test.txt

PLAY [Check if directory is in whitelist path] *****************************************************************************************************************************************************************************************

TASK [Validate Search /toto/pipo/test.txt] *********************************************************************************************************************************************************************************************
skipping: [localhost] => (item=/local/web) 
ok: [localhost] => (item=/toto/pipo) => {
    "msg": "directory is good!"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

推荐阅读