2

AIM : To be able to create directories by passing a list as argument in ansible

  • I am able to do so while running the playbook on localhost
  • However, but when I run it on remote host, it fails as the lookup happens on my machine and not the remote machine.
  • I want to be able to create directories by passing a list var_list_dirs which i can set in the playbook. This way my roles does not need to change no matter how many playbooks want to create the directories.

Any help would be appreciated.

playbook-create-directory.yaml

################ SAMPLE USAGE #################################################################################
#         ansible-playbook playbook-create-directory.yaml --tags="set-user,create-dir"
###############################################################################################################

- name: create directory
  hosts: gcp
  become: true
  vars:
### CREATE USERS
    var_user: "test"
    var_group: "test"
### CREATE DIRECTORIES
    var_context: "test"
    var_mount_path: "/data"
    context: "{{ var_context }}"
    var_context_opt_dir: "/opt/{{ context }}"
    var_context_config_dir: "{{ var_context_opt_dir }}/config"
    var_context_log_dir: "{{ var_mount_path }}/log/{{ context }}"
    var_context_data_dir: "{{ var_mount_path }}/var/lib/{{ context }}"
    var_context_backup_dir: "{{ var_mount_path }}/var/{{ context }}-backup"
    var_list_dirs: "{{ var_context_opt_dir }}
    {{ var_context_config_dir }}
    {{ var_context_log_dir }}
    {{ var_context_data_dir }}
    {{ var_context_backup_dir }}"
  roles:
    - { role: user,                        tags: [ 'user' ] }
    - { role: directory,                   tags: [ 'directory'] }

main.yaml

---
## https://stackoverflow.com/questions/1271222/replace-whitespace-with-a-comma-in-a-text-file-in-linux
- name: get list of directories
  shell: |
    echo "{{ var_list_dirs }}" |  tr " " "\n" > /tmp/dirs.txt
  tags:
    - create-dir


- name: create dirs
  file:
    path: "{{ item|safe|trim }}"
    state: directory
    owner: "{{ var_user }}"
    group: "{{ var_group }}"
    mode: 0777
    recurse: yes
  with_lines: cat /tmp/dirs.txt
  tags:
    - create-dir



codeaprendiz
  • 2,703
  • 1
  • 25
  • 49

1 Answers1

4

To use the values from var_list_dirs use with_items to loop for all the values in var_list_dirs. This will perform create dirs action for each value in the list.

- name: create dirs
  file:
    path: "{{ item|safe|trim }}"
    state: directory
    owner: "{{ var_user }}"
    group: "{{ var_group }}"
    mode: 0777
    recurse: yes
  with_items: "{{ var_list_dirs }}"
  when:
    - inventory_hostname in groups['node-hostname']
  tags:
    - create-dir

Pass the list as:

var_list_dirs:
  - directory1
  - directory2
  - directory3
aru_sha4
  • 368
  • 2
  • 11
  • 1
    thanks @aru_sha4, but actually my question is to be able to create directories by passing a list `var_list_dirs` which i can set in the playbook. This way my roles does not need to change no matter how many playbooks want to create the directories. – codeaprendiz Jun 20 '20 at 15:52
  • Okay. So you want to pass the `var_list_dirs` and the action for creating that directory should be executed for all the values present in `var_list_dirs`? Right? I have added another update. Kindly let me know if that is what you want. – aru_sha4 Jun 20 '20 at 15:55
  • Hi @aru_sha4, it gives me this error. ` /bin/sh: 1: /data: not found fatal: [35.232.170.16]: FAILED! => {"msg": "lookup_plugin.lines(/data /opt/elasticsearch-server /opt/elasticsearch-server/config /data/log/elasticsearch-server /data/var/lib/elasticsearch-server_01 /data/var/lib/elasticsearch-server_02 /data/var/lib/elasticsearch-server_03 /data/var/elasticsearch-server-backup) returned 127"}` – codeaprendiz Jun 20 '20 at 16:05
  • oh i was passing the list incorrectly. Oh god. Let me try again. It makes sense. – codeaprendiz Jun 20 '20 at 16:07
  • Sure. Hope it helps. – aru_sha4 Jun 20 '20 at 16:20
  • thanks a lot. It works nicely. Could you please change the answer to the second case which you gave. I will accept it. Its perfect. – codeaprendiz Jun 20 '20 at 16:20