美文网首页Ansible运维监控
如何不通过 inventory 文件,直接指定 host 运行

如何不通过 inventory 文件,直接指定 host 运行

作者: hoxis | 来源:发表于2018-06-07 13:00 被阅读18次

使用过 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"
}

参考:

https://stackoverflow.com/questions/17188147/how-to-run-ansible-without-specifying-the-inventory-but-the-host-directly

相关文章

网友评论

    本文标题:如何不通过 inventory 文件,直接指定 host 运行

    本文链接:https://www.haomeiwen.com/subject/mccgsftx.html