Ansible。何时仅限精确字符串

时间:2018-03-19 12:43:26

标签: ansible jinja2

启动下一个命令我可以获得所有已安装的模式:

# fusiondirectory-insert-schema -l
core
cosine
nis
inetorgperson
core-fd
core-fd-conf
ldapns
template-fd
openssh-lpk

如果遗漏某些架构,我可以通过命令安装它:

# fusiondirectory-insert-schema -i mail-fd-conf.schema

所以在使用shell模块的Ansible中,我可以将所有已安装的模式转换为变量:

- name: Get info which schemas are enabled
  shell: fusiondirectory-insert-schema -l
  register: fusiondirectory_schemas

然后分析此输出并安装跳过的架构:

- name: Apply mail schemas for Fusiondirectory
  shell: fusiondirectory-insert-schema -i mail-fd-conf.schema
  when: ('mail-fd-conf' not in fusiondirectory_schemas.stdout)
  args:
    chdir: "/etc/ldap/schema/fusiondirectory/"

假设我希望安装几个不同的模式,并且只为fusiondirectory_schemas.stdout使用一个when输出。所以我的问题是如何在when参数regex中使用^mail-fd-conf$(但没有mail-fd-conf-somethingtest-mail-fd-conf),因为我只想要完全重叠。

1 个答案:

答案 0 :(得分:2)

使用match filter

- name: set regex pattern
  set_fact:
    regex_pattern: "^.*mail-fd-conf.*$"

- name: Apply mail schemas for Fusiondirectory
  shell: fusiondirectory-insert-schema -i mail-fd-conf.schema
  when: fusiondirectory_schemas.stdout |match("{{ regex_pattern }}")
  args:
    chdir: "/etc/ldap/schema/fusiondirectory/