使用额外变量在特定主机上运行剧本

问题描述 投票:0回答:1

我正在尝试改进 Ansible 库存,并且我已经制作了此库存:

test_inventory.yml:

############### Inventaire Test##############
envtest:
    hosts:
        toto:
            java_version: 8
            si: si1
            type: oracle
            ip: xxx
        tata:
            java_version: 8
            si: si2
            type: postgres
            ip: xxx
        tato:
            java_version: 8
            si: si3
            type: jboss
            gnl: coeur_sint
            ip: xx

我写了一个测试剧本(我知道它会失败,但只是为了检查是否调用了正确的主机):

################## Playbook de Test ##################
---
  - name: juste pour tester
    hosts: "{{ hosts }}"
    gather_facts: false
    tasks:
        - debug:
            msg: "test"

然后我像这样启动剧本:

ansible-playbook -i test_inventory.yml test.yml --extra-vars "hosts=envtest:*:*:rgcu"

但它在控制台中返回我的所有主机..:

PLAY [juste pour tester] ***************

TASK [debug] *************************
ok: [tata] => {
    "msg": "test"
}
ok: [tato] => {
    "msg": "test"
}
ok: [toto] => {
    "msg": "test"

如何过滤额外变量以使用

si=si2
在主机上进行定位?

filter ansible ansible-inventory
1个回答
0
投票

真正的问题是,为什么您要这样做,而 Ansible 具有专门为此目的的内置功能 - 模式:定位主机和组

这个想法是,你可以在你的剧本中留下

hosts: envtest
甚至
hosts: all
(如果你确定这个剧本不会与其他随机库存一起使用)。如果您需要仅针对某些主机运行剧本,则只需使用标准
--limit
标志:

ansible-playbook test.yml -i test_inventory.yml --limit tata

模式和 ansible-playbook 标志部分涵盖了更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.