1

Here is a simple vars file we have to debug ./roles/test/vars/{{ ansible_distribution|lower }}/apt-packages.yml

  packages:
    required:
      - htop
#      - aptitude

  package:
    htop:
      allow_unauthenticated: no
      autoclean: no
      autoremove: no
      cache_valid_time: 0
#    default_release:
      force: no
      force_apt_get: no
      install_recommends: yes
      only_upgrade: no
      purge: no 
      state: latest
      update_cache: yes
      upgrade: no

Here is a simple task to debug ./roles/test/tasks/main.yml

- name: "Register variable"
  include_vars:
    #dir: vars/ubuntu
    file: "vars/{{ ansible_distribution|lower }}/apt-packages.yml"
    name: apt_install

- name: "This a test"
  apt:
    name: "{{item}}"
    cache_valid_time: "{{ apt_install.package[item].cache_valid_time }}"
    state: "{{ apt_install.package[item].state }}"
    update_cache: "{{ apt_install.package[item].update_cache }}"
  with_items: "{{ apt_install.packages.required }}"

./roles/test-playbook.yml

- name: "playbook test"
  hosts: localhost
  roles:
    - role: test
  become: true
  become_user: root
  become_method: sudo

using following answer stackoverflow.com/questions/29276198 we are trying to loop through items and get item related values.

Tasks is looping well through items but it is impossible to retrieve related variable with [item] syntax or any other one we have tested.

We have always the same error

fatal: [localhost]: FAILED! => { "msg": "The task includes an option with an undefined variable. The error was: dict object has no element [u'htop']

But calling directly the variable works

- name: "echo variable test"
  debug: 
    msg: "{{ apt_install.package.htop.allow_unauthenticated }}"

What is the right syntax to get current loop value of a variable and use it inside another variable to retrieve related value ... (inside the same task) ?

So far it's we who are going around in circles without end !

Kind Regards

0

1 Answer 1

1

This is fully working using a loop / loop_control instead of with_items

- name: "This a test"
  apt:
    name: "{{item}}"
    cache_valid_time: "{{ apt_install.package[item].cache_valid_time }}"
    state: "{{ apt_install.package[item].state }}"
    update_cache: "{{ apt_install.package[item].update_cache }}"
  loop: "{{ apt_install.packages.required|flatten(levels=1) }}"
  loop_control:
    index_var: index

So as it we can define different settings for each package and for each distrib. Now I can export too settings for each package in different var files.

Learning is hard :)

Sign up to request clarification or add additional context in comments.

1 Comment

Not verified this, but using the "flatten(level=1)" looks promising, but not sure "loop_control.index_var: index" is correct, when the default 'item' is being referenced in the task body.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.