Ansible:如何继承变量?

Joh*_*nes 5 ansible

我想在Ansible中实现变量继承。可以说我有:

group_vars /全部

---
ifaces:
   -   name: eth0
       adress: 10.107.13.236
       netmask: 255.255.255.192
       routes:
           - {from: 10.108.100.34/31, via: 10.107.13.193}
           - {from: 10.108.105.128/31, via: 10.107.13.193}
   -   name: eth1
       adress: 10.112.13.236
       netmask: 255.255.255.192
       gateway: 10.112.13.193
       routes:
           - {from: 10.2.1.0/26, via: 10.112.13.254}
Run Code Online (Sandbox Code Playgroud)

现在,我想扩展eth0的路由,如下所示:

group_vars /网络服务器

--- ifaces:
   -   name: eth0
       routes:
           - {from: 1.2.3.34, via: 5.6.7.8}
           - {from: 9.9.9.9/9, via: 5.6.7.8}
Run Code Online (Sandbox Code Playgroud)

我想要的结果是:

---
ifaces:
   -   name: eth0
       adress: 10.107.13.236
       netmask: 255.255.255.192
       routes:
           - {from: 10.108.100.34/31, via: 10.107.13.193}
           - {from: 10.108.105.128/31, via: 10.107.13.193}
           - {from: 1.2.3.34, via: 5.6.7.8}
           - {from: 9.9.9.9/9, via: 5.6.7.8}
   -   name: eth1
       adress: 10.112.13.236
       netmask: 255.255.255.192
       gateway: 10.112.13.193
       routes:
           - {from: 10.2.1.0/26, via: 10.112.13.254}
Run Code Online (Sandbox Code Playgroud)

因此,路由应扩展且不被覆盖。我知道设置hash_behaviour: mergeansible.cfg但这不能满足我的需要,因为我想附加到存储在路由中的列表中。

背景是,我需要能够定义一些标准路由(请注意:这不仅限于路由,它只是示例),并针对特定组增强这些标准,而不是覆盖它们。

这在Ansible中可能吗?

Vor*_*Vor 2

我建议创建lookup_plugin它将完成所有合并/附加变量的繁重工作。

例如:

Lookup_plugins/myvars.py

import yaml
class LookupModule(object):
    def __init__(self, basedir=None, **kwargs):
        self.basedir = basedir
        self.plugin_name = 'myvars'
    def run(self, vars_file, variable=None, **kwargs):
        all_routes = yaml.load(file('group_vars/all'))['ifaces'][0]['routes']
        all_routes.extend(
            yaml.load(file('group_vars/%s' % vars_file[0]))['ifaces'][0]['routes'])
        return [all_routes]
Run Code Online (Sandbox Code Playgroud)

剧本.yml

---
- hosts: webservers
  gather_facts: no
  connection: local
  tasks:
    - debug: msg=" Hey {{ item }}"
      with_myvars:
        - webservers
Run Code Online (Sandbox Code Playgroud)

当然,它并不能完全解决您的问题,您仍然需要对其进行调整以使其正常工作。但您应该从这个示例中了解如何实现这一目标。

其输出应如下所示:

PLAY [webservers] ************************************************************* 

TASK: [debug msg=" Hey {{ item }}"] ******************************************* 
ok: [localhost] => (item=[{'via': '10.107.13.193', 'from': '10.108.100.34/31'}, {'via': '10.107.13.193', 'from': '10.108.105.128/31'}, {'via': '5.6.7.8', 'from': '1.2.3.34'}, {'via': '5.6.7.8', 'from': '9.9.9.9/9'}]) => {
    "item": [
        {
            "from": "10.108.100.34/31",
            "via": "10.107.13.193"
        },
        {
            "from": "10.108.105.128/31",
            "via": "10.107.13.193"
        },
        {
            "from": "1.2.3.34",
            "via": "5.6.7.8"
        },
        {
            "from": "9.9.9.9/9",
            "via": "5.6.7.8"
        }
    ],
    "msg": " Hey [{'via': '10.107.13.193', 'from': '10.108.100.34/31'}, {'via': '10.107.13.193', 'from': '10.108.105.128/31'}, {'via': '5.6.7.8', 'from': '1.2.3.34'}, {'via': '5.6.7.8', 'from': '9.9.9.9/9'}]"
}

PLAY RECAP ******************************************************************** 
localhost                  : ok=1    changed=0    unreachable=0    failed=0   
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它返回了路线列表,因此您可以轻松地将其放置{{ item }}在您需要的任何位置。