---
# Run: ANSIBLE_STDOUT_CALLBACK=profile_tasks ansible-playbook looping_over_packages.yml
- name: Test looping over list of packages vs list of packages
  hosts: localhost
  remote_user: root
  gather_facts: False

  vars:
    packages:
        - bash
        - bash-completion
        - less
        - acl
        - adduser
        - anacron
        - ansible
        - apt
        - apt-utils
        - arping
        - aria2
#        - aspell
#        - auditd
#        - awesome
#        - bc
#        - binutils
#        - bzip2
#        - bzip2-doc
#        - ca-certificates
#        - ca-certificates-java
#        - bluetooth
#        - bluez
#        - bluez-obexd
#        - bluez-tools
#        - cryptmount
#        - cryptsetup
#        - cryptsetup-bin
#        - cryptsetup-initramfs
#        - cryptsetup-run
#        - dbus
#        - dbus-user-session
#        - dbus-x11
#        - dnsmasq
#        - dnsmasq-base
#        - ethtool
#        - evince
#        - evince-common
#        - firewall-config
#        - firewalld
#        - glabels
#        - glabels-data

    code1: | 
       {% raw %}
       - name: Install packages from list
         apt:
           name: "{{ packages }}"
           state: present
       {% endraw %}

    code2: |
      {% raw %}
      - name: Install packages by using Loop over package list
        apt:
          name: "{{ item }}"
          state: present
        loop: "{{ packages }}"
      {% endraw %}

  tasks:
    - name: Showing code for install packages from list
      debug:
        msg: "{{ code1.split('\n') }}"


    - name: Install packages from list
      apt:
        name: "{{ packages }}"
        state: present

    - name: Showing code for install packages by using Loop over package list
      debug:
        msg: "{{ code2.split('\n') }}"


    - name: Install packages by using Loop over package list
      apt:
        name: "{{ item }}"
        state: present
      loop: "{{ packages }}"

...
