Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a couple of dozen Fedora-clients with DHCP-IP-addresses and I need to set them to static IPs. Usually there is (in this case) only one client per VLAN, but there can be more. The idea is to check if a IP-address (in the same range) is pingable and if not I set the clients IP to this address. This would be sufficient for my case but I'm happy to hear better suggestions. Setting a given IP-address works fine, but how can I find the first "free" address and use it later in the playbook? So i guess: How can I register the first "failed" IP-address?

My ping-test-command

- name: "Test if the ip address is already online."
  command: "ping -q -c 1 -W 1 192.168.178.{{ item }}"
  register: res
  loop: "{{ range(1,5) | list }}"
  #failed_when: res.rc == 0

Output:

[user@host ansible]$ ansible-playbook change_network.yml

PLAY [all] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [192.168.178.40]

TASK [change_network : Test if the ip address is already online.] *******************************************************************************************************************************************
changed: [192.168.178.40] => (item=1)
changed: [192.168.178.40] => (item=2)
failed: [192.168.178.40] (item=3) => {"ansible_loop_var": "item", "changed": true, "cmd": ["ping", "-q", "-c", "1", "-W", "1", "192.168.178.3"], "delta": "0:00:01.003836", "end": "2020-12-30 12:04:18.700349", "item": 3, "msg": "non-zero return code", "rc": 1, "start": "2020-12-30 12:04:17.696513", "stderr": "", "stderr_lines": [], "stdout": "PING 192.168.178.3 (192.168.178.3) 56(84) bytes of data.

--- 192.168.178.3 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms", "stdout_lines": ["PING 192.168.178.3 (192.168.178.3) 56(84) bytes of data.", "", "--- 192.168.178.3 ping statistics ---", "1 packets transmitted, 0 received, 100% packet loss, time 0ms"]}
failed: [192.168.178.40] (item=4) => {"ansible_loop_var": "item", "changed": true, "cmd": ["ping", "-q", "-c", "1", "-W", "1", "192.168.178.4"], "delta": "0:00:01.004021", "end": "2020-12-30 12:04:20.741041", "item": 4, "msg": "non-zero return code", "rc": 1, "start": "2020-12-30 12:04:19.737020", "stderr": "", "stderr_lines": [], "stdout": "PING 192.168.178.4 (192.168.178.4) 56(84) bytes of data.

--- 192.168.178.4 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms", "stdout_lines": ["PING 192.168.178.4 (192.168.178.4) 56(84) bytes of data.", "", "--- 192.168.178.4 ping statistics ---", "1 packets transmitted, 0 received, 100% packet loss, time 0ms"]}

So 192.168.178.3 is the first free address and could be used in this example.

The second problem is, that I need to search in the correct range, i.e. replace the hardcoded "192.168.178" with an ansible variable (the first three octets of the DHCP-address stay the same). I tried something like from this question: Ansible increment IP address

- name: Append the last octet to the first 3 octets
  set_fact: new_ip_address="{{ ansible_default_ipv4.address | regex_replace('(^.*.).*$', '\1') }}{{ item }}"
  loop: "{{ range(1,5) | list }}"
- debug: var=new_ip_address

It works in this form, but I couldn't get this to work in my ping-command. Also maybe there is a prettier solution with a jinja-filter instead of the regex?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
659 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...