I have baremetal servers where I want to set a specific port for the serial console via proxying, and I decided to make the port from the last octet of the ip, but sometimes the servers have two digits in the last octet, and in such a situation I want zero added for them.
Example:
---
- name: Test
hosts: test
gather_facts: no
tasks:
- name: IPV4 address lookup for node
set_fact:
ip: "{{ lookup('community.general.dig', '{{ inventory_hostname }}-ipmi.mydomain.com') }}"
delegate_to: localhost
- name: Set port for serial console
debug:
msg: "Console port will set 48{{ ip.split('.')[3] }}"
Output:
TASK [IPV4 address lookup for node] ************************************************************************************************************************************************************************
ok: [hostA] => {
"ansible_facts": {
"ip": "10.100.100.25"
},
"changed": false
}
ok: [hostB] => {
"ansible_facts": {
"ip": "10.101.100.203"
},
"changed": false
}
TASK [Set port for serial console] *************************************************************************************************************************************************************************
ok: [hostA] => {
"msg": "Console port will set 4825"
}
ok: [hostB] => {
"msg": "Console port will set 48203"
}
The problem is that I also need to set the port after 48K for hostA. Can this be done with Ansible?