ansible - 如何在特定主机: ansible-playbook -i <hostname>,上运行ansible

标签 ansible

The docs指定我可以使用 -i 在特定主机上运行我的剧本:

Patterns and ansible-playbook flags

You can change the behavior of the patterns defined in playbooks using command-line options. For example, you can run a playbook that defines hosts: all on a single host by specifying -i 127.0.0.2,. This works even if the host you target is not defined in your inventory. You can also limit the hosts you target on a particular run with the --limit flag:*

但是,我尝试运行 ansible-playbook <playbook> -i <new_hostname> -u <username>并且使用的库存仍然是我的默认库存。如何正确使用?

最佳答案

引自“man ansible”

-i, --inventory, --inventory-file specify inventory host path or comma separated host list.

要将单个主机指定为“逗号分隔的主机列表”,仍然需要逗号。例如,剧本

shell> cat playbook.yml
- hosts: all
  gather_facts: false
  tasks:
    - debug:
        var: inventory_hostname

给予

shell> ansible-playbook -i test_99, playbook.yml

PLAY [all] ****

TASK [debug] ****
ok: [test_99] => 
  inventory_hostname: test_99

主机后面没有逗号,Ansible 将参数作为“库存主机路径”。

shell> ansible-playbook -i test_99 playbook.yml
[WARNING]: Unable to parse /scratch/test_99 as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'

PLAY [all] ****
skipping: no hosts matched

关于ansible - 如何在特定主机: ansible-playbook -i <hostname>,上运行ansible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63944419/

相关文章:

ubuntu - 自动化 nginx/passenger 安装

Ansible:通知多个任务 block 的处理程序

variables - Ansible block 变量

仅在需要升级时停止服务的 Ansible 方法

ansible - 我的剧本不起作用。我无法复制文件

ansible - 如何使用 Ansible 获取物理网络接口(interface)列表

ansible - 我可以根据 ansible 详细级别将 ansible no_log 设置为默认值吗?

ansible - 如何在本地目录中建立文件名列表?

ansible - 如果主机无法访问,则中止 ansible playbook

ansible 获取变量的 rpm 版本