# run two command in one task

**URL:** <https://forum.ansible.com/t/run-two-command-in-one-task/18743>\
**Category:** Ansible Project\
**Created:** [July 13, 2015, 2:58pm UTC](https://forum.ansible.com/t/run-two-command-in-one-task/18743 "2015-07-13T14:58:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@Roy](https://forum.ansible.com/u/Roy)\
**Post date:** [July 13, 2015, 2:58pm UTC](https://forum.ansible.com/t/run-two-command-in-one-task/18743/1 "2015-07-13T14:58:13Z")

</div>

Hi,

I was trying to write simple task in ansible to create HDFS user hone directory if it’s not exist. So following is the task I wrote

> - name: create user home dir in HDFS
> 
> shell: shell: “hadoop fs -test -d /user/{{ item.name }} || (hadoop fs -mkdir /user/{{ item.name }} && hadoop fs -chown {{ item.name }} /user/{{ item.name }})”
> 
> when: “‘hdfsuser’ in {{ item.groups }} and ‘{{ inventory\_hostname }}’ in [‘[hadoop-client01.dev.abc.com](http://hadoop-client01.dev.abc.com)’]”
> 
> with\_items: ssh\_users
> 
> remote\_user: hdfs
> 
> tags:
> 
> - hadoop

But I want to make it more simplified by breaking the shell command into two.

so is it possible to have conditional dependent two commands in one single task.

or is there better way to do this ?

Thanks

---

<div class="post-metadata">

**Author:** ![Paul\_Markham](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@Paul\_Markham](https://forum.ansible.com/u/Paul_Markham)\
**Post date:** [July 19, 2015, 4:27am UTC](https://forum.ansible.com/t/run-two-command-in-one-task/18743/2 "2015-07-19T04:27:50Z")

</div>

Maybe use register variables; something like:

- name: test user home dir in HDFS  
shell: hadoop fs -test -d /user/{{ item.name }}  
register: hadoop\_dir

- name: create user home dir in HDFS  
shell: hadoop fs -mkdir /user/{{ item.name }}  
when: hadoop\_dir|failed

- name: chown user home dir in HDFS  
hadoop fs -chown {{ item.name }} /user/{{ item.name }})  
when: hadoop\_dir|failed
