简体   繁体   中英

Ansible how use with_dict by extra_vars?

how can I use with_dict by extra_vars?

I try I know everything but all output with_dict expects a 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.

When run command, it output: fatal: [172.16.1.227]: FAILED! => {"msg": "with_dict expects a dict"}

I think it's need var in var, but I try many different way, all fail.

My demand is send a float dep var and get correspond data from vars.yml.

Thanks all, have a good day!

The problem is that "{{ dep }}" evaluates to the string "rd1"

with_dict: "{{ dep }}"

This is the reason for the error "with_dict expects a dict" .

Instead, you need lookup and vars plugin. For example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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