我的 ansible 库存文件有以下条目
[non_prod_servers]
oracle[1:13]
如何从上述规范中删除主机“oracle7”和“oracle10”,而无需创建以下条目?
[non_prod_servers]
oracle[1:6]
oracle[8:9]
oracle[11:13]
本质上,寻找比我想出的更优雅的解决方案。
提前致谢。
FR
恕我直言,您正在寻找的功能不可用。您可以在清单中使用 Python 切片。库存模式不适用于库存文件内部。
例如,如果您需要自动化,您想通过几个变量来控制流程,下面的清单和剧本会动态创建组
shell> cat hosts
[non_prod_servers]
localhost
[non_prod_servers:vars]
_name=oracle
_from=1
_to=13
_deny=[7,10]
shell> cat playbook.yml
---
- hosts: non_prod_servers
gather_facts: false
tasks:
- add_host:
hostname: "{{ _name }}{{ item }}"
groups: non_prod_servers_dyn
loop: "{{ range(_from, _to + 1) | difference(_deny) }}"
run_once: true
- hosts: non_prod_servers_dyn
gather_facts: false
tasks:
- debug:
var: ansible_play_hosts_all
run_once: true
给出(删节)
shell> ansible-playbook -i hosts playbook.yml
PLAY [non_prod_servers] *************************************************
TASK [add_host] *********************************************************
ok: [localhost] => (item=1)
ok: [localhost] => (item=2)
ok: [localhost] => (item=3)
ok: [localhost] => (item=4)
ok: [localhost] => (item=5)
ok: [localhost] => (item=6)
ok: [localhost] => (item=8)
ok: [localhost] => (item=9)
ok: [localhost] => (item=11)
ok: [localhost] => (item=12)
ok: [localhost] => (item=13)
PLAY [non_prod_servers_dyn] **********************************************
TASK [debug] *************************************************************
ok: [oracle1] =>
ansible_play_hosts_all:
- oracle1
- oracle2
- oracle3
- oracle4
- oracle5
- oracle6
- oracle8
- oracle9
- oracle11
- oracle12
- oracle13