【发布时间】:2019-06-24 19:40:36
【问题描述】:
我正在尝试使用 regex_replace 替换字符串,但我遇到了参数问题。
vars:
**mongoURI**: "mongodb://shard1:27017,shard2:27017,shard3:27017/?ssl=true&authSource=admin&replicaSet=TestCluster-shard-0"
- name: Create Mongo connection string
set_fact:
**readinput**: "mongodb://{{ (userinfo_json.Users | first).username }}:{{ (userinfo_json.Users | first).password }}@"
readconnstring: "{{ mongoURI | regex_replace('mongodb://', '{{ readinput }}') | regex_replace('\\?ssl', 'test?ssl') + ('&retryWrites=true') }}"
以上工作正常,如果我用 URI 初始化 mongoURI 变量,我会得到所需的输出。这是输出。
mongodb://testuser:password@shard1:27017,shard1:27017,shard1:27017/?ssl=true&authSource=admin&replicaSet=TestCluster-shard-0
但是我需要从 JSON 输入中读取 mongoURI 值。当我这样做时,第一个 regex_replace 无法识别变量(readinput)。
- name: Fetch Mongo URI
set_fact:
mongoURI: "{{ (cluster_status_check.content | from_json).mongoURIWithOptions }}"
when: cluster_status_check.status == 200
在这种情况下,最后两个 regex_replaces 工作正常,但第一个正被替换为变量名,如下所示。
*{{ readinput }}*shard1:27017,shard1:27017,shard1:27017/?ssl=true&authSource=admin&replicaSet=TestCluster-shard-0
我尝试使用 urlsplit('hostname') 但 'mongodb://' 未被识别为主机名。
我还尝试删除 readinput 变量周围的单引号 readconnstring: "{{ mongoURI | regex_replace('mongodb://', {{ readinput }})) 并删除变量的 {{ }} readconnstring: "{{ mongoURI | regex_replace('mongodb://', readinput))
但两者都给出语法错误。
由于 from_json 将输出转换为字典,我尝试使用 with_items 但没有成功。
- name: Create Mongo connection string
set_fact:
readinput: "mongodb://{{ (userinfo_json.Users | first).username }}:{{ (userinfo_json.Users | first).password }}@"
readconnstring: "{{ item | regex_replace('mongodb://', '{{ readinput }}') | regex_replace('\\?ssl', 'test?ssl') + ('&retryWrites=true') }}"
with_items:
- "{{ mongoURI }}"
---
- hosts: localhost
gather_facts: no
vars:
mongoURI: ""
- name: Check whether cluster already exists
uri:
url: https://cloud.mongodb.com/api/atlas/v1.0/groups/{{ groupid }}/clusters/{{ clustername }}
return_content: yes
method: GET
user: "{{ user }}"
password: "{{ apikey }}"
status_code: 404, 200
register: cluster_status_check
- name: Fetch Mongo URI
set_fact:
mongoURI: "{{ (cluster_status_check.content | from_json).mongoURIWithOptions }}"
when: cluster_status_check.status == 200
- name: Create Mongo connection string
set_fact:
readinput: "mongodb://{{ (userinfo_json.Users | first).username }}:{{ (userinfo_json.Users | first).password }}@"
readconnstring: "{{ mongoURI | regex_replace('mongodb://', '{{ readinput }}') | regex_replace('\\?ssl', 'test?ssl') + ('&retryWrites=true') }}"
不是将 mongodb:// 替换为 readinput 值,而是将其替换为 {{ readinput }}
期望的输出:
mongodb://testuser:password@shard1:27017,shard1:27017,shard1:27017/?ssl=true&authSource=admin&replicaSet=TestCluster-shard-0
实际输出:
{{ readinput }}shard1:27017,shard1:27017,shard1:27017/?ssl=true&authSource=admin&replicaSet=TestCluster-shard-0
【问题讨论】:
-
在没有
{{}}的情况下使用 readinput 时会出现什么语法错误?见this答案 -
致命:[本地主机]:失败! => { "msg": "({{ mongoURI | regex_replace('mongodb://', readinput) }}) 发生意外的模板类型错误:'NoneType' 类型的对象没有 len()" }
-
在执行 regex_replace 之前,您可以尝试打印 mongoURI 的值吗?
-
是的,我打印的是一样的。
- name: Fetch Mongo URI set_fact: mongoURI: "{{ (cluster_status_check.content | from_json).mongoURIWithOptions }}" when: cluster_status_check.status == 200 - debug: msg: "Fetched URI:{{ mongoURI }}"这是价值。出于隐私原因稍作改动。mongodb://testcluster-shard-00-00-azure.mongodb.net:27017,testcluster-shard-00-01-azure.mongodb.net:27017,testcluster-shard-00-02-azure.mongodb.net:27017/?ssl=true&authSource=admin&replicaSet=TestCluster-shard-0 -
我也在 regex_replace 之前打印了,mongoURI 变量的内容与上面给出的相同。