HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How do a flaten a list mixed with nested dict in ansible playbook?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
dictansiblewithflatenplaybooknestedhowlistmixed

Problem

I have a variable named packages as showed below, it is not a plain list, I have some nested dict in it like browsers.


---
packages:
- apt-file
- axel
- ibus-rime
- rofi
- i3
- ffmpeg
- xclip
- cargo
- httpie
- ttf-ubuntu-font-family
- tldr-py
- glances
- browsers:
- brave-browser
- chromium
- editors:
- emacs


I'd like to use instructions like below

- name: Install a list of packages
    become: yes
    apt:
        name: "{{ packages }}"


to install all the pacakges defined in the variable, but I do not know what filter I should use the convert the variable to plain list, I need the items in the nested dictionary.

any help will be appreciated.

Solution

I probably should use comment instead group the value in a dict who's value is a list

like

packages:
  - apt-file
  - axel
  - ibus-rime
  - rofi
  - i3
  - ffmpeg
  - xclip
  - cargo
  - httpie
  - ttf-ubuntu-font-family
  - tldr-py
  - glances
  # browsers:
  - brave-browser
  - chromium
  # editors
  - emacs

Code Snippets

packages:
  - apt-file
  - axel
  - ibus-rime
  - rofi
  - i3
  - ffmpeg
  - xclip
  - cargo
  - httpie
  - ttf-ubuntu-font-family
  - tldr-py
  - glances
  # browsers:
  - brave-browser
  - chromium
  # editors
  - emacs

Context

StackExchange DevOps Q#11703, answer score: 1

Revisions (0)

No revisions yet.