递增IP地址 [英] Ansible increment IP address

查看:112
本文介绍了递增IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过--extra-vars "lan=10.10.10.1"

我现在需要增加此IP地址,以便最后一个八位位组为.2,因此它将等于10.10.1.0.2.

I now need to increment this ip address so that the last octet is .2 so it will equal 10.10.10.2.

这将如何实现呢?

推荐答案

一行:

- set_fact: new_ip="{{ lan | regex_replace('(^.*\.).*$', '\\1') }}{{lan.split('.')[3] | int + 1 }}"

它如何工作?

  tasks:
  - name: Echo the passed IP
    debug: var={{lan}}

  - name: Extract the last octet, increment it and store it
    set_fact: octet={{lan.split('.')[3] | int + 1 }}
  - debug: var=octet

  - name: Append the incremented octet to the first 3 octets
    set_fact: new_ip="{{ lan | regex_replace('(^.*\.).*$', '\\1') }}{{octet}}"
  - debug: var=new_ip

输出

TASK: [Echo the passed IP] ****************************************************
ok: [127.0.0.1] => {
    "127.0.0.1": "{{ 127.0.0.1 }}"
}
TASK: [Extract the last octet, increment it and store it] *********************
ok: [127.0.0.1] => {"ansible_facts": {"octet": "2"}}

TASK: [debug var=octet] *******************************************************
ok: [127.0.0.1] => {
    "octet": "2"
}
TASK: [Append the incremented octet to the first 3 octets] ********************
ok: [127.0.0.1] => {"ansible_facts": {"new_ip": "127.0.0.2"}}

TASK: [debug var=new_ip] ******************************************************
ok: [127.0.0.1] => {
    "new_ip": "127.0.0.2"
}

这篇关于递增IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆