使用过 ansible 的都知道,不管是运行 Ad-Hoc 还是 playbook,都需要指定一个 inventory 文件,或者使用默认的 inventory 文件。但是现在有一个主机,它尚未在 inventory 文件中配置,那么我们该如何用 ansible 对其进行操作呢?
一般我们执行都是这样的:
# ansible 172.16.1.7 -m ping
172.16.1.7 | SUCCESS => {
"changed": false,
"ping": "pong"
}
这时需要在 inventory 文件 /etc/ansible/hosts
中配置 172.16.1.7 信息:
[test]
172.16.1.7
那么,如果没有在 inventory 文件中配置该主机信息,又想用 ansible 对其进行操作呢?
见证奇迹的时刻到了,这个神奇的主角就是 ,
字符,使用方法如下:
# Host and IP address
ansible all -i example.com,
ansible all -i 93.184.216.119,
# Requires 'hosts: all' in your playbook
ansible-playbook -i example.com, playbook.yml
需要注意的是,需要指定范围为
all
# ansible all -i 172.16.1.7, -m ping
172.16.1.7 | SUCCESS => {
"changed": false,
"ping": "pong"
}
参考:
网友评论