ansible - 使用 ipmath 将 1 添加到 ip 地址变量

问题描述

这看起来应该相当简单。我有一个在 default.yml 中定义的变量,我想加一:

start_ip: 10.10.10.10

然后我设置事实并报告它:

- set_fact:
    repo_ip:  "{{ start_ip|ipmath(1) }}"

- debug:
   msg: "repo_ip is {{ repo_ip }}"

我也试过:

 - set_fact:
       repo_ip:  "{{ start_ip }}|ipmath(1)"

结果相同:

repo_ip is 10.10.10.10|ipmath(1)

当然我想要的是 10.10.10.11。我做错了什么?

解决方法

尝试使用 ansible.netcommon.ipmath(5) 而不是 ipmath(1)

此外,您在所有示例中都缺少第二组双引号

,

实际上,它应该像您在第一个示例中所写的那样工作。显然,第二个是错误的,因为过滤器仅在花括号内起作用。

    - hosts: localhost
  gather_facts: false
  connection: local
  vars:
    start_ip: 10.10.10.10

  tasks:
    - set_fact:
        next_ip: "{{ start_ip|ipmath(1) }}"
    - debug:
        msg: "Next ip of {{ start_ip }} is: {{ next_ip }}"

这将返回您想要的内容。

PLAY [localhost] ***************************************************************

TASK [set_fact] ****************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "Next ip of 10.10.10.10 is: 10.10.10.11"
}