如何将 AnsibleUnsafeText HEX 值转换为 int?

How to convert an AnsibleUnsafeText HEX value to int?

我有一个简单的 Ansible 剧本,它从日志文件中读取十六进制值(纪元时间),然后我想使用 strftime 获得可读格式,但我无法让它工作。

剧本示例:

- debug:
    msg:
      - "Time = {{ '%Y-%m-%d %H:%M:%S' | strftime( item ) }}"
  with_lines: cat /tmp/log.file

我发现返回值定义为AnsibleUnsafeText,我尝试将其转换为整数,但仍然无效。

Ansible 版本:2.9

根据您的描述,我了解到您喜欢将 626698A0 之类的字符串转换为秒。让我们假设有 hex.log 带有 Unix HEX 时间戳的文件

626698A0
626698B0
626698C0
626698D0
626698E0
626698F0

和像

这样的测试剧本
---
- hosts: localhost
  become: false
  gather_facts: true

  vars:

    HEX: 0x626698A0
    HEX_STRING: '626698A0'

  tasks:

  - name: Show vars type
    debug:
      msg:
        - "{{ HEX }} is {{ HEX | type_debug }} and HEX {{ '%#X' % HEX }}"
        - "{{ HEX_STRING }} is {{ HEX_STRING | type_debug }}"

  - name: Show values type
    debug:
      msg: "{{ item }} is {{ item | type_debug }}"
    with_lines: cat hex.log

然后它会导致输出

TASK [Show vars type] ******************
ok: [localhost] =>
  msg:
  - 1650890912 is int and HEX 0X626698A0
  - 626698A0 is AnsibleUnicode

TASK [Show values type] *************
ok: [localhost] => (item=626698A0) =>
  msg: 626698A0 is AnsibleUnsafeText
ok: [localhost] => (item=626698B0) =>
  msg: 626698B0 is AnsibleUnsafeText
ok: [localhost] => (item=626698C0) =>
  msg: 626698C0 is AnsibleUnsafeText
ok: [localhost] => (item=626698D0) =>
  msg: 626698D0 is AnsibleUnsafeText
ok: [localhost] => (item=626698E0) =>
  msg: 626698E0 is AnsibleUnsafeText
ok: [localhost] => (item=626698F0) =>
  msg: 626698F0 is AnsibleUnsafeText

从 Ansible v2.10 开始,应该可以使用

"{{ HEX_STRING | int(base=16) }}"

因此在 Ansible v2.9 中会出现错误

TASK [Show values type] ************************************************************************************
fatal: [localhost]: FAILED! =>
  msg: 'Unexpected templating type error occurred ...: do_int() got an unexpected keyword argument ''base'''

更多文档和问答


关于“有没有办法将来自日志文件的值从 AnsibleUnsafeText 转换为 int" 并且作为 Ansible v2.9 中的解决方法,可能 write an own filter plugin similar to other solutions here, in example version_sort. It would just be necessary to use an approach like in Convert HEX string to int in Python 代替。

使用 hex2int.py 过滤器插件进行简短测试后

#!/usr/bin/python

class FilterModule(object):
    def filters(self):
        return {
            'hex2int': self.hex2int,
        }

    def hex2int(self, hexString):
        intValue = int(hexString, 16)
        return intValue

哪个需要放在ansible.cfgfilter_plugins =中指定的路径下,我发现它可以工作

  - name: Show int values and type
    debug:
      msg: "{{ item | hex2int }} is {{ item | hex2int | type_debug }}"
    with_lines: cat hex.log

并产生

的输出
TASK [Show int values and type] *************
ok: [localhost] => (item=626698A0) =>
  msg: 1650890912 is int
ok: [localhost] => (item=626698B0) =>
  msg: 1650890928 is int
ok: [localhost] => (item=626698C0) =>
  msg: 1650890944 is int
ok: [localhost] => (item=626698D0) =>
  msg: 1650890960 is int
ok: [localhost] => (item=626698E0) =>
  msg: 1650890976 is int
ok: [localhost] => (item=626698F0) =>
  msg: 1650890992 is int