Ansible-从字​​符串中删除单引号(')字符

时间:2018-09-25 10:20:26

标签: ansible-2.x

我是Ansible的新手,尝试使用Assert模块来验证长度 字符串。结果寄存器包含字符串:"'MWCC' | length == 3"

请帮助您删除字符串中的单引号',以便 结果将是这样的:"MWCC | length == 3”

谢谢。

- set_fact:
    test_code: 'MWCC'

 - name: validate three characters code
   assert:
     that:
       "'{{test_code}}' | length == 3 "
   ignore_errors: True
   register: code_result

 - debug: var=code_result.assertion


 - name: extract the string from assertion test output
   set_fact:
     extract_result: "{{code_result.assertion |regex_replace('\'')}}"


TASK [set_fact] *********************************************************************************
ok: [localhost] => {
    "ansible_facts": {
        "test_code": "MWCC"
    },
    "changed": false
}

TASK [validate three characters code] ****************************************************************************
fatal: [localhost]: FAILED! => {
    "assertion": "'MWCC' | length == 3 ",
    "changed": false,
    "evaluated_to": false
}
...ignoring

TASK [debug] ***************************************************************************
ok: [localhost] => {
    "code_result.assertion": "'MWCC' | length == 3 "
}

TASK [extract the string from assertion test output] **********************************************************************
fatal: [localhost]: FAILED! => {
    "msg": "template error while templating string: unexpected char u\"'\" at 41. String: {{code_result.assertion |regex_replace(''')}}"
}

2 个答案:

答案 0 :(得分:0)

我认为您的断言不应再加上一对引号,

   "{{test_code}} | length == 3 "

可以肯定的是,4个字符串的长度应该为4,不是吗?

答案 1 :(得分:0)

"{{ test_string |replace(\"'\",'') }}"

它具有replace Jinja2过滤器,带有两个参数:

  1. 要替换的单引号,由转义的双引号引起来。
  2. 用两个单引号引起来的空字符串。