简体   繁体   English

Ansible:如何在剧本中声明全局变量?

[英]Ansible: How to declare global variable within playbook?

How can I declare global variable within Ansible playbook.如何在 Ansible 剧本中声明全局变量。 I have searched in google and found the below solution, but its not working as expected.我在谷歌中搜索并找到了以下解决方案,但它没有按预期工作。

- hosts: all
  vars:
    prod-servers:
     - x.x.x.x
     - x.x.x.x


- hosts: "{{prod-servers}}"
  tasks:
  - name: ping
    action: ping

When I'm trying the above code, it says variable prod-servers is undefined.当我尝试上面的代码时,它说变量prod-servers未定义。

You cannot define a variable accessible on a playbook level (global variable) from within a play.不能从剧本中定义在剧本级别(全局变量)上可访问的变量。

Variable Scopes 变量范围

Ansible has 3 main scopes: Ansible 有 3 个主要作用域:

  • Global: this is set by config, environment variables and the command line全局:由配置、环境变量和命令行设置

  • Play: each play and contained structures, vars entries (vars; vars_files; vars_prompt), role defaults and vars.播放:每个播放和包含的结构、vars 条目(vars; vars_files; vars_prompt)、角色默认值和 vars。

  • Host: variables directly associated to a host, like inventory, include_vars, facts or registered task outputs主机:与主机直接关联的变量,例如库存、include_vars、事实或已注册的任务输出

Anything you declare inside a play can thus only be either a play variable, or a (host) fact.因此,您在游戏中声明的任何内容都只能是游戏变量或(主机)事实。


To define a variable, which you can use in the hosts declaration:要定义一个变量,您可以在hosts声明中使用该变量:

  • run ansible-playbook with --extra-vars option and pass the value in the argument;使用--extra-vars选项运行 ansible ansible-playbook并在参数中传递值;

or to achieve the same functionality (decide which hosts to run a play on, from within a preceding play):或实现相同的功能(从之前的播放中决定在哪些主机上运行播放):

what you seem to want is an inventory ( http://docs.ansible.com/ansible/latest/intro_inventory.html ), it looks like you have an static list of IP's that may be prod servers (or dev, or whatever), therefore you can create an static inventory.你似乎想要的是一个清单( http://docs.ansible.com/ansible/latest/intro_inventory.html ),看起来你有一个静态的IP列表,可能是产品服务器(或开发,或其他) ,因此您可以创建静态清单。

In your second play you want to use the list of IP's as hosts to run the tasks, that's not what Ansible expects.在您的第二次游戏中,您希望使用 IP 列表作为主机来运行任务,这不是 Ansible 所期望的。 After the "hosts" keyword in a play declaration, Ansible expects a group name from the inventory.在 play 声明中的“hosts”关键字之后,Ansible 需要一个来自清单的组名。

If, on the opossite, your prod servers change from time to time, you may need to create a dynamic inventory.如果在 opossite 上,您的产品服务器不时更改,您可能需要创建动态清单。 You can have a look at examples inhttps://github.com/ansible/ansible/tree/devel/contrib/inventory (for instance, there are examples of dynamic inventory based on EC2 from Amazon or vsphere)您可以查看https://github.com/ansible/ansible/tree/devel/contrib/inventory中的示例(例如,有来自 Amazon 或 vsphere 的基于 EC2 的动态库存示例)

regards问候

well, this can be done using好吧,这可以使用

set_fact.设置事实。

I don't know the best practice for this but this works for me我不知道最好的做法,但这对我有用

Here's my playbook example这是我的剧本示例

- hosts: all
  gather_facts: false
  tasks:
   - set_fact: host='hostname'

- hosts: host-name1
  gather_facts: false
  tasks:
    - name: CheckHostName
      shell: "{{ host }}"
      register: output
    - debug: msg="{{ output }}"

- hosts: host-name2
  gather_facts: false
  tasks:
    - name: CheckHostName
      shell: "{{ host }}"
      register: output
    - debug: msg="{{ output }}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM