整个剧本范围内的变量 [英] Ansible playbook-wide variable

查看:74
本文介绍了整个剧本范围内的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一部包含多个主机的剧本.我想在此playbook.yml文件中定义一个仅在文件内适用的变量,例如:

I have a playbook with multiple hosts section. I would like to define a variable in this playbook.yml file that applies only within the file, for example:

vars:
  my_global_var: 'hello'

- hosts: db
  tasks:
   -shell: echo {{my_global_var}} 

- hosts: web
  tasks:
   -shell: echo {{my_global_var}} 

上面的示例不起作用.我必须为每个主机部分复制该变量(坏),或者在更高级别上对其进行定义,例如在我的group_vars/all中(不是我想要的,但是可以工作).我也知道可以包含变量文件,但这会影响可读性.有什么建议将其放在正确的范围内(例如,剧本文件本身)?

The example above does not work. I have to either duplicate the variable for each host section (bad) or define it at higher level, for example in my group_vars/all (not what I want, but works). I am also aware that variables files can be included, but this affects readibility. Any suggestion to get it in the right scope (e.g the playbook file itself)?

推荐答案

如果group_vars不适合您的需求,set_fact模块将完成此操作.

The set_fact module will accomplish this if group_vars don't suit your needs.

http://docs.ansible.com/ansible/set_fact_module.html

此模块允许设置新变量.就像在setup模块中发现的事实一样,在逐个主机的基础上设置变量.这些变量将在Ansible运行期间在播放之间存活,但是即使使用事实缓存,也不会在执行之间保存这些变量.

This module allows setting new variables. Variables are set on a host-by-host >basis just like facts discovered by the setup module. These variables will >survive between plays during an Ansible run, but will not be saved across >executions even if you use a fact cache.

- hosts: db:web
  tasks:
  - set_fact: my_global_var='hello'

- hosts: db
  tasks:
  -shell: echo {{my_global_var}} 

- hosts: web
  tasks:
  -shell: echo {{my_global_var}} 

这篇关于整个剧本范围内的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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