1

私はここで頭がおかしくなっていますが、私の問題がどこにあるのかわかりません! 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テンプレートでどこが間違っているのか誰にもわかりますか?

よろしくお願いします:)

クリス

4

1 に答える 1