2016-10-05 209 views
0

注册任务的结果,当我调试变量之后之后。我得到的价值是双引号,如"1234"。如果我在另一个模块或任务中使用,它将变为[u'1234']使用注册变量ansible去除单引号

我已删除u和用括号替换字符。

如何摆脱单引号是ansible默认的。

Actual output : '1234' 
expected output: 1234 

剧本片断

- uri: 
     url: http://test/ws?Id=4a3d 
     method: GET 
     content_as_json: yes 
     password: admin 
     user: admin 
     validate_certs: no 
     return_content: yes 
     HEADER_Cookie: "{{login.set_cookie}}" 
    register: name 

- debug: msg="{{ name.content | regex_findall('name=\"(\d+)\"') }}" 
    register: test  

    - uri: 
     url: "http://test/ws?name={{test.msg | replace('u','') | replace('[','') | replace(']','')}}" 
     method: GET 
     content_as_json: true 
     password: admin 
     user: admin 
     validate_certs: no 
     return_content: yes 
     HEADER_Cookie: "{{login.set_cookie}}" 

我用int也但返回0

{{test.msg | replace('u','') | replace('[','') | replace(']','') | int}} 

URI模块输出:在

"<listResponse type=\"cust\" count=\"1\"><instance name=\"1234\" id=\"abcd\" customerRefId=\"xyz\" refId1=\"2345\" type=\"org\" enabled=\"true\" phone=\"\" fax=\"\" billingZip=\"\" billingAddress=\"\" billingCity=\"\" billingCountry=\"\" billingState=\"\" vendorId=\"1\" defaultEmail=\"pqr\" defaultContactName=\"wer\"/></listResponse>" 
+0

请添加您的playbook片段。我怀疑还有更好的办法。 –

+0

添加了代码片段 –

+0

为什么在这里使用debug?只需将'regex_findall'提供给下一个任务的url即可。 –

回答

0

删除debug动作,并与下一个任务使用过滤名:

- uri: 
     url: "http://test/ws?name={{ name.content | regex_search('name=\"(\\d+)\"','\\1') | first }}" 
     method: GET 
     content_as_json: true 
     password: admin 
     user: admin 
     validate_certs: no 
     return_content: yes 
     HEADER_Cookie: "{{login.set_cookie}}" 

在这种情况下regex_search('name=\"(\d+)\"','\1')搜索name=\"<number>\"和仅打印数(因为它是第一个匹配的组 - 用于regex_search第二参数) 。

+0

语法使用该解决方案在错误“(\ d +)\”指向第一slash.If我删除外双引号然后我得到相同的“[u'1234' ]”作为名称 –

+0

响应@DeepaliMittal抱歉,校正的代码:逸出斜线和取第一个元素。 –

+0

感谢。它的工作和其如此惊人的我试过这么多东西正则表达式和反斜杠,但无法得到它的工作。 –

0

寄存器输出变量,并使用{{var.stdout}}得到结果。它将1234没有更多的字符。

- shell: echo 1234 
    register: out 

- debug: msg="Output= {{out.stdout}}" 
+0

我正在注册输出。但它是JSON和XML格式的webservices响应。从那里我获取价值并传递给下一个任务 –

+0

'debug:msg =“{{name.content}}''将返回没有任何'u'或''''的数据。你是否试过通过'{ {name.content}}'到下一个任务? – Nasr

+0

我不能直接使用name.context,它有很多其他的数据,以及XML格式的。你看上面我已经添加了什么是他们在内容 –