【发布时间】:2021-08-06 17:17:59
【问题描述】:
我在这里疯了,但我似乎看不出我的问题出在哪里!我试图让 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 密钥(users-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 模板可能出了什么问题,因为我做了很多事情但一无所获!
提前非常感谢:)
克里斯
【问题讨论】:
标签: python ansible jinja2 cisco asa