简体   繁体   English

在 ansible 中,如何连接设置文件 yaml 中已定义的变量和额外变量?

[英]In ansible how do I concatenate a already defined variable in a settings file yaml and an extra-var?

I created a settings file called settings.yaml that looks like this:我创建了一个名为settings.yaml的设置文件,如下所示:

cust_int: 'ens224'
cust_sub_int: '{{ cust_int }}.{{ cust }}
  • cust_int, is the already defined variable above cust_int,就是上面已经定义好的变量
  • cust, Is a variable provided with --extra-var cust, 是随--extra-var一起提供的变量

here is the playbook:这是剧本:

- name: Include vars
  include_vars:
      file: ../../../settings.yaml
      name: settings

- debug: msg="{{ settings.cust_sub_int }}"

When trying to concatenate this way I get unclear error that the playbook "did not find the expected key".当尝试以这种方式连接时,我得到不清楚的错误,即剧本“没有找到预期的密钥”。

My question is, how can I combine these two variables in my settings file?我的问题是,如何在我的设置文件中组合这两个变量? I dont want to have to use set_fact in all my playbooks.我不想在我的所有剧本中都使用set_fact

See ansible cannot use variable to learn details why this is not working.请参阅ansible cannot use variable以了解这不起作用的详细信息。 The solution is rather simple.解决方案相当简单。 Split the settings into common variables and the dictionary.将设置拆分为公共变量和字典。 For example,例如,

shell> cat settings-common.yaml
_cust_int: 'ens224'
shell> cat settings.yaml
cust_int: '{{ _cust_int }}'
cust_sub_int: '{{ _cust_int }}.{{ cust }}'

Include both files包括这两个文件

    - include_vars:
        file: settings-common.yaml
    - include_vars:
        file: settings.yaml
        name: settings

Running the play with extra var cust=foo gives the expected result使用额外的 var cust=foo运行游戏会得到预期的结果

  settings:
    cust_int: ens224
    cust_sub_int: ens224.foo

Example of a complete project for testing完整的测试项目示例

shell> tree. . ├── ansible.cfg ├── hosts ├── pb.yml ├── settings-common.yaml └── settings.yaml 0 directories, 5 files
 shell> cat ansible.cfg [defaults] gathering = explicit inventory = $PWD/hosts retry_files_enabled = false stdout_callback = yaml
 shell> cat hosts localhost
 shell> cat pb.yml - hosts: localhost tasks: - include_vars: file: settings-common.yaml - include_vars: file: settings.yaml name: settings - debug: var: settings - debug: msg: "{{ settings.cust_sub_int }}"

gives

shell> ansible-playbook pb.yml -e cust=foo PLAY [localhost] ***************************************************************************************************************** TASK [include_vars] ************************************************************************************************************** ok: [localhost] TASK [include_vars] ************************************************************************************************************** ok: [localhost] TASK [debug] ********************************************************************************************************************* ok: [localhost] => settings: cust_int: ens224 cust_sub_int: ens224.foo TASK [debug] ********************************************************************************************************************* ok: [localhost] => msg: ens224.foo PLAY RECAP *********************************************************************************************************************** localhost: ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

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

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