-1

I have a couple of ansible role tasks which I am using to setup a number of configuration directories. The problem is that I have a list of configurations which need to be setup with a certain path but only if they do not exist.

- name: Ensure core configuration directories exist.
  shell: "cp -r {{ service_install_path }}/conf/ {{ service_home }}/{{ item }}.conf.d"
  when: 
    - "item in conf_indexes_current.content"
    - "not {{ service_home }}/{{ item }}.conf.d.stat.exists"
  with_items: "{{ conf_dirs }}"

The problem however is that you can't stat a path like this:

    - "not {{ service_home }}/{{ item }}.conf.d.stat.exists"

The error I am getting is this:

The conditional check 'not {{ service_home }}/{{ item }}.conf.d.stat.exists' failed. The error was: unexpected '/' line 1

Is there a way to set a dynamic variable full path and then test it? It does not seem like I can set a fact here either.

[update]: Just read another question trying to do something with a very vaguely similar loop concept. Is the correct method to simply use a shell script/template at this point?

dcmbrown
  • 1,049
  • 9
  • 15
  • 1
    Why do you want to use Ansible in the first place, if you ignore its aim and capabilities and try to reimplement them using a patchwork of workarounds for a non-existent problem? – techraf Oct 03 '17 at 23:33
  • Ansible(Puppet, Saltstack, etc etc) really is just a fancy wrapper for something that can just as easily be done with shell-scripting. Is that what you are asking or are you just trolling? The inherit trade-offs between ansible vs shell-scripting vs another method has to do with our development technologies, processes, ease of use, and reusability. – dcmbrown Oct 04 '17 at 16:18
  • The point is getting an updated package and using it's distributed conf directory as a template for multiple configurations which are created (this package is a service in terms of constant use, not as a daemon, I could have used a different variable name for ubfuscating). Ansible modifies various files rather than storing entire sets of conf files in the project repo. – dcmbrown Oct 04 '17 at 16:44

1 Answers1

-2

Alright I did eventually figure this out with hints from bits and pieces of other Stack questions and random sites. I don't use ansible very often and we've only been using it for roughly a year. I ended up doing the following, breaking it into two tasks:

- name: Check existence of config for each index.
  stat: 
    path: "{{ service_home }}/{{ item }}.conf.d"
  register: "{{ item }}_conf_true"
  with_items: "{{ conf_dirs }}"

- name: Ensure core configuration directories exist as full templates for modification.
  shell: "cp -r {{ service_install_path }}/conf/ {{ service_home }}/{{ item }}.conf.d"
  when: 
    - "item in conf_indexes_current.content"
    - "{{ item }}_conf_true is not defined"
  with_items: "{{ conf_dirs }}"

Does it work? Yes. Is it proper? Maybe but there is likely a better way I have not thought of or seen yet.

Ry-
  • 218,210
  • 55
  • 464
  • 476
dcmbrown
  • 1,049
  • 9
  • 15