Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template 從dict 中取值 #56

Open
changchichung opened this issue Jul 20, 2021 · 1 comment
Open

template 從dict 中取值 #56

changchichung opened this issue Jul 20, 2021 · 1 comment

Comments

@changchichung
Copy link

changchichung commented Jul 20, 2021

我有兩個 dict 內容長這樣
teams.yml

{
"teams": [{"gid": 10125,"location": ["hq"],"name": "aa","users": ["chenhu","chouf"]},
{"gid": 10126,"location": ["hq"],"name": "ac","users": ["guoj","yangc"]}]

}

users.yml
{
"users": [{"name": "changc","uid": 20000},{"name": "changb","uid": 20001}]
}

我想要取出 teams 裡面的users
所以我簡單的先用個for loop 來跑跑看

{% for j in teams %}   
{{ j.users }}
{% endfor %}         

然後就會報錯

AnsibleUndefinedVariable: 'dict object' has no attribute 'users'

可是 users 不就在那邊嗎?為什麼抓不到啊?
不只是 users ,只要是值有用 [ ] 包起來的,像是 teams 的location ,我也是抓不到 ,但是 teams.gid 卻可以... ???

@elleryq
Copy link

elleryq commented Jul 22, 2021

我測試 ok ,以下是我的 playbook

https://gist.github.com/elleryq/b0b62498b3f57a9badbba26830f67344

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants