【问题标题】:ansible - add 1 to ip address variable using ipmathansible - 使用 ipmath 将 1 添加到 ip 地址变量
【发布时间】:2021-05-25 11:16:21
【问题描述】:

这看起来应该相当简单。我有一个在 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 ip-address


    【解决方案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"
    }
    

    【讨论】:

    • 完美运行。谢谢!
    【解决方案2】:

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

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

    【讨论】:

    • 感谢您发现我缺少双引号 - 我已解决此问题。在上面尝试过,我最终在我的 sources.list 文件中得到“10.10.10.10|ansible.netcommon.ipmath(5)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2020-08-28
    • 2017-10-12
    • 2012-10-19
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多