run two command in one task

386 views
Skip to first unread message

Roy

unread,
Jul 13, 2015, 10:58:13 AM7/13/15
to ansible...@googlegroups.com
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']"
  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



Paul Markham

unread,
Jul 19, 2015, 12:27:51 AM7/19/15
to ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages