Ansible - using for loops inside user module












0















I recently discovered the use of for loops in Ansible and was very excited about it.
Tries to use it inside the debug module and it worked superfine, but when I am trying to use the same inside "user" module, the control flow is not able to identify the "name" keyword of user module. Below is my poetry,



- hosts: testservers
tasks:
- name: Setting user facts
set_fact:
username: "{{ lookup('ini', 'name section=userDetails file=details.ini') }}"
userpass: "{{ lookup('ini', 'password section=userDetails file=details.ini') }}"

- name: User creation
become: true
# debug:
# msg: |
# {% for x,y in item.1,item.2 %}
# {{ x }} is the username and its password is {{ y }}
# {% endfor %}
# with_items:
# - { 1: "{{ username.split(',') }}", 2: "{{ userpass.split(',') }}" }
user: |
{% for x,y in item.1,item.2 %}
name: "{{ x }}"
password: "{{ y }}"
{% endfor %}
with_items:
- { 1: "{{ username.split(',') }}", 2: "{{ userpass.split(',') }}" }


Details.ini file contents below



#User basic details
[userDetails]
name=vasanth,vasanthnagkv
password=vasanth12,pass2


The commented part above works fine. but the uncommented part throws the below error



failed: [10.0.0.47] (item={1: [u'vasanth', u'vasanthnagkv'], 2: [u'vasanth12', u'pass2']}) => {
"changed": false,
"invocation": {
"module_args": {
"append": false,
"create_home": true,
"force": false,
"move_home": false,
"non_unique": false,
"remove": false,
"ssh_key_bits": 0,
"ssh_key_comment": "ansible-generated on APUA-02",
"ssh_key_type": "rsa",
"state": "present",
"system": false,
"update_password": "always"
}
},
"item": {
"1": [
"vasanth",
"vasanthnagkv"
],
"2": [
"vasanth12",
"pass2"
]
},
"msg": "missing required arguments: name"
}
to retry, use: --limit @/home/admin/ansiblePlaybooks/userCreation/userCreate.retry

PLAY RECAP ************************************************************************************************************************************************************************************
10.0.0.47 : ok=2 changed=0 unreachable=0 failed=1


Appreciate any kind of help here.










share|improve this question



























    0















    I recently discovered the use of for loops in Ansible and was very excited about it.
    Tries to use it inside the debug module and it worked superfine, but when I am trying to use the same inside "user" module, the control flow is not able to identify the "name" keyword of user module. Below is my poetry,



    - hosts: testservers
    tasks:
    - name: Setting user facts
    set_fact:
    username: "{{ lookup('ini', 'name section=userDetails file=details.ini') }}"
    userpass: "{{ lookup('ini', 'password section=userDetails file=details.ini') }}"

    - name: User creation
    become: true
    # debug:
    # msg: |
    # {% for x,y in item.1,item.2 %}
    # {{ x }} is the username and its password is {{ y }}
    # {% endfor %}
    # with_items:
    # - { 1: "{{ username.split(',') }}", 2: "{{ userpass.split(',') }}" }
    user: |
    {% for x,y in item.1,item.2 %}
    name: "{{ x }}"
    password: "{{ y }}"
    {% endfor %}
    with_items:
    - { 1: "{{ username.split(',') }}", 2: "{{ userpass.split(',') }}" }


    Details.ini file contents below



    #User basic details
    [userDetails]
    name=vasanth,vasanthnagkv
    password=vasanth12,pass2


    The commented part above works fine. but the uncommented part throws the below error



    failed: [10.0.0.47] (item={1: [u'vasanth', u'vasanthnagkv'], 2: [u'vasanth12', u'pass2']}) => {
    "changed": false,
    "invocation": {
    "module_args": {
    "append": false,
    "create_home": true,
    "force": false,
    "move_home": false,
    "non_unique": false,
    "remove": false,
    "ssh_key_bits": 0,
    "ssh_key_comment": "ansible-generated on APUA-02",
    "ssh_key_type": "rsa",
    "state": "present",
    "system": false,
    "update_password": "always"
    }
    },
    "item": {
    "1": [
    "vasanth",
    "vasanthnagkv"
    ],
    "2": [
    "vasanth12",
    "pass2"
    ]
    },
    "msg": "missing required arguments: name"
    }
    to retry, use: --limit @/home/admin/ansiblePlaybooks/userCreation/userCreate.retry

    PLAY RECAP ************************************************************************************************************************************************************************************
    10.0.0.47 : ok=2 changed=0 unreachable=0 failed=1


    Appreciate any kind of help here.










    share|improve this question

























      0












      0








      0


      0






      I recently discovered the use of for loops in Ansible and was very excited about it.
      Tries to use it inside the debug module and it worked superfine, but when I am trying to use the same inside "user" module, the control flow is not able to identify the "name" keyword of user module. Below is my poetry,



      - hosts: testservers
      tasks:
      - name: Setting user facts
      set_fact:
      username: "{{ lookup('ini', 'name section=userDetails file=details.ini') }}"
      userpass: "{{ lookup('ini', 'password section=userDetails file=details.ini') }}"

      - name: User creation
      become: true
      # debug:
      # msg: |
      # {% for x,y in item.1,item.2 %}
      # {{ x }} is the username and its password is {{ y }}
      # {% endfor %}
      # with_items:
      # - { 1: "{{ username.split(',') }}", 2: "{{ userpass.split(',') }}" }
      user: |
      {% for x,y in item.1,item.2 %}
      name: "{{ x }}"
      password: "{{ y }}"
      {% endfor %}
      with_items:
      - { 1: "{{ username.split(',') }}", 2: "{{ userpass.split(',') }}" }


      Details.ini file contents below



      #User basic details
      [userDetails]
      name=vasanth,vasanthnagkv
      password=vasanth12,pass2


      The commented part above works fine. but the uncommented part throws the below error



      failed: [10.0.0.47] (item={1: [u'vasanth', u'vasanthnagkv'], 2: [u'vasanth12', u'pass2']}) => {
      "changed": false,
      "invocation": {
      "module_args": {
      "append": false,
      "create_home": true,
      "force": false,
      "move_home": false,
      "non_unique": false,
      "remove": false,
      "ssh_key_bits": 0,
      "ssh_key_comment": "ansible-generated on APUA-02",
      "ssh_key_type": "rsa",
      "state": "present",
      "system": false,
      "update_password": "always"
      }
      },
      "item": {
      "1": [
      "vasanth",
      "vasanthnagkv"
      ],
      "2": [
      "vasanth12",
      "pass2"
      ]
      },
      "msg": "missing required arguments: name"
      }
      to retry, use: --limit @/home/admin/ansiblePlaybooks/userCreation/userCreate.retry

      PLAY RECAP ************************************************************************************************************************************************************************************
      10.0.0.47 : ok=2 changed=0 unreachable=0 failed=1


      Appreciate any kind of help here.










      share|improve this question














      I recently discovered the use of for loops in Ansible and was very excited about it.
      Tries to use it inside the debug module and it worked superfine, but when I am trying to use the same inside "user" module, the control flow is not able to identify the "name" keyword of user module. Below is my poetry,



      - hosts: testservers
      tasks:
      - name: Setting user facts
      set_fact:
      username: "{{ lookup('ini', 'name section=userDetails file=details.ini') }}"
      userpass: "{{ lookup('ini', 'password section=userDetails file=details.ini') }}"

      - name: User creation
      become: true
      # debug:
      # msg: |
      # {% for x,y in item.1,item.2 %}
      # {{ x }} is the username and its password is {{ y }}
      # {% endfor %}
      # with_items:
      # - { 1: "{{ username.split(',') }}", 2: "{{ userpass.split(',') }}" }
      user: |
      {% for x,y in item.1,item.2 %}
      name: "{{ x }}"
      password: "{{ y }}"
      {% endfor %}
      with_items:
      - { 1: "{{ username.split(',') }}", 2: "{{ userpass.split(',') }}" }


      Details.ini file contents below



      #User basic details
      [userDetails]
      name=vasanth,vasanthnagkv
      password=vasanth12,pass2


      The commented part above works fine. but the uncommented part throws the below error



      failed: [10.0.0.47] (item={1: [u'vasanth', u'vasanthnagkv'], 2: [u'vasanth12', u'pass2']}) => {
      "changed": false,
      "invocation": {
      "module_args": {
      "append": false,
      "create_home": true,
      "force": false,
      "move_home": false,
      "non_unique": false,
      "remove": false,
      "ssh_key_bits": 0,
      "ssh_key_comment": "ansible-generated on APUA-02",
      "ssh_key_type": "rsa",
      "state": "present",
      "system": false,
      "update_password": "always"
      }
      },
      "item": {
      "1": [
      "vasanth",
      "vasanthnagkv"
      ],
      "2": [
      "vasanth12",
      "pass2"
      ]
      },
      "msg": "missing required arguments: name"
      }
      to retry, use: --limit @/home/admin/ansiblePlaybooks/userCreation/userCreate.retry

      PLAY RECAP ************************************************************************************************************************************************************************************
      10.0.0.47 : ok=2 changed=0 unreachable=0 failed=1


      Appreciate any kind of help here.







      ansible






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 12:04









      Vasanth Nag K VVasanth Nag K V

      1,51641534




      1,51641534
























          1 Answer
          1






          active

          oldest

          votes


















          0














          This line user: | means your passing a string to user module using the block style indicator: https://yaml-multiline.info/)



          Since Ansible will just treat it as a string, you are not passing the required name parameter to the user module



          Try splitting the names after the lookup in the first task so you can have the names and passwords list:



          - name: Setting user facts
          set_fact:
          username: "{{ lookup('ini', 'name section=userDetails file=details.ini').split(',') }}"
          userpass: "{{ lookup('ini', 'password section=userDetails file=details.ini').split(',') }}"


          Once you have both the username and password list, you can use both variables by:



              - user:
          name: "{{ item }}"
          password: "{{ userpass[index] }}"
          loop: "{{ username }}"
          loop_control:
          index_var: index





          share|improve this answer
























          • How does 'item' point to username list In your example ? Shouldn't you pass a with statement ? And also, how does the index variable increment on each iteration.? Does it happen implicitly ?

            – Vasanth Nag K V
            Nov 24 '18 at 13:24













          • It's explained here: docs.ansible.com/ansible/2.5/user_guide/playbooks_loops.html: Before 2.5 Ansible mainly used the with_<lookup> keywords to create loops, the loop keyword is basically analogous to with_list.

            – Domingo Tamayo
            Nov 26 '18 at 7:09













          • "If you need to keep track of where you are in a loop, you can use the index_var option to loop control to specify a variable name to contain the current loop index." - this helped clear my doubt regarding loop control variable. thanks! this worked for me

            – Vasanth Nag K V
            Nov 26 '18 at 12:33











          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%2f53430625%2fansible-using-for-loops-inside-user-module%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









          0














          This line user: | means your passing a string to user module using the block style indicator: https://yaml-multiline.info/)



          Since Ansible will just treat it as a string, you are not passing the required name parameter to the user module



          Try splitting the names after the lookup in the first task so you can have the names and passwords list:



          - name: Setting user facts
          set_fact:
          username: "{{ lookup('ini', 'name section=userDetails file=details.ini').split(',') }}"
          userpass: "{{ lookup('ini', 'password section=userDetails file=details.ini').split(',') }}"


          Once you have both the username and password list, you can use both variables by:



              - user:
          name: "{{ item }}"
          password: "{{ userpass[index] }}"
          loop: "{{ username }}"
          loop_control:
          index_var: index





          share|improve this answer
























          • How does 'item' point to username list In your example ? Shouldn't you pass a with statement ? And also, how does the index variable increment on each iteration.? Does it happen implicitly ?

            – Vasanth Nag K V
            Nov 24 '18 at 13:24













          • It's explained here: docs.ansible.com/ansible/2.5/user_guide/playbooks_loops.html: Before 2.5 Ansible mainly used the with_<lookup> keywords to create loops, the loop keyword is basically analogous to with_list.

            – Domingo Tamayo
            Nov 26 '18 at 7:09













          • "If you need to keep track of where you are in a loop, you can use the index_var option to loop control to specify a variable name to contain the current loop index." - this helped clear my doubt regarding loop control variable. thanks! this worked for me

            – Vasanth Nag K V
            Nov 26 '18 at 12:33
















          0














          This line user: | means your passing a string to user module using the block style indicator: https://yaml-multiline.info/)



          Since Ansible will just treat it as a string, you are not passing the required name parameter to the user module



          Try splitting the names after the lookup in the first task so you can have the names and passwords list:



          - name: Setting user facts
          set_fact:
          username: "{{ lookup('ini', 'name section=userDetails file=details.ini').split(',') }}"
          userpass: "{{ lookup('ini', 'password section=userDetails file=details.ini').split(',') }}"


          Once you have both the username and password list, you can use both variables by:



              - user:
          name: "{{ item }}"
          password: "{{ userpass[index] }}"
          loop: "{{ username }}"
          loop_control:
          index_var: index





          share|improve this answer
























          • How does 'item' point to username list In your example ? Shouldn't you pass a with statement ? And also, how does the index variable increment on each iteration.? Does it happen implicitly ?

            – Vasanth Nag K V
            Nov 24 '18 at 13:24













          • It's explained here: docs.ansible.com/ansible/2.5/user_guide/playbooks_loops.html: Before 2.5 Ansible mainly used the with_<lookup> keywords to create loops, the loop keyword is basically analogous to with_list.

            – Domingo Tamayo
            Nov 26 '18 at 7:09













          • "If you need to keep track of where you are in a loop, you can use the index_var option to loop control to specify a variable name to contain the current loop index." - this helped clear my doubt regarding loop control variable. thanks! this worked for me

            – Vasanth Nag K V
            Nov 26 '18 at 12:33














          0












          0








          0







          This line user: | means your passing a string to user module using the block style indicator: https://yaml-multiline.info/)



          Since Ansible will just treat it as a string, you are not passing the required name parameter to the user module



          Try splitting the names after the lookup in the first task so you can have the names and passwords list:



          - name: Setting user facts
          set_fact:
          username: "{{ lookup('ini', 'name section=userDetails file=details.ini').split(',') }}"
          userpass: "{{ lookup('ini', 'password section=userDetails file=details.ini').split(',') }}"


          Once you have both the username and password list, you can use both variables by:



              - user:
          name: "{{ item }}"
          password: "{{ userpass[index] }}"
          loop: "{{ username }}"
          loop_control:
          index_var: index





          share|improve this answer













          This line user: | means your passing a string to user module using the block style indicator: https://yaml-multiline.info/)



          Since Ansible will just treat it as a string, you are not passing the required name parameter to the user module



          Try splitting the names after the lookup in the first task so you can have the names and passwords list:



          - name: Setting user facts
          set_fact:
          username: "{{ lookup('ini', 'name section=userDetails file=details.ini').split(',') }}"
          userpass: "{{ lookup('ini', 'password section=userDetails file=details.ini').split(',') }}"


          Once you have both the username and password list, you can use both variables by:



              - user:
          name: "{{ item }}"
          password: "{{ userpass[index] }}"
          loop: "{{ username }}"
          loop_control:
          index_var: index






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 12:36









          Domingo TamayoDomingo Tamayo

          12612




          12612













          • How does 'item' point to username list In your example ? Shouldn't you pass a with statement ? And also, how does the index variable increment on each iteration.? Does it happen implicitly ?

            – Vasanth Nag K V
            Nov 24 '18 at 13:24













          • It's explained here: docs.ansible.com/ansible/2.5/user_guide/playbooks_loops.html: Before 2.5 Ansible mainly used the with_<lookup> keywords to create loops, the loop keyword is basically analogous to with_list.

            – Domingo Tamayo
            Nov 26 '18 at 7:09













          • "If you need to keep track of where you are in a loop, you can use the index_var option to loop control to specify a variable name to contain the current loop index." - this helped clear my doubt regarding loop control variable. thanks! this worked for me

            – Vasanth Nag K V
            Nov 26 '18 at 12:33



















          • How does 'item' point to username list In your example ? Shouldn't you pass a with statement ? And also, how does the index variable increment on each iteration.? Does it happen implicitly ?

            – Vasanth Nag K V
            Nov 24 '18 at 13:24













          • It's explained here: docs.ansible.com/ansible/2.5/user_guide/playbooks_loops.html: Before 2.5 Ansible mainly used the with_<lookup> keywords to create loops, the loop keyword is basically analogous to with_list.

            – Domingo Tamayo
            Nov 26 '18 at 7:09













          • "If you need to keep track of where you are in a loop, you can use the index_var option to loop control to specify a variable name to contain the current loop index." - this helped clear my doubt regarding loop control variable. thanks! this worked for me

            – Vasanth Nag K V
            Nov 26 '18 at 12:33

















          How does 'item' point to username list In your example ? Shouldn't you pass a with statement ? And also, how does the index variable increment on each iteration.? Does it happen implicitly ?

          – Vasanth Nag K V
          Nov 24 '18 at 13:24







          How does 'item' point to username list In your example ? Shouldn't you pass a with statement ? And also, how does the index variable increment on each iteration.? Does it happen implicitly ?

          – Vasanth Nag K V
          Nov 24 '18 at 13:24















          It's explained here: docs.ansible.com/ansible/2.5/user_guide/playbooks_loops.html: Before 2.5 Ansible mainly used the with_<lookup> keywords to create loops, the loop keyword is basically analogous to with_list.

          – Domingo Tamayo
          Nov 26 '18 at 7:09







          It's explained here: docs.ansible.com/ansible/2.5/user_guide/playbooks_loops.html: Before 2.5 Ansible mainly used the with_<lookup> keywords to create loops, the loop keyword is basically analogous to with_list.

          – Domingo Tamayo
          Nov 26 '18 at 7:09















          "If you need to keep track of where you are in a loop, you can use the index_var option to loop control to specify a variable name to contain the current loop index." - this helped clear my doubt regarding loop control variable. thanks! this worked for me

          – Vasanth Nag K V
          Nov 26 '18 at 12:33





          "If you need to keep track of where you are in a loop, you can use the index_var option to loop control to specify a variable name to contain the current loop index." - this helped clear my doubt regarding loop control variable. thanks! this worked for me

          – Vasanth Nag K V
          Nov 26 '18 at 12:33


















          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%2f53430625%2fansible-using-for-loops-inside-user-module%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

          Wiesbaden

          Marschland

          Dieringhausen