从命令行覆盖Ansible剧本的主机变量 [英] Override hosts variable of Ansible playbook from the command line

查看:51
本文介绍了从命令行覆盖Ansible剧本的主机变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在使用的(server.yml)剧本的一部分:

This is a fragment of a playbook that I'm using (server.yml):

- name: Determine Remote User
  hosts: web
  gather_facts: false
  roles:
    - { role: remote-user, tags: [remote-user, always] }

我的主机文件具有不同的服务器组,例如

My hosts file has different groups of servers, e.g.

[web]
x.x.x.x

[droplets]
x.x.x.x

现在我要执行ansible-playbook -i hosts/<env> server.yml并从server.yml覆盖hosts: web来为[droplets]运行此剧本.

Now I want to execute ansible-playbook -i hosts/<env> server.yml and override hosts: web from server.yml to run this playbook for [droplets].

我可以直接重写一次,而不必直接编辑server.yml吗?

Can I just override as a one time off thing, without editing server.yml directly?

谢谢.

推荐答案

我认为Ansible不应该提供此功能.您可以执行以下操作:

I don't think Ansible provides this feature, which it should. Here's something that you can do:

hosts: "{{ variable_host | default('web') }}"

,您可以从命令行或vars文件传递variable_host,例如:

and you can pass variable_host from either command-line or from a vars file, e.g.:

ansible-playbook server.yml --extra-vars "variable_host=newtarget(s)"

这篇关于从命令行覆盖Ansible剧本的主机变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆