0

I would like to build an ansible playbook and at this point I need some help.

I have my defaults:

variables:
  module1: true
  module2: false
  module3: true
  module4: true

I need now to find out which module key has a true-value and put the key name in my command

- name: Install Packages
  command: {{ item }}
  with_items:
   - ./configure

The output should be something like this:

- name: Install Packages
  command: {{ item }}
  with_items:
   - ./configure --module1 **--no**-module2 --module3 --module4

How can I get all this modules and build my configure command?

Thanks!

sokolata
  • 491
  • 3
  • 7
  • 21

1 Answers1

0

The play below

- hosts: localhost
  gather_facts: no
  vars:
    command: ""
    my_variables:
      module1: true
      module2: false
      module3: true
      module4: true
  tasks:
    - set_fact:
        command: "{{ command ~ item.value|ternary(' --' ~ item.key,
                                                  ' **--no**-' ~ item.key) }}"
      loop: "{{ my_variables|dict2items }}"
    - debug:
        var: command

gives (abridged):

"command": " --module4 --module3 **--no**-module2 --module1"

Notes:

  • dict2items works with ansible version >= 2.6
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63