Jack A
Jack A

Reputation: 57

Multiple facts retrieved in set_fact task causes playbook run to issue error

Multiple facts retrieved using set_fact in playbook, causes error

I have tried separate set_fact tasks, 1 per fact retrieved. Also got error. Seems to occur when I have 3 facts defined under set_fact i.e when I include mountsize_tmp. No error when I have the first 2 facts only. Does a variable name used in set_fact need to be defined in the var variables section?

Error is: The offending line appears to be: set_fact: ^ here exception type: exception: No first item, sequence was empty.

   - set_fact:
      alto_seal: "{{ ansible_local.alto_bootstrap.seal }}"  # local fact
      mountsize: "{{ ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | first }}"     # ansible fact
      mountsize_tmp: "{{ ansible_mounts | selectattr('mount', 'equalto', '/tmp') | map(attribute='size_total') | first }}"

  - debug:
      msg: "{{ 'Alto start mountsize ' ~ mountsize }}"  
  - debug:
      msg: "{{'Seal  ' ~ alto_seal }}"

Expect values of a file mount a size and a fact from a custom fact file to be displayed in 2 rows

Upvotes: 1

Views: 2849

Answers (1)

Romain Spinelli
Romain Spinelli

Reputation: 443

To avoid your exception you can manage that by:

  1. Defining default values:
- set_fact:
      alto_seal: "{{ ansible_local.alto_bootstrap.seal }}"  # local fact

- set_fact:
      mountsize: "<DEFAULT_VALUE_OF_YOUR_CHOICE>"
      mountsize_tmp: "<DEFAULT_VALUE_OF_YOUR_CHOICE>"

- set_fact:
      mountsize: "{{ ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | first }}"     # ansible fact
  when: ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | list != []

- set_fact:
      mountsize_tmp: "{{ ansible_mounts | selectattr('mount', 'equalto', '/tmp') | map(attribute='size_total') | first }}"
  when: ansible_mounts | selectattr('mount', 'equalto', '/tmp') | map(attribute='size_total') | list != []

or

  1. Sending a specific message:
- set_fact:
      alto_seal: "{{ ansible_local.alto_bootstrap.seal }}"  # local fact

- set_fact:
      mountsize: "{{ ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | first }}"     # ansible fact
      msg: "{{ 'Alto start mountsize ' ~ mountsize }}"
  when: ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | list != []

- set_fact:
      msg: "Alto start mountsize cannot be calculated"
  when: ansible_mounts | selectattr('mount', 'equalto', '/abc') | map(attribute='size_total') | list == []

- debug:
      msg: "{{ msg }}"

Upvotes: 0

Related Questions