剧本中的条件语句可根据命令中传递的变量将文件复制到两个不同的位置

时间:2019-05-17 20:50:29

标签: ansible

我想基于命令行参数中传递的变量将文件复制到一个位置(有两个不同的位置)。 我正在使用复制模块进行复制。

  - name: Copy the file
    copy:
       src: /mydocuments/folder1/file.txt
       dest: /mypictures/folder1
       owner: myuser
       group: mygroup
       mode: 0755
       remote_src: true

基本上,当命令中传递的变量是“ mypictures”时,应将file.txt复制到dest:/ mypictures / folder1,如果变量是“ myfolder”,则应复制到dest:/ myfolder1 / notes < / p>

1 个答案:

答案 0 :(得分:1)

假设传入的变量为myvar,则如下设置目标:

- name: set array
  set_fact:
    dest:
      myfolder: /myfolder1/notes
      mypictures: /mypictures/folder1

- name: Copy the file
  copy:
    src: /mydocuments/folder1/file.txt
    dest: "{{ dest[myvar] }}"
    owner: myuser
   group: mygroup
   mode: 0755
   remote_src: true