1

目前正在使用 Ansible 将旧系统的配置移植到多个IBM ISAM9 个系统ISAM-Ansible-Roleshttps://github.com/IBM-Security/isam-ansible-roles)并且遇到特定字符串的问题。

我需要的结果是这样的:

request-log-format = { "seconds": "%{%s}t" }

我得到的.yml文件如下:

---
- name: Run some commands
  hosts: all
  connection: local
  vars:
    username: "admin"
    password: "password1234"
    lmi_port: "443"
    log_level: "CRITICAL"
    force: True
    start_config_wait_time: 120
  roles:
    - role: update_reverseproxy_conf
      update_reverseproxy_conf_reverseproxy_id: "TestInstance"
      update_reverseproxy_conf_entries:
      - stanza_id: "logging"
        entry_id: "request-log-format"
        value_id: " { \"seconds\": \"%{%s}t\" } "

最后一行value_id:是我遇到问题的地方。尝试了各种使用单引号/双引号和不同转义字符的方法,("\" and "%")但都失败了。下面列出了其中一些。

1) " { \"seconds\": \"%{%s}t\" } "  error: Encountered unknown tag 's'
2) ' { \"seconds\": \"%{%s}t\" } '  error: Encountered unknown tag 's'
3) ' { \"seconds\": \"%{\%s}t\" } ' no error but with incorrect result extra back slash before %s)  request-log-format = { "seconds": "%{\%s}t" }
4) ' { \"seconds\": \"\%{%s}t\" } ' error: Encountered unknown tag 's'
5) " { \"seconds\": \"%{%%s}t\" } "  error: tag name expected
6) " { \"seconds\": \"%%{%s}t\" } "  error: tag name expected

在网上做了一些搜索,但找不到任何解决方案。请帮助,欢迎任何建议或想法。谢谢!

[2018.01.05更新]

我使用 docker 来运行 ansible。该文件保存为“testOneLine.yml”,运行命令为:

sudo docker run -it --rm -v /home/user1/Documents/myplatform:/ansible/playbooks --name test mludocker/isam-ansible -i hosts testOneLine.yml

还按照建议在下面尝试了更多,但都失败了:

7) '{% raw %}{ "seconds": "%{%s}t" }{% endraw %}'  error: Encountered unknown tag 's'
8) !unsafe ' { "seconds": "%{%s}t" } '  error: SyntaxError: invalid syntax MODULE FAILURE

尝试Rob H的建议并重写如下,最后加上额外的 regex_replace ,但它也失败了。

---
- name: Run some commands
  hosts: all
  connection: local
  vars:
    username: "admin"
    password: "password1234"
    lmi_port: "443"
    log_level: "CRITICAL"
    force: True
    start_config_wait_time: 120
    starter_value: "{ \"seconds\": \"${$s}t\" }"        
  roles:
    - role: update_reverseproxy_conf
      update_reverseproxy_conf_reverseproxy_id: "TestInstance"
      update_reverseproxy_conf_entries:
      - stanza_id: "logging"
        entry_id: "request-log-format"
        value_id: "{{ starter_value | regex_replace('\\$','%') }}"

可以看到“regex_replace”部分正在工作,因为最终的“value_id”字符串将“$”正确更改为“%”。下面显示的错误消息与之前相同(遇到未知标签's')。

FAILED! => {
"failed": true, 
"msg": "{u'entry_id': u'request-log-format', u'stanza_id': u'logging', u'value_id': u' { \"seconds\": \"%{%s}t\" } '}: template error while templating string: Encountered unknown tag 's'.. String:  { \"seconds\": \"%{%s}t\" } "
}

我还提出了一个关于 IBM 安全的新问题 #53

4

2 回答 2

1

IBM 提供了一个解决方案/解决方法(请参阅已关闭的问题 #53

测试并确认我是否将最后一行更改为如下,(1) 将 "%" 替换为 "\x25" 和 (2) 用单引号将整个字符串括起来(如果使用双引号则不起作用),

value_id: '{ \"seconds\": \"\x25{\x25s}t\" }'

然后它用预期的结果正确地更新了“request-log-format”键:

request-log-format = { "seconds": "%{%s}t" }

谢谢

于 2018-01-09T00:45:20.280 回答
0

这是一个令人惊讶的令人沮丧的问题,我不确定这是否会有所帮助,但我能够获得与您所需格式相匹配的字符串集

request-log-format = { "seconds": "%{%s}t" }

尚不清楚您正在运行的 isam 角色是否能够使用它。如果角色内部最终使用任何 jinja 模板,我怀疑这只会将问题转移到代码的不同部分,但我想我至少可以分享我发现的内容,看看它是否对你有帮助:

---
- name: handle problems with %
  connection: local
  hosts: localhost

  tasks:
    - name: set a starter value that passes validation
      set_fact:
        starter_value: "request-log-format = { \"seconds\": \"%{$s}t\" }"

    #
    # now set value_id to the final correct value
    # running ansible-playbook with verbose on you can see
    # that the var is set correctly but see comments below
    #
    - name: set value_id (changing $ to %)
      set_fact:
        value_id: "{{ starter_value | regex_replace('\\$','%') }}"

    #
    # to reference the field in ansible must use the start_value and substitute
    # dumping the created file shows the desired values
    #
    - copy: content="{{ starter_value | regex_replace('\$','%') }}" dest=/tmp/value_id

    #
    # using the already converted value will fail
    #
    #- copy: content="{{ value_id }}" dest=/tmp/value_id      
于 2018-01-03T14:21:22.787 回答