【问题标题】:Split MAC Address and increment using Ansible使用 Ansible 拆分 MAC 地址并增加
【发布时间】:2021-06-07 10:38:19
【问题描述】:

我正在尝试获取一个 mac 地址并将其中一个值增加 1,例如46:7f:d3:17:06:0b 到 46:7f:d3:17:07:0b

我尝试了几个选项来尝试将变量拆分为 :,然后尝试将值增加 1,但我仍然遇到问题。

有没有人做过这样或类似的事情,你拆分一个字符串,加 1 然后重建字符串

更新:

这是我尝试的,它将 06 增加到 7,因为我将其转换为整数,所以我需要在 7 之前添加 0。

我只是不确定是否有更清洁、更简单的方法来完成此操作。

- set_fact:
    last_mac: "{{ vm.ovirt_nics | json_query(jmesquery)| last}}"
  vars:
    jmesquery: "[*].mac.address"

- debug:
    msg: "{{ last_mac }}"

- set_fact:
   value_to_increment: "{{ last_mac.split(':')[4] }}"

- debug:
    msg: "{{ value_to_increment }}"

- set_fact:
   incremented_value: "{{ value_to_increment | int + 1 }}"

- debug:
    msg: "{{ incremented_value }}"

【问题讨论】:

  • I have tried a couple of options to try and split the variable on : and then try to increment the value by 1 but I am still having issues. => 你写的代码在哪里?你有什么问题?
  • @Zeitounator 我刚刚更新了帖子,我在提出问题时忘记包含它。

标签: math split ansible mac-address


【解决方案1】:

例如

    - set_fact:
        incremented_value: "{{ new|join(':') }}"
      vars:
        arr: "{{ last_mac.split(':') }}"
        ar4: "{{ '%02x' % (arr.4|int(base=16) + 1) }}"
        new: "{{ arr[:4] + [ar4] + arr[5:] }}"

给予

  incremented_value: 46:7f:d3:17:07:0b

【讨论】:

  • 谢谢,太好了,一个问题,这是做什么的? ar4: "{{ '%02x' % (arr.4|int(base=16) + 1) }}"
  • 不客气。见format。
猜你喜欢
  • 2019-11-03
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-22
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多