0

When I try to loop (with Ansible 2.6 loop) with subelements through all public keys of a list of users and encounter a user which has no public keys defined:

- authorized_key:
    user: "{{ item.0.username }}"
    state: present
    key: "{{ item.1.pub_key }}"
  loop: "{{ users | subelements('ssh_pub_keys') | default ([]) }}"
  loop_control:
    label: "{{ item.username }}"

I'm getting the following error: the key 'ssh_pub_keys' should point to a list, got None

When I try to use skip_missing like this:

- authorized_key:
    user: "{{ item.0.username }}"
    state: present
    key: "{{ item.1.pub_key }}"
    loop: "{{ lookup('subelements', users, 'ssh_pub_keys', {'skip_missing': True})}}"
  loop_control:
    label: "{{ item.username }}"

I get this error: 'list object' has no attribute 'username'

This could be the users list:

users:
  - username: usera
    ssh_pub_keys:
      - from: home
        pub_key: kdzadizajdiazjd
      - from: work
        pub_key: dzadadazdzadzad
  - username: userb
    ssh_pub_keys:
      - from: home
        pub_key: kdzadizajdiazjd
      - from: work
        pub_key: dzadadazdzadzad
  - username: userc
  - username: userd
    ssh_pub_keys:
      - from: home
        pub_key: kdzadizajdiazjd
      - from: work
        pub_key: dzadadazdzadzad

How can I make the loop with subelements to go to the next user without throwing an error when a user is encountered which has no ssh_pub_keys list?

0

You might want to use lookup with 'skip_missing': True.

loop: "{{ lookup('subelements', users, 'ssh_pub_keys', {'skip_missing': True})}}"
  • Thanks, but when I try 'loop: "{{lookup('subelements', users, 'ssh_pub_keys', {'skip_missing': True})}}"' I get the error: 'list object' has no attribute 'username' – willemdh Jan 14 '19 at 20:20
  • It seems like when I remove the loop control, your solution works. Feel free to add that to your answer. – willemdh Jan 15 '19 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.