简体   繁体   English

Ansible 如何通过 extra_vars 使用 with_dict?

[英]Ansible how use with_dict by extra_vars?

how can I use with_dict by extra_vars?如何通过 extra_vars 使用 with_dict?

I try I know everything but all output with_dict expects a dict :(我试着我知道一切,但所有输出 with_dict 都需要一个 dict :(

This is all files这是所有文件

# vars.yml
rd1:
  Terry:
    user_name:terry_liu
    user_birth:1994/05/11
  Cary:
    user_name:cary_lin
    user_birth:1992/02/19
rd6:
  Jessie:
    user_name:jessie_chen
    user_birth:1996/11/20
  Sherry:
    user_name:sherry_hsu
    user_birth:1989/07/23

- ——

# test.yml
- name: demo
  hosts: test
  vars_files:
    - vars.yml

  tasks:
    - name: show data
      debug:
        msg: "{{ item }}"
      with_dict: "{{ dep }}"

- ——

#command
ansible-playbook -i inventory test.yml --extra-vars 'dep=rd1'

- ——

Inventory's host is my test vm, just have an ip and it can be ssh. Inventory 的主机是我的测试虚拟机,只要有一个 ip 就可以是 ssh。

When run command, it output: fatal: [172.16.1.227]: FAILED!运行命令时,输出: fatal: [172.16.1.227]: FAILED! => {"msg": "with_dict expects a dict"} => {"msg": "with_dict 需要一个 dict"}

I think it's need var in var, but I try many different way, all fail.我认为它需要 var in var,但我尝试了很多不同的方法,都失败了。

My demand is send a float dep var and get correspond data from vars.yml.我的需求是发送一个 float dep var 并从 vars.yml 获取相应的数据。

Thanks all, have a good day!谢谢大家,祝你有美好的一天!

The problem is that "{{ dep }}" evaluates to the string "rd1"问题是"{{ dep }}"计算结果为字符串"rd1"

with_dict: "{{ dep }}"

This is the reason for the error "with_dict expects a dict" .这就是错误"with_dict expects a dict"

Instead, you need lookup and vars plugin.相反,您需要lookupvars插件。 For example例如

with_dict: "{{ lookup('vars', dep) }}"

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

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