如何将正则表达式搜索值添加到Ansible中的变量列表?

时间:2018-12-12 08:35:56

标签: regex ansible

我的要求是读取tnsnames.ora文件并将其架构名称获取到变量列表。还需要将它们添加到模板中。

我能够从tnsnames.ora文件(Matching)中匹配所需的架构名称。但在Ansible中,我不确定如何将所有匹配的单词存储在变量列表中。感谢您的帮助以找到解决方法。

- name: Lookup "{{ ansible_env.TNS_ADMIN }}/tnsnames.ora"
  slurp:
    src: "{{ ansible_env.TNS_ADMIN}}/tnsnames.ora"
  register: contents

- set_fact: matches="{{ contents['content'] | b64decode|regex_search(reg_pattern) |list|join("")}}"

- debug: msg="{{ item }}"
  with_items:
    - "{{ matches }}"

下面是正则表达式模式,它可以帮助我仅获取模式名称。

reg_pattern: '^([^#()\W ][a-zA-Z.]*(?:[.][a-zA-Z]*\s?=)?)((\d*[a-zA-Z]*|(\0*)))'

下面是我的tnsnames.ora文件。

abcdsvsurv = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 30)
                        (DELAY = 5)
                )
        )
)

abcdsvsg = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 20)
                        (DELAY = 2)
                )
        )
)

abcdsvser = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 20)
                        (DELAY = 2)
                )
        )
)

abcdsvmtech = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 20)
                        (DELAY = 2)
                )
        )
)

abcdsvyaan = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 20)
                        (DELAY = 2)
                )
        )
)

abcdsvmrs = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 20)
                        (DELAY = 2)
                )
        )
)

fltsurv03 = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 30)
                        (DELAY = 5)
                )
        )
)

fltsurvsg03 = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 20)
                        (DELAY = 2)
                )
        )
)

fltsurvser03 = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 20)
                        (DELAY = 2)
                )
        )
)

fltsurvmtech03 = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 20)
                        (DELAY = 2)
                )
        )
)

fltsurvyaan03 = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 20)
                        (DELAY = 2)
                )
        )
)

fltsurv03surv = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 30)
                        (DELAY = 5)
                )
        )
)

fltsurvmrs03 = ( DESCRIPTION =
        (FAILOVER = ON)
        (ENABLE=BROKEN)
        (ADDRESS = (PROTOCOL = TCP)(HOST = 172.25.43.165)(PORT = 1521))
        (CONNECT_DATA =
                (SERVER = DEDICATED)
                (SERVICE_NAME = survdb)
                (FAILOVER_MODE =
                        (TYPE = SELECT)
                        (METHOD = BASIC)
                        (RETRIES = 20)
                        (DELAY = 2)
                )
        )
)

1 个答案:

答案 0 :(得分:1)

使用此任务:

- set_fact:
    matches: "{{ contents['content'] | b64decode | regex_findall(reg_pattern, '\\1') }}"

使用该正则表达式:

reg_pattern: '^([^#()\W ][0-9a-zA-Z.]*(?:[.][a-zA-Z]*\s?=)?)'