Run an imported Ansible playbook for each unique value in a set of host vars












0















I've got a playbook which needs to run against my entire inventory, with a list of hostnames as an extra variable (target_hosts).



The hosts in target_hosts all have a group_id hostvar defined on them. I use the whole inventory because some ancillary hosts which correspond to the group_id var need per-group configuration to match in one section.



There will often be multiple group_id values associated with the hosts in the target_hosts list. I need to select the correct inventory group of ancillary hosts and import/run a playbook to configure both sets of servers partway through the main playbook.



This is what I currently do:



include_playbook: group-configure.yaml
vars:
src_hosts: "group-{{ group_id }}-ancillary-1"
dest_hosts: "{{ target_hosts }}"


I currently have to manually separate the target_hosts by group_id manually, then run the main playbook once for each. This has tons of unnecessary overhead.



What I really want to execute is this:



for each group of hosts from `target_hosts` with the same `group_id` hostvar:
import and run group-configure.yaml with:
src_hosts: "ancillary-{{ group_id }}"
target_hosts: restricted to those with that value of `group_id`'


How can I do this? If the current way this is structured won't work, what's the best alternative approach?










share|improve this question



























    0















    I've got a playbook which needs to run against my entire inventory, with a list of hostnames as an extra variable (target_hosts).



    The hosts in target_hosts all have a group_id hostvar defined on them. I use the whole inventory because some ancillary hosts which correspond to the group_id var need per-group configuration to match in one section.



    There will often be multiple group_id values associated with the hosts in the target_hosts list. I need to select the correct inventory group of ancillary hosts and import/run a playbook to configure both sets of servers partway through the main playbook.



    This is what I currently do:



    include_playbook: group-configure.yaml
    vars:
    src_hosts: "group-{{ group_id }}-ancillary-1"
    dest_hosts: "{{ target_hosts }}"


    I currently have to manually separate the target_hosts by group_id manually, then run the main playbook once for each. This has tons of unnecessary overhead.



    What I really want to execute is this:



    for each group of hosts from `target_hosts` with the same `group_id` hostvar:
    import and run group-configure.yaml with:
    src_hosts: "ancillary-{{ group_id }}"
    target_hosts: restricted to those with that value of `group_id`'


    How can I do this? If the current way this is structured won't work, what's the best alternative approach?










    share|improve this question

























      0












      0








      0








      I've got a playbook which needs to run against my entire inventory, with a list of hostnames as an extra variable (target_hosts).



      The hosts in target_hosts all have a group_id hostvar defined on them. I use the whole inventory because some ancillary hosts which correspond to the group_id var need per-group configuration to match in one section.



      There will often be multiple group_id values associated with the hosts in the target_hosts list. I need to select the correct inventory group of ancillary hosts and import/run a playbook to configure both sets of servers partway through the main playbook.



      This is what I currently do:



      include_playbook: group-configure.yaml
      vars:
      src_hosts: "group-{{ group_id }}-ancillary-1"
      dest_hosts: "{{ target_hosts }}"


      I currently have to manually separate the target_hosts by group_id manually, then run the main playbook once for each. This has tons of unnecessary overhead.



      What I really want to execute is this:



      for each group of hosts from `target_hosts` with the same `group_id` hostvar:
      import and run group-configure.yaml with:
      src_hosts: "ancillary-{{ group_id }}"
      target_hosts: restricted to those with that value of `group_id`'


      How can I do this? If the current way this is structured won't work, what's the best alternative approach?










      share|improve this question














      I've got a playbook which needs to run against my entire inventory, with a list of hostnames as an extra variable (target_hosts).



      The hosts in target_hosts all have a group_id hostvar defined on them. I use the whole inventory because some ancillary hosts which correspond to the group_id var need per-group configuration to match in one section.



      There will often be multiple group_id values associated with the hosts in the target_hosts list. I need to select the correct inventory group of ancillary hosts and import/run a playbook to configure both sets of servers partway through the main playbook.



      This is what I currently do:



      include_playbook: group-configure.yaml
      vars:
      src_hosts: "group-{{ group_id }}-ancillary-1"
      dest_hosts: "{{ target_hosts }}"


      I currently have to manually separate the target_hosts by group_id manually, then run the main playbook once for each. This has tons of unnecessary overhead.



      What I really want to execute is this:



      for each group of hosts from `target_hosts` with the same `group_id` hostvar:
      import and run group-configure.yaml with:
      src_hosts: "ancillary-{{ group_id }}"
      target_hosts: restricted to those with that value of `group_id`'


      How can I do this? If the current way this is structured won't work, what's the best alternative approach?







      ansible ansible-facts






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 31 '18 at 22:55









      Captain BlammoCaptain Blammo

      1,4901630




      1,4901630
























          1 Answer
          1






          active

          oldest

          votes


















          2














          I am pretty sure the add_host: combined with groupby is what you are looking for, which will allow you to roll up those hosts by their attribute, and then run the playbook against them as if that group was defined already:



          - hosts: localhost
          connection: local
          gather_facts: no
          become: no
          vars:
          list_of_name_groups: >-
          {%- set results = -%}
          {%- for g_id, items in (dict(hostvars) | dict2items | groupby("value.group_id")) -%}
          {%- for hostname in (items | map(attribute="key") | list) -%}
          {%- set _ = results.append({"group_id": g_id, "hostname": hostname}) -%}
          {%- endfor -%}
          {%- endfor -%}
          {{ results }}
          tasks:
          - add_host:
          name: '{{ item.hostname }}'
          groups: ancillary-{{ item.group_id }}
          with_items: '{{ list_of_name_groups }}'

          - hosts: ancillary-my-awesome-groupid
          # etc etc





          share|improve this answer























            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%2f53991992%2frun-an-imported-ansible-playbook-for-each-unique-value-in-a-set-of-host-vars%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            I am pretty sure the add_host: combined with groupby is what you are looking for, which will allow you to roll up those hosts by their attribute, and then run the playbook against them as if that group was defined already:



            - hosts: localhost
            connection: local
            gather_facts: no
            become: no
            vars:
            list_of_name_groups: >-
            {%- set results = -%}
            {%- for g_id, items in (dict(hostvars) | dict2items | groupby("value.group_id")) -%}
            {%- for hostname in (items | map(attribute="key") | list) -%}
            {%- set _ = results.append({"group_id": g_id, "hostname": hostname}) -%}
            {%- endfor -%}
            {%- endfor -%}
            {{ results }}
            tasks:
            - add_host:
            name: '{{ item.hostname }}'
            groups: ancillary-{{ item.group_id }}
            with_items: '{{ list_of_name_groups }}'

            - hosts: ancillary-my-awesome-groupid
            # etc etc





            share|improve this answer




























              2














              I am pretty sure the add_host: combined with groupby is what you are looking for, which will allow you to roll up those hosts by their attribute, and then run the playbook against them as if that group was defined already:



              - hosts: localhost
              connection: local
              gather_facts: no
              become: no
              vars:
              list_of_name_groups: >-
              {%- set results = -%}
              {%- for g_id, items in (dict(hostvars) | dict2items | groupby("value.group_id")) -%}
              {%- for hostname in (items | map(attribute="key") | list) -%}
              {%- set _ = results.append({"group_id": g_id, "hostname": hostname}) -%}
              {%- endfor -%}
              {%- endfor -%}
              {{ results }}
              tasks:
              - add_host:
              name: '{{ item.hostname }}'
              groups: ancillary-{{ item.group_id }}
              with_items: '{{ list_of_name_groups }}'

              - hosts: ancillary-my-awesome-groupid
              # etc etc





              share|improve this answer


























                2












                2








                2







                I am pretty sure the add_host: combined with groupby is what you are looking for, which will allow you to roll up those hosts by their attribute, and then run the playbook against them as if that group was defined already:



                - hosts: localhost
                connection: local
                gather_facts: no
                become: no
                vars:
                list_of_name_groups: >-
                {%- set results = -%}
                {%- for g_id, items in (dict(hostvars) | dict2items | groupby("value.group_id")) -%}
                {%- for hostname in (items | map(attribute="key") | list) -%}
                {%- set _ = results.append({"group_id": g_id, "hostname": hostname}) -%}
                {%- endfor -%}
                {%- endfor -%}
                {{ results }}
                tasks:
                - add_host:
                name: '{{ item.hostname }}'
                groups: ancillary-{{ item.group_id }}
                with_items: '{{ list_of_name_groups }}'

                - hosts: ancillary-my-awesome-groupid
                # etc etc





                share|improve this answer













                I am pretty sure the add_host: combined with groupby is what you are looking for, which will allow you to roll up those hosts by their attribute, and then run the playbook against them as if that group was defined already:



                - hosts: localhost
                connection: local
                gather_facts: no
                become: no
                vars:
                list_of_name_groups: >-
                {%- set results = -%}
                {%- for g_id, items in (dict(hostvars) | dict2items | groupby("value.group_id")) -%}
                {%- for hostname in (items | map(attribute="key") | list) -%}
                {%- set _ = results.append({"group_id": g_id, "hostname": hostname}) -%}
                {%- endfor -%}
                {%- endfor -%}
                {{ results }}
                tasks:
                - add_host:
                name: '{{ item.hostname }}'
                groups: ancillary-{{ item.group_id }}
                with_items: '{{ list_of_name_groups }}'

                - hosts: ancillary-my-awesome-groupid
                # etc etc






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 1 at 21:15









                Matthew L DanielMatthew L Daniel

                8,89612728




                8,89612728
































                    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%2f53991992%2frun-an-imported-ansible-playbook-for-each-unique-value-in-a-set-of-host-vars%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

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'