如何在ansible中指定host作为变量?

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

互联网上有一些例子,但似乎都不适合我:

测试.yaml

---

- hosts: "{{ non_default_host }}"
  tasks:
    - debug:
        msg: 'This is the host specified"

然后我用

运行这个
ansible-playbook -i hosts test.yaml -e "non_default_host=<somehost>"

我看到的只是一堆警告:

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided host list is empty, only localhost is available
[WARNING]: Could not match supplied host pattern, ignoring somehost

PLAY [somehost]
skipping: no hosts matched

但是如果我将主机更改为全部

测试.yaml

---

- hosts: all
  tasks:
    - debug:
        msg: 'This is the host specified"

然后使用下面的命令,效果很好

   ansible-playbook -i 'somehost,'  test.yaml
ansible
1个回答
0
投票

对于库存文件

example
,内容为

[example:children]
default
non_default

[default]
node[01:03].example.com

[non_default]
node[01:03].example.net

检查过

ansible-inventory --inventory example --graph
@all:
  |--@ungrouped:
  |--@example:
  |  |--@default:
  |  |  |--node01.example.com
  |  |  |--node02.example.com
  |  |  |--node03.example.com
  |  |--@non_default:
  |  |  |--node01.example.net
  |  |  |--node02.example.net
  |  |  |--node03.example.net

一个最小的示例手册

---
- hosts: "{{ host }}"
  become: false
  gather_facts: false
  connection: local

  tasks:

  - debug:
      msg: "This is the 'hosts: {{ host }}' specified"

通过

调用
 ansible-playbook --inventory example test.yml --extra-vars="host=node03.example.com"

将产生

的输出
PLAY [node03.example.com] **********************************

TASK [debug] ***********************************************
ok: [node03.example.com] =>
  msg: 'This is the ''hosts: node03.example.com'' specified'

文档

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