如何在ansible中传递多个变量?

how to pass many variables in ansible?

当我不知道如何在 with_items:

中传递许多变量时,我遇到了问题

我有这些变量:

- 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

我有一个任务是提取档案:

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

每个路径都应该匹配它自己的 .tar 文件。

例如,

    - 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

给出(删节)

  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/

问:“如何将这些文件解压缩到文件夹?”

A:适当使用表达方式,例如

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

(未测试)

这取决于您将变量 filespath 放在哪里。参见 Variable precedence: Where should I put a variable?