Ansible+Jinja2循环Dict对象没有属性

2024-09-30 08:28:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这里失去了理智,但我似乎看不到我的问题在哪里!我试图让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模板在哪里出了问题,因为我做了很多事情,但却一事无成

非常感谢:)

克里斯


Tags: andkey用户isvaluemsgitemusers
1条回答
网友
1楼 · 发布于 2024-09-30 08:28:15

这个循环

{% 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 %}

…不检查用户是否具有sshkey属性。表达式if users and 'sshkey' is defined与写入if users相同,因为表达式if 'sshkey' is defined将始终是true{}是一个文本字符串,而不是一个变量

由于循环为所有用户运行,因此在到达harry时失败,因为harry没有sshkey属性

您需要筛选那些具有sshkey属性的用户,例如使用循环筛选,如in the documentation所述:

{% for user, value in users.items() if value.sshkey is defined %}
username {{ user }} privilege {{ value.privilegelevel }}
username {{ user }} attributes
  ssh authentication publickey {{ value.sshkey }}
{% endfor %}

请注意,我还将username {{ value }}替换为username {{ user }},因为我相当确定前者不是您的意思


例如,本剧本:

- hosts: localhost
  gather_facts: false
  vars:
    users:
      tom:
        sshkey: "xxxx"
        privilegelevel: 15
      dick:
        sshkey: "xxxx"
        privilegelevel: 15
      harry:
        password: "yyyy"
        privilegelevel: 15

  tasks:
    - copy:
        dest: users.txt
        content: |
          {% for user, value in users.items() if value.sshkey is defined %}
          username {{ user }} privilege {{ value.privilegelevel }}
          username {{ user }} attributes
            ssh authentication publickey {{ value.sshkey }}
          {% endfor %}

users.txt中生成为输出:

username tom privilege 15
username tom attributes
  ssh authentication publickey xxxx
username dick privilege 15
username dick attributes
  ssh authentication publickey xxxx

相关问题 更多 >

    热门问题