0

I'm running into a problem where a registered variable is incorrectly substituted in a subsequent task that is delegated to another host. This is the stripped down version of my playbook:

- hosts: all
  name: Test Play
  gather_facts: false

  tasks:
      - name: Create output directory
        tempfile:
            state: directory
            suffix: diag
        register: output_dir

      - name: Create API resources directory
        file:
            path: "{{ output_dir.path }}/api-resources"
            state: directory
        delegate_to: "{{groups['control-plane'][0]}}"
        run_once: yes
        register: api_resources_dir

Now every host will create a temporary directory but only one of my hosts shall additionally create another directory inside of that temporary directory. When this playbook is run, though, the host that the 2nd task is delegated to ends up with two different temporary directories, one of which containing the api-resources directory. The latter one is inside of a temporary directory that has the same name as the delegating host.

My suspicion is that the variable {{ output_dir.path }} in the 2nd task is substituted on the delegating host and only then the task is delegated whereas I would have assumed that the variable is substituted with the value of the host the task is delegated to.

How can I use the output_dir variable in this scenario correctly so that the api-resources directory ends up inside of the temporary directory directory created in the 1st task?

1 Answer 1

0

The answer is simple, just use when instead of delegate_to:

- hosts: all
  name: Test Play
  gather_facts: false

  tasks:
      - name: Create output directory
        tempfile:
            state: directory
            suffix: diag
        register: output_dir

      - name: Create API resources directory
        file:
            path: "{{ output_dir.path }}/api-resources"
            state: directory
        when: inventory_hostname == groups['control-plane'][0]
        register: api_resources_dir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.