Loop_control index_var in Ansible 2.0









up vote
0
down vote

favorite












I have a playbook that is compatible with Ansible 2.6. This playbook uses the loop_control module to construct a string.



vars:
app_config:
attr1 :
- "1"
nexatt :
- "b"
...
- set_fact:
app_properties: ""

- name: Reading the Specific Configuration
set_fact:
app_properties: " app_properties ternary(',','') item.key = item.value[0] "
loop: "dict2items "
loop_control:
index_var: index


The string is then passed to a script as an option:



- name: Create Configurations
command: " dir / script
item "
with_items:
- " app_properties "


Is there a way to do this so that it is Ansible 2.0 compatible (given that Ansible 2.0 does not have loop_control)?
(I have another setup that requires Ansible 2.0 and needs this playbook. I cannot upgrade to Ansible 2.6).










share|improve this question

























    up vote
    0
    down vote

    favorite












    I have a playbook that is compatible with Ansible 2.6. This playbook uses the loop_control module to construct a string.



    vars:
    app_config:
    attr1 :
    - "1"
    nexatt :
    - "b"
    ...
    - set_fact:
    app_properties: ""

    - name: Reading the Specific Configuration
    set_fact:
    app_properties: " app_properties ternary(',','') item.key = item.value[0] "
    loop: "dict2items "
    loop_control:
    index_var: index


    The string is then passed to a script as an option:



    - name: Create Configurations
    command: " dir / script
    item "
    with_items:
    - " app_properties "


    Is there a way to do this so that it is Ansible 2.0 compatible (given that Ansible 2.0 does not have loop_control)?
    (I have another setup that requires Ansible 2.0 and needs this playbook. I cannot upgrade to Ansible 2.6).










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a playbook that is compatible with Ansible 2.6. This playbook uses the loop_control module to construct a string.



      vars:
      app_config:
      attr1 :
      - "1"
      nexatt :
      - "b"
      ...
      - set_fact:
      app_properties: ""

      - name: Reading the Specific Configuration
      set_fact:
      app_properties: " app_properties ternary(',','') item.key = item.value[0] "
      loop: "dict2items "
      loop_control:
      index_var: index


      The string is then passed to a script as an option:



      - name: Create Configurations
      command: " dir / script
      item "
      with_items:
      - " app_properties "


      Is there a way to do this so that it is Ansible 2.0 compatible (given that Ansible 2.0 does not have loop_control)?
      (I have another setup that requires Ansible 2.0 and needs this playbook. I cannot upgrade to Ansible 2.6).










      share|improve this question













      I have a playbook that is compatible with Ansible 2.6. This playbook uses the loop_control module to construct a string.



      vars:
      app_config:
      attr1 :
      - "1"
      nexatt :
      - "b"
      ...
      - set_fact:
      app_properties: ""

      - name: Reading the Specific Configuration
      set_fact:
      app_properties: " app_properties ternary(',','') item.key = item.value[0] "
      loop: "dict2items "
      loop_control:
      index_var: index


      The string is then passed to a script as an option:



      - name: Create Configurations
      command: " dir / script
      item "
      with_items:
      - " app_properties "


      Is there a way to do this so that it is Ansible 2.0 compatible (given that Ansible 2.0 does not have loop_control)?
      (I have another setup that requires Ansible 2.0 and needs this playbook. I cannot upgrade to Ansible 2.6).







      ansible






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 3:02









      SSF

      173115




      173115






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          If you want a direct equivalent, you can use the with_indexed_items loop construct to iterate over a list along with an index value. Because the with_* loops perform an implicit flattening of their input, you'll need to wrap your list in a list, so that the final playbook looks like this:



          - hosts: localhost
          gather_facts: false
          vars:
          app_config:
          attr1:
          - "1"
          nexatt:
          - "b"
          tasks:
          - name: Reading the Specific Configuration
          set_fact:
          app_properties: "default('') ternary(',','') item.1.0 = item.1.1.0 "
          with_indexed_items: [" app_config.items() "]

          - debug:
          var: app_properties


          I've dropped your task that initializes app_properties in favor of
          using Ansible's default filter.



          For what you're doing you don't even need to use a loop
          index. For example, if you're willing to live with an additional
          set_fact task, you could do this:



          - hosts: localhost
          gather_facts: false
          vars:
          app_config:
          attr1:
          - "1"
          nexatt:
          - "b"
          tasks:
          - name: Reading the Specific Configuration
          set_fact:
          app_properties_list: "default() + ['%s=%s' % (item.0, item.1.0)] "
          with_items: [" app_config.items() "]

          - name: Create comma-delimieted app_properties list
          set_fact:
          app_properties: " ','.join(app_properties_list) "

          - debug:
          var: app_properties


          The above will work with Ansible 2.0.0.2 or later (and probably
          earlier!). There are probably other ways of tackling this problems as
          well (such as a template % for %...% endfor % loop).






          share|improve this answer




















          • That is phenomenal. Thank you for your help. This is great solution. I like the alternative solution.
            – SSF
            Nov 12 at 5:54










          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%2f53255453%2floop-control-index-var-in-ansible-2-0%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








          up vote
          1
          down vote



          accepted










          If you want a direct equivalent, you can use the with_indexed_items loop construct to iterate over a list along with an index value. Because the with_* loops perform an implicit flattening of their input, you'll need to wrap your list in a list, so that the final playbook looks like this:



          - hosts: localhost
          gather_facts: false
          vars:
          app_config:
          attr1:
          - "1"
          nexatt:
          - "b"
          tasks:
          - name: Reading the Specific Configuration
          set_fact:
          app_properties: "default('') ternary(',','') item.1.0 = item.1.1.0 "
          with_indexed_items: [" app_config.items() "]

          - debug:
          var: app_properties


          I've dropped your task that initializes app_properties in favor of
          using Ansible's default filter.



          For what you're doing you don't even need to use a loop
          index. For example, if you're willing to live with an additional
          set_fact task, you could do this:



          - hosts: localhost
          gather_facts: false
          vars:
          app_config:
          attr1:
          - "1"
          nexatt:
          - "b"
          tasks:
          - name: Reading the Specific Configuration
          set_fact:
          app_properties_list: "default() + ['%s=%s' % (item.0, item.1.0)] "
          with_items: [" app_config.items() "]

          - name: Create comma-delimieted app_properties list
          set_fact:
          app_properties: " ','.join(app_properties_list) "

          - debug:
          var: app_properties


          The above will work with Ansible 2.0.0.2 or later (and probably
          earlier!). There are probably other ways of tackling this problems as
          well (such as a template % for %...% endfor % loop).






          share|improve this answer




















          • That is phenomenal. Thank you for your help. This is great solution. I like the alternative solution.
            – SSF
            Nov 12 at 5:54














          up vote
          1
          down vote



          accepted










          If you want a direct equivalent, you can use the with_indexed_items loop construct to iterate over a list along with an index value. Because the with_* loops perform an implicit flattening of their input, you'll need to wrap your list in a list, so that the final playbook looks like this:



          - hosts: localhost
          gather_facts: false
          vars:
          app_config:
          attr1:
          - "1"
          nexatt:
          - "b"
          tasks:
          - name: Reading the Specific Configuration
          set_fact:
          app_properties: "default('') ternary(',','') item.1.0 = item.1.1.0 "
          with_indexed_items: [" app_config.items() "]

          - debug:
          var: app_properties


          I've dropped your task that initializes app_properties in favor of
          using Ansible's default filter.



          For what you're doing you don't even need to use a loop
          index. For example, if you're willing to live with an additional
          set_fact task, you could do this:



          - hosts: localhost
          gather_facts: false
          vars:
          app_config:
          attr1:
          - "1"
          nexatt:
          - "b"
          tasks:
          - name: Reading the Specific Configuration
          set_fact:
          app_properties_list: "default() + ['%s=%s' % (item.0, item.1.0)] "
          with_items: [" app_config.items() "]

          - name: Create comma-delimieted app_properties list
          set_fact:
          app_properties: " ','.join(app_properties_list) "

          - debug:
          var: app_properties


          The above will work with Ansible 2.0.0.2 or later (and probably
          earlier!). There are probably other ways of tackling this problems as
          well (such as a template % for %...% endfor % loop).






          share|improve this answer




















          • That is phenomenal. Thank you for your help. This is great solution. I like the alternative solution.
            – SSF
            Nov 12 at 5:54












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          If you want a direct equivalent, you can use the with_indexed_items loop construct to iterate over a list along with an index value. Because the with_* loops perform an implicit flattening of their input, you'll need to wrap your list in a list, so that the final playbook looks like this:



          - hosts: localhost
          gather_facts: false
          vars:
          app_config:
          attr1:
          - "1"
          nexatt:
          - "b"
          tasks:
          - name: Reading the Specific Configuration
          set_fact:
          app_properties: "default('') ternary(',','') item.1.0 = item.1.1.0 "
          with_indexed_items: [" app_config.items() "]

          - debug:
          var: app_properties


          I've dropped your task that initializes app_properties in favor of
          using Ansible's default filter.



          For what you're doing you don't even need to use a loop
          index. For example, if you're willing to live with an additional
          set_fact task, you could do this:



          - hosts: localhost
          gather_facts: false
          vars:
          app_config:
          attr1:
          - "1"
          nexatt:
          - "b"
          tasks:
          - name: Reading the Specific Configuration
          set_fact:
          app_properties_list: "default() + ['%s=%s' % (item.0, item.1.0)] "
          with_items: [" app_config.items() "]

          - name: Create comma-delimieted app_properties list
          set_fact:
          app_properties: " ','.join(app_properties_list) "

          - debug:
          var: app_properties


          The above will work with Ansible 2.0.0.2 or later (and probably
          earlier!). There are probably other ways of tackling this problems as
          well (such as a template % for %...% endfor % loop).






          share|improve this answer












          If you want a direct equivalent, you can use the with_indexed_items loop construct to iterate over a list along with an index value. Because the with_* loops perform an implicit flattening of their input, you'll need to wrap your list in a list, so that the final playbook looks like this:



          - hosts: localhost
          gather_facts: false
          vars:
          app_config:
          attr1:
          - "1"
          nexatt:
          - "b"
          tasks:
          - name: Reading the Specific Configuration
          set_fact:
          app_properties: "default('') ternary(',','') item.1.0 = item.1.1.0 "
          with_indexed_items: [" app_config.items() "]

          - debug:
          var: app_properties


          I've dropped your task that initializes app_properties in favor of
          using Ansible's default filter.



          For what you're doing you don't even need to use a loop
          index. For example, if you're willing to live with an additional
          set_fact task, you could do this:



          - hosts: localhost
          gather_facts: false
          vars:
          app_config:
          attr1:
          - "1"
          nexatt:
          - "b"
          tasks:
          - name: Reading the Specific Configuration
          set_fact:
          app_properties_list: "default() + ['%s=%s' % (item.0, item.1.0)] "
          with_items: [" app_config.items() "]

          - name: Create comma-delimieted app_properties list
          set_fact:
          app_properties: " ','.join(app_properties_list) "

          - debug:
          var: app_properties


          The above will work with Ansible 2.0.0.2 or later (and probably
          earlier!). There are probably other ways of tackling this problems as
          well (such as a template % for %...% endfor % loop).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 4:11









          larsks

          113k18184194




          113k18184194











          • That is phenomenal. Thank you for your help. This is great solution. I like the alternative solution.
            – SSF
            Nov 12 at 5:54
















          • That is phenomenal. Thank you for your help. This is great solution. I like the alternative solution.
            – SSF
            Nov 12 at 5:54















          That is phenomenal. Thank you for your help. This is great solution. I like the alternative solution.
          – SSF
          Nov 12 at 5:54




          That is phenomenal. Thank you for your help. This is great solution. I like the alternative solution.
          – SSF
          Nov 12 at 5:54

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53255453%2floop-control-index-var-in-ansible-2-0%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







          這個網誌中的熱門文章

          What does pagestruct do in Eviews?

          Dutch intervention in Lombok and Karangasem

          Channel Islands