5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AnsibleでIPアドレスをインクリメントする

Last updated at Posted at 2018-10-09

Ansibleのフィルターを使ってIPアドレスのインクリメントをしたかったので簡単に備忘録として書いておきます。

1. ipaddr filter

2. 環境

項目 バージョン
Ansible 2.6.5

3. 必要なモジュール

  • python-netaddr

4. IPアドレスのインクリメント

4-1. モジュールをインストール

必要なモジュールをインストールします。

[root@66091d5a51be ~]# yum -y install python-netaddr

4-2. インクリメント例

以下は 192.168.0.1 のIPアドレスに +1 して 192.168.0.2 にするPlaybookです。

example.yml
---
- name: example
  hosts: localhost
  gather_facts: no
  vars:
    ip: 192.168.0.1
  tasks:
    - debug: msg="{{ (( ip | ipaddr('int')) + 1 ) | ipaddr }}"

実行すると 192.168.0.2 になっています。

[root@66091d5a51be ~]# ansible-playbook example.yml
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [example] ************************************************************************************************************************************************

TASK [debug] **************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "192.168.0.2"
}

PLAY RECAP ****************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

ただし、普通に数値をプラス、マイナスするだけだとブロードキャストやネットワークアドレスも含まれてしまいます。

example.yml
---
- name: example
  hosts: localhost
  gather_facts: no
  vars:
    ip: 192.168.0.254
  tasks:
    - debug: msg="{{ (( ip | ipaddr('int')) + 1 ) | ipaddr }}"

実行します。

[root@66091d5a51be ~]# ansible-playbook example.yml
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [example] ************************************************************************************************************************************************

TASK [debug] **************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "192.168.0.255"
}

PLAY RECAP ****************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

例えば(乱暴な書き方ですが)、/24のブロードキャストを含まない場合は以下のような条件式を書きます。

example.yml
---
- name: example
  hosts: localhost
  gather_facts: no
  vars:
    ip: 192.168.0.254
  tasks:
    - set_fact:
        ip: "{{ (( ip | ipaddr('int')) + 1 ) | ipaddr }}"

    - set_fact:
        ip: "{{ (( ip | ipaddr('int')) + 2 ) | ipaddr }}"
      when: ip.split('.')[3] == "255"

    - debug: msg="{{ ip }}"

実行します。

[root@66091d5a51be ~]# ansible-playbook example.yml
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [example] ************************************************************************************************************************************************

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

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

TASK [debug] **************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "192.168.1.1"
}

PLAY RECAP **************************************************************************************************************************************************** localhost                  : ok=3    changed=0    unreachable=0    failed=0

結果は 192.168.1.1 と3オクテット目の位が上がっていることが確認できます。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?