Ansible with_dict需要一个dict-blank空字典变量
表1.9.4 在我的Ansible with_dict需要一个dict-blank空字典变量,ansible,ansible-2.x,Ansible,Ansible 2.x,表1.9.4 在我的group\u vars/slave/slave文件中,我将以下变量设置为空值(更准确地说,为无/未定义/空字符串): Playbook调用任务/操作: - name: Ensure nfs mount directory exists file: path={{item.key}} state=directory with_dict: "{{slave_nfsmount | default({})}}" ignore_errors: yes 获
group\u vars/slave/slave
文件中,我将以下变量设置为空值(更准确地说,为无/未定义/空字符串):
Playbook调用任务/操作:
- name: Ensure nfs mount directory exists
file: path={{item.key}} state=directory
with_dict: "{{slave_nfsmount | default({})}}"
ignore_errors: yes
获取错误消息:
TASK: [Ensure nfs mount directory exists] **************************************
fatal: [12.19.22.33] => with_dict expects a dict
FATAL: all hosts have already failed -- aborting
I不希望在全局变量/slave/slave文件中设置slave\u nfsmount变量的值(因为用户可以在命令行中传递此值,也可以通过其他方式使其可用,这取决于用户在运行时是否希望传递)。因为,目前它对slave\u nfsmount变量使用空的/none值,所以我使用“{{slave\u nfsmount | default({}}}}}”
和with_dict:
语句
当值为空/空/未定义/空时,我如何使其工作,以便我的剧本不会失败
我试着给出:并且没有定义slave\n数量
或
ignore\u errors:yes
(True
),但它不起作用,playbook仍然失败,出现致命错误,如上所示
我将以下变量设置为空值(无/未定义)
不,你没有。您已经定义了一个变量
例如,使用三元过滤器
with_dict: "{{slave_nfsmount | ternary(slave_nfsmount, {})}}"
default()
过滤器仅在变量未定义时返回默认值
如果该值未定义,则返回传递的默认值,否则返回变量的值:
在您的例子中,定义了从属\u nfsmount,它为空
如果您想让额外的变量(即-e
参数)覆盖该变量,请在初始化时为其提供一个默认值:
slave_nfsmount: {}
同意@techraf,但问题仍然有效:)不,该值为空。这实际上是有意义的(与我的问题相关)。仍然想知道,这是否解决了问题,很好,但是为什么
忽略错误:是或忽略错误:True
没有忽略/跳过错误..因为它类似于语法/编译错误(无法解决的变量引用),而不是任务本身生成的错误。
slave_nfsmount: {}