gpt4 book ai didi

ansible - 如果位数少于三,则加零

转载 作者:行者123 更新时间:2023-12-04 15:09:53 24 4
gpt4 key购买 nike

我有裸机服务器,我想通过代理为串行控制台设置一个特定端口,我决定从 ip 的最后一个八位位组创建端口,但有时服务器在最后一个八位位组中有两位数字,并且在这种情况下,我希望为他们添加零。

例子:

---
- 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] }}"

输出:

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"
}

问题是我还需要为hostA设置48K之后的端口。这可以用 Ansible 完成吗?

最佳答案

使用format .例如,剧本

- hosts: localhost
vars:
ip:
- 10.100.100.2
- 10.100.100.25
- 10.101.100.203
prefix: 48
tasks:
- debug:
msg: "{{ item }}:{{ prefix }}{{ console_port }}"
loop: "{{ ip }}"
vars:
console_port: "{{ '%03d'|format(item.split('.')|last|int) }}"

给出(删节)

  msg: 10.100.100.2:48002
msg: 10.100.100.25:48025
msg: 10.101.100.203:48203

关于ansible - 如果位数少于三,则加零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65409853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com