song
song

Reputation: 27

Ansible judgment file directory

I want to do such a thing.In the presence of directory skip, otherwise will be created.

For example:

#!/bin/bash

for $i in 'a.test.com a.test.com c.test.com'
do
     if [ ! -e $i ]:
     then
         mkdir $i
     fi
done

How to use ansible-playbook implement the above code.

thanks

Upvotes: 1

Views: 96

Answers (2)

song
song

Reputation: 27

If the directory is created, or skip.

- name: test log dir
  shell: "test -e /data/{{item[0]}}/{{item[1]}} -o -h /data/{{item[0]}}/{{item[1]}}"
  ignore_errors: True
  with_nested:
    - ['logs', 'html']
    - ['a.test.com', 'b.test.com']
  register: dir_stats
  tags: check_log

- name: create log dir
  file: path=/data/{{item.item[0]}}/{{item.item[1]}} state=directory owner=apache group=apache recursive=yes
  with_items: dir_stats.results
  when: item.rc != 0
  tags: check_log

Upvotes: 0

300D7309EF17
300D7309EF17

Reputation: 24613

Looks like simple directory creation using the file module in a loop.

- name: make sure subdomain directories exist
  file: path=/opt/{{item}} state=directory recursive=yes
  with_items:
    - a.test.com
    - b.test.com
    - c.test.com

Upvotes: 1

Related Questions