ansible multi dimensional array












0















i start learning ansible and i try to install PHP and php extensions.
I have a file where i declare my php versions and php extensions and its look like:



    php:
versions:
- 7.2:
- cli
- fpm
- curl
- mysql
- pdo
- zip
- xml
- gd
- mbstring
- tokenizer
- ctype
- json
- 7.3:
- cli
- fpm
- curl
- mysql
- pdo
- zip
- xml
- gd
- mbstring
- tokenizer
- ctype
- json


I am doing that because i wanna learning ansible, and also for flexibility and to install different extensions for every php versions.



I try to debug my code using this (this is under tasks section in my playbook.yml file)



- name: DEBUG ANSIBLE PLAYBOOK
debug:
msg: The key is - {{ item.keys() | first }} The item is "{{ item[item.keys() | first] }}"
loop: "{{ php.versions }}"
loop_control:
loop_var: item


I get this output:



TASK [DEBUG ANSIBLE PLAYBOOK] 
ok: [127.0.0.1] => (item={7.2: [u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']}) => {
"msg": "The key is - 7.2 The item is "[u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']""
}
ok: [127.0.0.1] => (item={7.3: [u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']}) => {
"msg": "The key is - 7.3 The item is "[u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']""
}


And i try to install PHP using this task:



- name: Install PHP
package:
pkg: php{{ item.keys() | first }}-{{ item[item.keys() | first] }}
state: latest
loop: "{{ php.versions }}"
loop_control:
loop_var: item
notify: Restart Nginx


But is not working because item[item.keys() | first] is an array and i don't know how to loop again to receive cli, fpt instead of an array.



I study ansible from 3 days, and i really don't understand how can loop more times. I search on the web, i read documentation, but i don't understand



https://docs.ansible.com/ansible/2.4/playbooks_loops.html



If someone can explain, i will appreciate very much!










share|improve this question













migrated from superuser.com Jan 19 at 18:09


This question came from our site for computer enthusiasts and power users.























    0















    i start learning ansible and i try to install PHP and php extensions.
    I have a file where i declare my php versions and php extensions and its look like:



        php:
    versions:
    - 7.2:
    - cli
    - fpm
    - curl
    - mysql
    - pdo
    - zip
    - xml
    - gd
    - mbstring
    - tokenizer
    - ctype
    - json
    - 7.3:
    - cli
    - fpm
    - curl
    - mysql
    - pdo
    - zip
    - xml
    - gd
    - mbstring
    - tokenizer
    - ctype
    - json


    I am doing that because i wanna learning ansible, and also for flexibility and to install different extensions for every php versions.



    I try to debug my code using this (this is under tasks section in my playbook.yml file)



    - name: DEBUG ANSIBLE PLAYBOOK
    debug:
    msg: The key is - {{ item.keys() | first }} The item is "{{ item[item.keys() | first] }}"
    loop: "{{ php.versions }}"
    loop_control:
    loop_var: item


    I get this output:



    TASK [DEBUG ANSIBLE PLAYBOOK] 
    ok: [127.0.0.1] => (item={7.2: [u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']}) => {
    "msg": "The key is - 7.2 The item is "[u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']""
    }
    ok: [127.0.0.1] => (item={7.3: [u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']}) => {
    "msg": "The key is - 7.3 The item is "[u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']""
    }


    And i try to install PHP using this task:



    - name: Install PHP
    package:
    pkg: php{{ item.keys() | first }}-{{ item[item.keys() | first] }}
    state: latest
    loop: "{{ php.versions }}"
    loop_control:
    loop_var: item
    notify: Restart Nginx


    But is not working because item[item.keys() | first] is an array and i don't know how to loop again to receive cli, fpt instead of an array.



    I study ansible from 3 days, and i really don't understand how can loop more times. I search on the web, i read documentation, but i don't understand



    https://docs.ansible.com/ansible/2.4/playbooks_loops.html



    If someone can explain, i will appreciate very much!










    share|improve this question













    migrated from superuser.com Jan 19 at 18:09


    This question came from our site for computer enthusiasts and power users.





















      0












      0








      0








      i start learning ansible and i try to install PHP and php extensions.
      I have a file where i declare my php versions and php extensions and its look like:



          php:
      versions:
      - 7.2:
      - cli
      - fpm
      - curl
      - mysql
      - pdo
      - zip
      - xml
      - gd
      - mbstring
      - tokenizer
      - ctype
      - json
      - 7.3:
      - cli
      - fpm
      - curl
      - mysql
      - pdo
      - zip
      - xml
      - gd
      - mbstring
      - tokenizer
      - ctype
      - json


      I am doing that because i wanna learning ansible, and also for flexibility and to install different extensions for every php versions.



      I try to debug my code using this (this is under tasks section in my playbook.yml file)



      - name: DEBUG ANSIBLE PLAYBOOK
      debug:
      msg: The key is - {{ item.keys() | first }} The item is "{{ item[item.keys() | first] }}"
      loop: "{{ php.versions }}"
      loop_control:
      loop_var: item


      I get this output:



      TASK [DEBUG ANSIBLE PLAYBOOK] 
      ok: [127.0.0.1] => (item={7.2: [u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']}) => {
      "msg": "The key is - 7.2 The item is "[u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']""
      }
      ok: [127.0.0.1] => (item={7.3: [u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']}) => {
      "msg": "The key is - 7.3 The item is "[u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']""
      }


      And i try to install PHP using this task:



      - name: Install PHP
      package:
      pkg: php{{ item.keys() | first }}-{{ item[item.keys() | first] }}
      state: latest
      loop: "{{ php.versions }}"
      loop_control:
      loop_var: item
      notify: Restart Nginx


      But is not working because item[item.keys() | first] is an array and i don't know how to loop again to receive cli, fpt instead of an array.



      I study ansible from 3 days, and i really don't understand how can loop more times. I search on the web, i read documentation, but i don't understand



      https://docs.ansible.com/ansible/2.4/playbooks_loops.html



      If someone can explain, i will appreciate very much!










      share|improve this question














      i start learning ansible and i try to install PHP and php extensions.
      I have a file where i declare my php versions and php extensions and its look like:



          php:
      versions:
      - 7.2:
      - cli
      - fpm
      - curl
      - mysql
      - pdo
      - zip
      - xml
      - gd
      - mbstring
      - tokenizer
      - ctype
      - json
      - 7.3:
      - cli
      - fpm
      - curl
      - mysql
      - pdo
      - zip
      - xml
      - gd
      - mbstring
      - tokenizer
      - ctype
      - json


      I am doing that because i wanna learning ansible, and also for flexibility and to install different extensions for every php versions.



      I try to debug my code using this (this is under tasks section in my playbook.yml file)



      - name: DEBUG ANSIBLE PLAYBOOK
      debug:
      msg: The key is - {{ item.keys() | first }} The item is "{{ item[item.keys() | first] }}"
      loop: "{{ php.versions }}"
      loop_control:
      loop_var: item


      I get this output:



      TASK [DEBUG ANSIBLE PLAYBOOK] 
      ok: [127.0.0.1] => (item={7.2: [u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']}) => {
      "msg": "The key is - 7.2 The item is "[u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']""
      }
      ok: [127.0.0.1] => (item={7.3: [u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']}) => {
      "msg": "The key is - 7.3 The item is "[u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']""
      }


      And i try to install PHP using this task:



      - name: Install PHP
      package:
      pkg: php{{ item.keys() | first }}-{{ item[item.keys() | first] }}
      state: latest
      loop: "{{ php.versions }}"
      loop_control:
      loop_var: item
      notify: Restart Nginx


      But is not working because item[item.keys() | first] is an array and i don't know how to loop again to receive cli, fpt instead of an array.



      I study ansible from 3 days, and i really don't understand how can loop more times. I search on the web, i read documentation, but i don't understand



      https://docs.ansible.com/ansible/2.4/playbooks_loops.html



      If someone can explain, i will appreciate very much!







      automation deployment ansible






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 15:56









      RedoColorRedoColor

      57111




      57111




      migrated from superuser.com Jan 19 at 18:09


      This question came from our site for computer enthusiasts and power users.









      migrated from superuser.com Jan 19 at 18:09


      This question came from our site for computer enthusiasts and power users.


























          2 Answers
          2






          active

          oldest

          votes


















          0














          Ok, after i research a lot i found a solution



          - name: Install PHP && Packages
          become: true
          package:
          pkg: php{{ item.0.version }}-{{ item.1 }}
          state: latest
          with_subelements:
          - "{{ php }}"
          - libs
          notify: Restart Nginx


          This work for me. Also, i change my array:



          php:
          - version: 7.2
          libs:
          - cli
          - fpm
          - curl
          - mysql
          - pdo
          - zip
          - xml
          - gd
          - mbstring
          - tokenizer
          - ctype
          - json
          - version: 7.3
          libs:
          - cli
          - fpm
          - curl
          - mysql
          - pdo
          - zip
          - xml
          - gd
          - mbstring
          - tokenizer
          - ctype
          - json


          I let this here, maybe someone will found this helpful.






          share|improve this answer































            0














            An option would be to use include_tasks. See the hint below.



            tasks:
            - include_tasks: test.yml
            loop: "{{ php.versions }}"
            loop_control:
            loop_var: php_ver


            .



            > cat test.yml
            - debug: msg="{{ lookup('dict', php_ver).key }}"
            - debug: msg="{{ item }}"
            loop: "{{ lookup('dict', php_ver).value }}"


            Here is the playbook



            > cat test-31.yml
            ---
            - hosts: localhost
            gather_facts: no
            vars:
            php:
            versions:
            - 7.2:
            - cli
            - fpm
            - curl
            - mysql
            - pdo
            - zip
            - xml
            - gd
            - mbstring
            - tokenizer
            - ctype
            - json
            - 7.3:
            - cli
            - fpm
            - curl
            - mysql
            - pdo
            - zip
            - xml
            - gd
            - mbstring
            - tokenizer
            - ctype
            - json
            tasks:
            - include_tasks: test.yml
            loop: "{{ php.versions }}"
            loop_control:
            loop_var: php_ver


            Here is the output



            > ansible-playbook test-31.yml | grep msg
            "msg": "7.2"
            "msg": "cli"
            "msg": "fpm"
            "msg": "curl"
            "msg": "mysql"
            "msg": "pdo"
            "msg": "zip"
            "msg": "xml"
            "msg": "gd"
            "msg": "mbstring"
            "msg": "tokenizer"
            "msg": "ctype"
            "msg": "json"
            "msg": "7.3"
            "msg": "cli"
            "msg": "fpm"
            "msg": "curl"
            "msg": "mysql"
            "msg": "pdo"
            "msg": "zip"
            "msg": "xml"
            "msg": "gd"
            "msg": "mbstring"
            "msg": "tokenizer"
            "msg": "ctype"
            "msg": "json"





            share|improve this answer


























            • I get this error: {"msg": "An unhandled exception occurred while running the lookup plugin 'dict'. Error was a <class 'ansible.errors.AnsibleError'>, original message: with_dict expects a dict"}.

              – RedoColor
              Jan 20 at 12:13











            • I've added the playbook and the output.

              – Vladimir Botka
              Jan 20 at 13:01











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54269947%2fansible-multi-dimensional-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Ok, after i research a lot i found a solution



            - name: Install PHP && Packages
            become: true
            package:
            pkg: php{{ item.0.version }}-{{ item.1 }}
            state: latest
            with_subelements:
            - "{{ php }}"
            - libs
            notify: Restart Nginx


            This work for me. Also, i change my array:



            php:
            - version: 7.2
            libs:
            - cli
            - fpm
            - curl
            - mysql
            - pdo
            - zip
            - xml
            - gd
            - mbstring
            - tokenizer
            - ctype
            - json
            - version: 7.3
            libs:
            - cli
            - fpm
            - curl
            - mysql
            - pdo
            - zip
            - xml
            - gd
            - mbstring
            - tokenizer
            - ctype
            - json


            I let this here, maybe someone will found this helpful.






            share|improve this answer




























              0














              Ok, after i research a lot i found a solution



              - name: Install PHP && Packages
              become: true
              package:
              pkg: php{{ item.0.version }}-{{ item.1 }}
              state: latest
              with_subelements:
              - "{{ php }}"
              - libs
              notify: Restart Nginx


              This work for me. Also, i change my array:



              php:
              - version: 7.2
              libs:
              - cli
              - fpm
              - curl
              - mysql
              - pdo
              - zip
              - xml
              - gd
              - mbstring
              - tokenizer
              - ctype
              - json
              - version: 7.3
              libs:
              - cli
              - fpm
              - curl
              - mysql
              - pdo
              - zip
              - xml
              - gd
              - mbstring
              - tokenizer
              - ctype
              - json


              I let this here, maybe someone will found this helpful.






              share|improve this answer


























                0












                0








                0







                Ok, after i research a lot i found a solution



                - name: Install PHP && Packages
                become: true
                package:
                pkg: php{{ item.0.version }}-{{ item.1 }}
                state: latest
                with_subelements:
                - "{{ php }}"
                - libs
                notify: Restart Nginx


                This work for me. Also, i change my array:



                php:
                - version: 7.2
                libs:
                - cli
                - fpm
                - curl
                - mysql
                - pdo
                - zip
                - xml
                - gd
                - mbstring
                - tokenizer
                - ctype
                - json
                - version: 7.3
                libs:
                - cli
                - fpm
                - curl
                - mysql
                - pdo
                - zip
                - xml
                - gd
                - mbstring
                - tokenizer
                - ctype
                - json


                I let this here, maybe someone will found this helpful.






                share|improve this answer













                Ok, after i research a lot i found a solution



                - name: Install PHP && Packages
                become: true
                package:
                pkg: php{{ item.0.version }}-{{ item.1 }}
                state: latest
                with_subelements:
                - "{{ php }}"
                - libs
                notify: Restart Nginx


                This work for me. Also, i change my array:



                php:
                - version: 7.2
                libs:
                - cli
                - fpm
                - curl
                - mysql
                - pdo
                - zip
                - xml
                - gd
                - mbstring
                - tokenizer
                - ctype
                - json
                - version: 7.3
                libs:
                - cli
                - fpm
                - curl
                - mysql
                - pdo
                - zip
                - xml
                - gd
                - mbstring
                - tokenizer
                - ctype
                - json


                I let this here, maybe someone will found this helpful.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 20 at 12:38









                RedoColorRedoColor

                57111




                57111

























                    0














                    An option would be to use include_tasks. See the hint below.



                    tasks:
                    - include_tasks: test.yml
                    loop: "{{ php.versions }}"
                    loop_control:
                    loop_var: php_ver


                    .



                    > cat test.yml
                    - debug: msg="{{ lookup('dict', php_ver).key }}"
                    - debug: msg="{{ item }}"
                    loop: "{{ lookup('dict', php_ver).value }}"


                    Here is the playbook



                    > cat test-31.yml
                    ---
                    - hosts: localhost
                    gather_facts: no
                    vars:
                    php:
                    versions:
                    - 7.2:
                    - cli
                    - fpm
                    - curl
                    - mysql
                    - pdo
                    - zip
                    - xml
                    - gd
                    - mbstring
                    - tokenizer
                    - ctype
                    - json
                    - 7.3:
                    - cli
                    - fpm
                    - curl
                    - mysql
                    - pdo
                    - zip
                    - xml
                    - gd
                    - mbstring
                    - tokenizer
                    - ctype
                    - json
                    tasks:
                    - include_tasks: test.yml
                    loop: "{{ php.versions }}"
                    loop_control:
                    loop_var: php_ver


                    Here is the output



                    > ansible-playbook test-31.yml | grep msg
                    "msg": "7.2"
                    "msg": "cli"
                    "msg": "fpm"
                    "msg": "curl"
                    "msg": "mysql"
                    "msg": "pdo"
                    "msg": "zip"
                    "msg": "xml"
                    "msg": "gd"
                    "msg": "mbstring"
                    "msg": "tokenizer"
                    "msg": "ctype"
                    "msg": "json"
                    "msg": "7.3"
                    "msg": "cli"
                    "msg": "fpm"
                    "msg": "curl"
                    "msg": "mysql"
                    "msg": "pdo"
                    "msg": "zip"
                    "msg": "xml"
                    "msg": "gd"
                    "msg": "mbstring"
                    "msg": "tokenizer"
                    "msg": "ctype"
                    "msg": "json"





                    share|improve this answer


























                    • I get this error: {"msg": "An unhandled exception occurred while running the lookup plugin 'dict'. Error was a <class 'ansible.errors.AnsibleError'>, original message: with_dict expects a dict"}.

                      – RedoColor
                      Jan 20 at 12:13











                    • I've added the playbook and the output.

                      – Vladimir Botka
                      Jan 20 at 13:01
















                    0














                    An option would be to use include_tasks. See the hint below.



                    tasks:
                    - include_tasks: test.yml
                    loop: "{{ php.versions }}"
                    loop_control:
                    loop_var: php_ver


                    .



                    > cat test.yml
                    - debug: msg="{{ lookup('dict', php_ver).key }}"
                    - debug: msg="{{ item }}"
                    loop: "{{ lookup('dict', php_ver).value }}"


                    Here is the playbook



                    > cat test-31.yml
                    ---
                    - hosts: localhost
                    gather_facts: no
                    vars:
                    php:
                    versions:
                    - 7.2:
                    - cli
                    - fpm
                    - curl
                    - mysql
                    - pdo
                    - zip
                    - xml
                    - gd
                    - mbstring
                    - tokenizer
                    - ctype
                    - json
                    - 7.3:
                    - cli
                    - fpm
                    - curl
                    - mysql
                    - pdo
                    - zip
                    - xml
                    - gd
                    - mbstring
                    - tokenizer
                    - ctype
                    - json
                    tasks:
                    - include_tasks: test.yml
                    loop: "{{ php.versions }}"
                    loop_control:
                    loop_var: php_ver


                    Here is the output



                    > ansible-playbook test-31.yml | grep msg
                    "msg": "7.2"
                    "msg": "cli"
                    "msg": "fpm"
                    "msg": "curl"
                    "msg": "mysql"
                    "msg": "pdo"
                    "msg": "zip"
                    "msg": "xml"
                    "msg": "gd"
                    "msg": "mbstring"
                    "msg": "tokenizer"
                    "msg": "ctype"
                    "msg": "json"
                    "msg": "7.3"
                    "msg": "cli"
                    "msg": "fpm"
                    "msg": "curl"
                    "msg": "mysql"
                    "msg": "pdo"
                    "msg": "zip"
                    "msg": "xml"
                    "msg": "gd"
                    "msg": "mbstring"
                    "msg": "tokenizer"
                    "msg": "ctype"
                    "msg": "json"





                    share|improve this answer


























                    • I get this error: {"msg": "An unhandled exception occurred while running the lookup plugin 'dict'. Error was a <class 'ansible.errors.AnsibleError'>, original message: with_dict expects a dict"}.

                      – RedoColor
                      Jan 20 at 12:13











                    • I've added the playbook and the output.

                      – Vladimir Botka
                      Jan 20 at 13:01














                    0












                    0








                    0







                    An option would be to use include_tasks. See the hint below.



                    tasks:
                    - include_tasks: test.yml
                    loop: "{{ php.versions }}"
                    loop_control:
                    loop_var: php_ver


                    .



                    > cat test.yml
                    - debug: msg="{{ lookup('dict', php_ver).key }}"
                    - debug: msg="{{ item }}"
                    loop: "{{ lookup('dict', php_ver).value }}"


                    Here is the playbook



                    > cat test-31.yml
                    ---
                    - hosts: localhost
                    gather_facts: no
                    vars:
                    php:
                    versions:
                    - 7.2:
                    - cli
                    - fpm
                    - curl
                    - mysql
                    - pdo
                    - zip
                    - xml
                    - gd
                    - mbstring
                    - tokenizer
                    - ctype
                    - json
                    - 7.3:
                    - cli
                    - fpm
                    - curl
                    - mysql
                    - pdo
                    - zip
                    - xml
                    - gd
                    - mbstring
                    - tokenizer
                    - ctype
                    - json
                    tasks:
                    - include_tasks: test.yml
                    loop: "{{ php.versions }}"
                    loop_control:
                    loop_var: php_ver


                    Here is the output



                    > ansible-playbook test-31.yml | grep msg
                    "msg": "7.2"
                    "msg": "cli"
                    "msg": "fpm"
                    "msg": "curl"
                    "msg": "mysql"
                    "msg": "pdo"
                    "msg": "zip"
                    "msg": "xml"
                    "msg": "gd"
                    "msg": "mbstring"
                    "msg": "tokenizer"
                    "msg": "ctype"
                    "msg": "json"
                    "msg": "7.3"
                    "msg": "cli"
                    "msg": "fpm"
                    "msg": "curl"
                    "msg": "mysql"
                    "msg": "pdo"
                    "msg": "zip"
                    "msg": "xml"
                    "msg": "gd"
                    "msg": "mbstring"
                    "msg": "tokenizer"
                    "msg": "ctype"
                    "msg": "json"





                    share|improve this answer















                    An option would be to use include_tasks. See the hint below.



                    tasks:
                    - include_tasks: test.yml
                    loop: "{{ php.versions }}"
                    loop_control:
                    loop_var: php_ver


                    .



                    > cat test.yml
                    - debug: msg="{{ lookup('dict', php_ver).key }}"
                    - debug: msg="{{ item }}"
                    loop: "{{ lookup('dict', php_ver).value }}"


                    Here is the playbook



                    > cat test-31.yml
                    ---
                    - hosts: localhost
                    gather_facts: no
                    vars:
                    php:
                    versions:
                    - 7.2:
                    - cli
                    - fpm
                    - curl
                    - mysql
                    - pdo
                    - zip
                    - xml
                    - gd
                    - mbstring
                    - tokenizer
                    - ctype
                    - json
                    - 7.3:
                    - cli
                    - fpm
                    - curl
                    - mysql
                    - pdo
                    - zip
                    - xml
                    - gd
                    - mbstring
                    - tokenizer
                    - ctype
                    - json
                    tasks:
                    - include_tasks: test.yml
                    loop: "{{ php.versions }}"
                    loop_control:
                    loop_var: php_ver


                    Here is the output



                    > ansible-playbook test-31.yml | grep msg
                    "msg": "7.2"
                    "msg": "cli"
                    "msg": "fpm"
                    "msg": "curl"
                    "msg": "mysql"
                    "msg": "pdo"
                    "msg": "zip"
                    "msg": "xml"
                    "msg": "gd"
                    "msg": "mbstring"
                    "msg": "tokenizer"
                    "msg": "ctype"
                    "msg": "json"
                    "msg": "7.3"
                    "msg": "cli"
                    "msg": "fpm"
                    "msg": "curl"
                    "msg": "mysql"
                    "msg": "pdo"
                    "msg": "zip"
                    "msg": "xml"
                    "msg": "gd"
                    "msg": "mbstring"
                    "msg": "tokenizer"
                    "msg": "ctype"
                    "msg": "json"






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 20 at 13:00

























                    answered Jan 19 at 18:17









                    Vladimir BotkaVladimir Botka

                    1,6001410




                    1,6001410













                    • I get this error: {"msg": "An unhandled exception occurred while running the lookup plugin 'dict'. Error was a <class 'ansible.errors.AnsibleError'>, original message: with_dict expects a dict"}.

                      – RedoColor
                      Jan 20 at 12:13











                    • I've added the playbook and the output.

                      – Vladimir Botka
                      Jan 20 at 13:01



















                    • I get this error: {"msg": "An unhandled exception occurred while running the lookup plugin 'dict'. Error was a <class 'ansible.errors.AnsibleError'>, original message: with_dict expects a dict"}.

                      – RedoColor
                      Jan 20 at 12:13











                    • I've added the playbook and the output.

                      – Vladimir Botka
                      Jan 20 at 13:01

















                    I get this error: {"msg": "An unhandled exception occurred while running the lookup plugin 'dict'. Error was a <class 'ansible.errors.AnsibleError'>, original message: with_dict expects a dict"}.

                    – RedoColor
                    Jan 20 at 12:13





                    I get this error: {"msg": "An unhandled exception occurred while running the lookup plugin 'dict'. Error was a <class 'ansible.errors.AnsibleError'>, original message: with_dict expects a dict"}.

                    – RedoColor
                    Jan 20 at 12:13













                    I've added the playbook and the output.

                    – Vladimir Botka
                    Jan 20 at 13:01





                    I've added the playbook and the output.

                    – Vladimir Botka
                    Jan 20 at 13:01


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54269947%2fansible-multi-dimensional-array%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Plaza Victoria

                    In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                    How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...