我在这里失去了理智,但我似乎看不到我的问题在哪里!我试图让ansible在Cisco ASA上创建我的用户,我使用Jinja 2模板
我有一个主机vars文件,其中我指定了用户(敏感数据):
users:
tom:
sshkey: "xxxx"
privilegelevel: 15
dick:
sshkey: "xxxx"
privilegelevel: 15
harry:
password: "yyyy"
privilegelevel: 15
用于创建用户的我的模板(users.j2):
{% if users is defined %}
{% for user, value in users.items() %}
username {{ value }} privilege {{ value.privilegelevel }}
{% endfor %}
{% endif %}
如果他们有ssh密钥(用户ssh.j2):
{% if users and 'sshkey' is defined %}
{% for user, value in users.items() %}
username {{ value }} privilege {{ value.privilegelevel }}
username {{ value }} attributes
ssh authentication publickey {{ value.sshkey }}
{% endfor %}
{% endif %}
最后但并非最不重要的是剧本:
- name: create users
asa_config:
src: templates/users.j2
provider: "{{ cli }}"
save: yes
tags: users
- name: create ssh pubkey auth
asa_config:
src: templates/users-ssh.j2
provider: "{{ cli }}"
save: yes
tags: users
运行playbook时,user.j2工作正常,但user-ssh.j2出现以下故障:
fatal: [HOSTNAME_HERE]: FAILED! => {"changed": false, "msg": "'dict object' has no attribute 'sshkey'"}
使用以下命令调用dict值时:
- name: Output dict
debug:
msg: "User is {{ item.key }} and the ssh key is {{ item.value.sshkey }}"
loop: "{{ lookup('dict', users) }}"
tags: users
它为我提供了正确的值:
ok: [fw01.prd.sc1] => (item={'key': 'tom', 'value': {'sshkey': 'xxxx', 'privilegelevel': 15}}) => {
"msg": "User is tom and the ssh key is xxxx"
}
ok: [fw01.prd.sc1] => (item={'key': 'dick', 'value': {'sshkey': 'xxxx', 'privilegelevel': 15}}) => {
"msg": "User is dick and the ssh key is xxxx"
}
任何人都能看到我的J2模板在哪里出了问题,因为我做了很多事情,但却一事无成
非常感谢:)
克里斯
这个循环
…不检查用户是否具有}是一个文本字符串,而不是一个变量
sshkey
属性。表达式if users and 'sshkey' is defined
与写入if users
相同,因为表达式if 'sshkey' is defined
将始终是true
{由于循环为所有用户运行,因此在到达
harry
时失败,因为harry
没有sshkey
属性您需要筛选那些具有
sshkey
属性的用户,例如使用循环筛选,如in the documentation所述:请注意,我还将
username {{ value }}
替换为username {{ user }}
,因为我相当确定前者不是您的意思例如,本剧本:
在
users.txt
中生成为输出:相关问题 更多 >
编程相关推荐