Answers: <p>I have fixed updated error with the below module:</p>
  
  <pre><code>- name: Join system to AD 
    expect:
      command: /bin/bash -c "realm join --membership-software=adcli --user=username@EXAMPLE.COM --computer-ou="OU={{ xxx }},OU={{ xxxx }},OU=xxxx,OU=xxxxx,DC=xxxxxxxx,DC=xxxx" --os-name={{ ansible_distribution }} --os-version={{ ansible_distribution_version }} DCNAME"
      responses:
          Password for *: "{{ join_user_pass | b64decode }}"
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-04-15T17:54:53.273Z
Answers: 2020-04-15T17:54:53.273Z
Answers: Santosh Garole
Answers: <p>The <a href="https://docs.ansible.com/ansible/latest/modules/command_module.html#command-module" rel="nofollow noreferrer">Ansible documentation</a> states that the command module doesn't get a shell </p>
  
  <blockquote>
    <p>The command(s) will not be processed through the shell</p>
  </blockquote>
  
  <p>The <code>command</code> module takes the command as an argument, so you can't have  list as you've written there. You could do the same thing in a <code>loop:</code> with <code>command: {{ item }}</code> : </p>
  
  <pre><code>- name: "Run {{ item }}"
    command: "{{ item }}"
    loop:
      - cmd2
      - cmd2 
      - cmd3
  </code></pre>
  
  <p>You could also have several <code>command:</code> tasks instead.</p>
  
  <p>However, this is generally considered bad practice (E.g. E303 from <a href="https://docs.ansible.com/ansible-lint/rules/default_rules.html" rel="nofollow noreferrer">Ansible Lint rules</a>) because it tends to break idempotency. It would be better to decompose those commands into something that uses Ansible modules.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-04-03T06:56:35.423Z
Answers: Thank you so much it worked Anyways the commands which i would like to run those do not have ansible modules to replace it with.
Answers: Santosh Garole
Answers: CC BY-SA 4.0
Answers: 2020-04-13T05:18:42.347Z
Answers: Can you please help me with an updated error.
Answers: Santosh Garole
Answers: CC BY-SA 4.0
Answers: 2023-02-07T06:38:32.790Z
Answers: I use the shell module but I am facing a similar problem. Shouldn't the shell module take care of multiple commands separated by a newline? My snippet is mentioned here: https://devops.stackexchange.com/q/17382/38593
Answers: Skegg
Answers: CC BY-SA 4.0
Answers: 2020-04-02T14:36:33.793Z
Answers: 2020-04-02T14:36:33.793Z
Answers: Bruce Becker
Body: <p>I want to run couple of commands in ansible playbook I am trying below:</p>
  
  <pre><code> - name: Running multiple cmds
     command:
      - |
        cmd2
        cmd2
        cmd3
        cmd4
  </code></pre>
  
  <p>But i am getting an error on this task while running above code.</p>
  
  <p><strong>Update</strong>: I am trying to use below commands using command module but getting an error:</p>
  
  <pre><code>- name: install pexpect using pip
    shell: /bin/bash -c "pip install pexpect"
  
  - name: Join system to AD 
    expect:
      command: "{{ item }}"
      loop:
        - source /etc/os-release
        - realm join --membership-software=adcli --user=username@EXAMPLE.COM --computer-ou="OU={{ env }},OU={{ account }},OU=XXXX,OU=XXXXXX,DC=XXXXXXX,DC=XXXXXXX" --os-name="$PRETTY_NAME" --os-version="$VERSION" &lt;DCNAME&gt;
      responses:
          Password for *: "{{ username | b64decode }}"
  </code></pre>
  
  <p>Error: </p>
  
  <pre><code>TASK [adjoin : Join system to AD] ***********************************************************************************************************************************************************
  fatal: [localhost]: FAILED! =&gt; {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/tmp/ansiblepull/playbooks/roles/adjoin/tasks/main.yml': line 58, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Join system to AD\n  ^ here\n"}
  </code></pre>
  
  <p>Can anyone help me in solving this.</p>
  
  <p>I am getting an error while syntax-check as well.</p>
  
Tags: <ansible>
Title: How to run multiple commands with command module in Ansible
UserDisplayName: Santosh Garole
ThreadText: # How to run multiple commands with command module in Ansible
  
  (Asked by: username_0 on 2020-04-02)
  
  I want to run couple of commands in ansible playbook I am trying below:
  
  ```
   - name: Running multiple cmds
     command:
      - |
        cmd2
        cmd2
        cmd3
        cmd4
  
  ```
  
  But i am getting an error on this task while running above code.
  
  **Update**: I am trying to use below commands using command module but getting an error:
  
  ```
  - name: install pexpect using pip
    shell: /bin/bash -c "pip install pexpect"
  
  - name: Join system to AD 
    expect:
      command: "{{ item }}"
      loop:
        - source /etc/os-release
        - realm join --membership-software=adcli --user=username@EXAMPLE.COM --computer-ou="OU={{ env }},OU={{ account }},OU=XXXX,OU=XXXXXX,DC=XXXXXXX,DC=XXXXXXX" --os-name="$PRETTY_NAME" --os-version="$VERSION" <DCNAME>
      responses:
          Password for *: "{{ username | b64decode }}"
  
  ```
  
  Error:
  
  ```
  TASK [adjoin : Join system to AD] ***********************************************************************************************************************************************************
  fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/tmp/ansiblepull/playbooks/roles/adjoin/tasks/main.yml': line 58, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Join system to AD\n  ^ here\n"}
  
  ```
  
  Can anyone help me in solving this.
  
  I am getting an error while syntax-check as well.
  
  **Tags:** ansible
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2020-04-15. Score: 0 | Accepted answer
  
  I have fixed updated error with the below module:
  
  ```
  - name: Join system to AD 
    expect:
      command: /bin/bash -c "realm join --membership-software=adcli --user=username@EXAMPLE.COM --computer-ou="OU={{ xxx }},OU={{ xxxx }},OU=xxxx,OU=xxxxx,DC=xxxxxxxx,DC=xxxx" --os-name={{ ansible_distribution }} --os-version={{ ansible_distribution_version }} DCNAME"
      responses:
          Password for *: "{{ join_user_pass | b64decode }}"
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2020-04-02. Score: 4
  
  The [Ansible documentation](https://docs.ansible.com/ansible/latest/modules/command_module.html#command-module) states that the command module doesn't get a shell
  
  > The command(s) will not be processed through the shell
  
  The `command` module takes the command as an argument, so you can't have list as you've written there. You could do the same thing in a `loop:` with `command: {{ item }}` :
  
  ```
  - name: "Run {{ item }}"
    command: "{{ item }}"
    loop:
      - cmd2
      - cmd2 
      - cmd3
  
  ```
  
  You could also have several `command:` tasks instead.
  
  However, this is generally considered bad practice (E.g. E303 from [Ansible Lint rules](https://docs.ansible.com/ansible-lint/rules/default_rules.html)) because it tends to break idempotency. It would be better to decompose those commands into something that uses Ansible modules.
  
  > **Comments:**
  > **username_0**: Thank you so much it worked Anyways the commands which i would like to run those do not have ansible modules to replace it with.  
  > **username_0**: Can you please help me with an updated error.  
  > **username_2**: I use the shell module but I am facing a similar problem. Shouldn't the shell module take care of multiple commands separated by a newline? My snippet is mentioned here: https://devops.stackexchange.com/q/17382/38593  
  
  ---
  
  