I want to run couple of commands in ansible playbook I am trying below:

 - name: Running multiple cmds
    - |

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 
    command: "{{ item }}"
      - source /etc/os-release
      - realm join --membership-software=adcli [email protected] --computer-ou="OU={{ env }},OU={{ account }},OU=XXXX,OU=XXXXXX,DC=XXXXXXX,DC=XXXXXXX" --os-name="$PRETTY_NAME" --os-version="$VERSION" <DCNAME>
        Password for *: "{{ username | b64decode }}"


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.

2 Answers 2


The Ansible documentation 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 }}"
    - 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) because it tends to break idempotency. It would be better to decompose those commands into something that uses Ansible modules.

  • 1
    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. Apr 3, 2020 at 6:56
  • Can you please help me with an updated error. Apr 13, 2020 at 5:18
  • 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: devops.stackexchange.com/q/17382/38593
    – Skegg
    Feb 7, 2023 at 6:38

I have fixed updated error with the below module:

- name: Join system to AD 
    command: /bin/bash -c "realm join --membership-software=adcli [email protected] --computer-ou="OU={{ xxx }},OU={{ xxxx }},OU=xxxx,OU=xxxxx,DC=xxxxxxxx,DC=xxxx" --os-name={{ ansible_distribution }} --os-version={{ ansible_distribution_version }} DCNAME"
        Password for *: "{{ join_user_pass | b64decode }}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.