Ansible - 将环境路径设置为库存变量 [英] Ansible - Set environment path as inventory variable

查看:80
本文介绍了Ansible - 将环境路径设置为库存变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Osmc 媒体播放器需要特定的 playbook 路径https://github.com/osmc/osmc/issues/319

Osmc media player needs a specific path for playbooks https://github.com/osmc/osmc/issues/319

environment:
  PATH: "{{ ansible_env.PATH }}:/sbin:/usr/sbin"

我想知道我是否可以在这些机器的清单中将其设置为环境变量,而不是将它放在每个剧本中或创建单独的剧本.

I was wondering whether I can set this as an environmental variable in the inventory for those machines, rather than have it in every playbook or create separate playbooks.

常见用法 - 如果在非 osmc 安装上实现该路径是否可能导致一般 *nix 机器出现问题?

In common usage - is that path likely to cause problems for general *nix machines if it is implemented on non-osmc installations?

如果您无法将其设置为库存变量:仅仅是因为它没有实现/对大多数人有用吗?或者因为库存与路径无关 - 例如那个时候它没有被调用?

If you can't set this an an inventory variable: Is that just because it's no implemented/ useful to most? Or because the inventory has no relation to path - e.g. it's not invoked at that point?

或者是将其作为角色中特定于机器的变量/任务的更好方法?请问那会怎样?

Or is a better way for all of this to have it as a machine specific variable/ task in a role? How would that look please?

对 ansible 不熟悉,但仍在尝试理解一些概念.

New to ansible and still trying to get my head round some of the concepts.

推荐答案

如前所述,environment 关键字只能在任务或剧本级别使用.

As said, the environment keyword can be used only at task or playbook level.

您只需添加以下内容即可使用标准剧本:

You will be able to use an standard playbook just adding the following:

---
- name: Environment
  hosts: localhost
  connection: local
  gather_facts: False

  tasks:

  - name: Setup
    setup:
      gather_subset:
        - "!all"

---
- name: Environment
  hosts: localhost
  connection: local
  gather_facts: True
  gather_subset:
    - "!all"

如果你调试变量:

---
- name: Environment
  hosts: localhost
  connection: local
  gather_facts: False

  tasks:

  - name: Setup
    setup:
      gather_subset:
        - "!all"

  - name: Debug
    debug:
      var: ansible_env.PATH

你会得到类似的东西:

TASK [Setup] *******************************************************************************************************************************************************
ok: [localhost]

TASK [Debug] *******************************************************************************************************************************************************
ok: [localhost] => {
    "ansible_env.PATH": "/Users/imjoseangel/source/venv/ansible/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
}

如果您想将该变量传递给另一个具有不同库存的游戏怎么办?

And what if you want to pass that variable to another play with different inventory?

只需执行hostvars.localhost.ansible_env.PATH

- name: Environment2
  hosts: windows
  connection: local
  gather_facts: False


  tasks:

  - name: Debug
    debug:
      var: hostvars.localhost.ansible_env.PATH

所以

environment:
  PATH: "{{ ansible_env.PATH }}:/sbin:/usr/sbin"

仅对定义清单下的 gather_facts 或 setup 模块有效,但您不需要拆分剧本.

Will be valid only with gather_facts or setup module under the defined inventory but you don't need to split playbooks.

这篇关于Ansible - 将环境路径设置为库存变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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