muhammed hilal
muhammed hilal

Reputation: 91

how to pass many variables in ansible?

I faced problems when I don't know how to pass many variables in with_items:

I have these vars:

- vars:
    paths:
      - /tmp/tecom/python3/
      - /tmp/tecom/pip/
      - /tmp/tecom/psycopg2/
      - /tmp/tecom/docker/
      - /tmp/tecom/postgresql/
    files:
      - python3.tar
      - pip.tar
      - psycopg2.tar
      - docker.tar
      - postgresql.tar

And I have the task that should extract the archives:

- name: unarchive "{{ item }}"
  unarchive:
    src: "{{ item }}"
    dest: # should take the paths above. Every path should match it's own `.tar` file.
  with_items: "{{ files }}"

Every path should match its own .tar file.

Upvotes: 1

Views: 161

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68034

For example,

    - debug:
        msg: "unarchive {{ item }} to {{ _path }}"
      loop: "{{ files }}"
      vars:
        _path: "{{ path }}/{{ item|splitext|first }}/"
        path: /tmp/tecom
        files:
          - python3.tar
          - pip.tar
          - psycopg2.tar
          - docker.tar
          - postgresql.tar

gives (abridged)

  msg: unarchive python3.tar to /tmp/tecom/python3/
  msg: unarchive pip.tar to /tmp/tecom/pip/
  msg: unarchive psycopg2.tar to /tmp/tecom/psycopg2/
  msg: unarchive docker.tar to /tmp/tecom/docker/
  msg: unarchive postgresql.tar to /tmp/tecom/postgresql/

Q: "How do I unarchive those files to the folders?"

A: Use the expressions as appropriate, e.g.

- name: "unarchive {{ item }}"
  unarchive:
    src: "{{ item }}"
    dest: "{{ path }}/{{ item|splitext|first }}/"
  loop: "{{ files }}"

(not tested)

It's up to you where you put the variables files and path. See Variable precedence: Where should I put a variable?

Upvotes: 2

Related Questions