Ansible 检查字典是否存在 [英] Ansible check if dictionary exists

查看:36
本文介绍了Ansible 检查字典是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想部署额外的 mysql 用户 - 如果设置.所以我正在使用字典.例如:

i want to deploy additional mysql users - if set. So i am using a dictionary. E.g.:

mysql_additional_users:
  user1:
    password: mysecretpass
    privs: "database1.*:ALL"

只要定义了字典,它就可以正常工作.如果没有 - 它会失败.

which works fine, as long as the dict is defined. If not - it fails.

虽然我试图确定,如果 dict 已定义:

Although i tried to determine, if the dict is defined:

  - name: create additional mysql users
    mysql_user: name={{ item.key }} password={{ item.value.password }} priv={{ item.value.privs }} state=present      
    with_dict: "{{ mysql_additional_users }}"
    when: mysql_additional_users is defined 

但我总是得到一个错误:

But i always get an error:

fatal: [<hostname>]: FAILED! => {"failed": true, "msg": "ERROR! 'mysql_additional_users' is undefined"}

有什么想法吗?

推荐答案

with_dict 总是被评估.如果你想在 when 中使用它,试试这个:

with_dict is evaluated always. If you want to use it with when, try this:

with_dict: mysql_additional_users | default({})
when: mysql_additional_users is defined

它将评估为空字典,但任务将被 when 跳过.

it will evaluate to empty dict but the task will be skipped by when.

这篇关于Ansible 检查字典是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆