在启动期间仅运行一次ansible任务并在结束时运行一个ansible任务

时间:2018-04-25 21:21:36

标签: linux ansible

我在ansible playbook下面我想做几件事:

  • 由于我在servers组中有100台机器,所以"任务1"我想在整个时间内运行第一个任务一次。应该只在开始时运行一次。
  • 我想在第三个任务中运行一次,但是当它在最后一台机器上运行时,在本剧本的最后。我不确定这是否可行。

我需要做的是:

  • clients.tar.gz文件从某些远程服务器复制到我的ansible正在运行的本地框(在/ tmp文件夹中)。
  • 然后取消归档这个" clients.tar.gz"文件在"服务器"中指定的所有服务器上基。
  • 最后从/ tmp文件夹中删除此tar.gz文件。

以下是我的回复剧本:这有可能做到吗?

---
- name: copy files
  hosts: servers
  serial: 10
  tasks:
      - name: copy clients.tar.gz file. Run this task only once during starting
        shell: "(scp -o StrictHostKeyChecking=no goldy@machineA:/process/snap/20180418/clients.tar.gz /tmp/) || (scp -o StrictHostKeyChecking=no goldy@machineB:/process/snap/20180418/clients.tar.gz /tmp/) || (scp -o StrictHostKeyChecking=no goldy@machineC:/process/snap/20180418/clients.tar.gz /tmp/)"

      - name: copy and untar latest clients.tar.gz file
        unarchive: src=/tmp/clients.tar.gz dest=/data/files/

      - name: Remove previous tarFile. Run this task only once at the end of this playbook
        file: path=/tmp/clients.tar.gz
              state=absent

      - name: sleep for few seconds
        pause: seconds=20

更新

我查看了链接的问题,看起来可以这样做吗?

---
- name: copy files
  hosts: servers
  serial: 10
  tasks:
      - name: copy clients.tar.gz file. Run this task only once during starting
        shell: "(scp -o StrictHostKeyChecking=no goldy@machineA:/process/snap/20180418/clients.tar.gz /tmp/) || (scp -o StrictHostKeyChecking=no goldy@machineB:/process/snap/20180418/clients.tar.gz /tmp/) || (scp -o StrictHostKeyChecking=no goldy@machineC:/process/snap/20180418/clients.tar.gz /tmp/)"
        delegate_to: "{{ groups['servers'] | first }}"
        run_once: true

      - name: copy and untar latest clients.tar.gz file
        unarchive: src=/tmp/clients.tar.gz dest=/data/files/

      - name: Remove previous tarFile. Run this task only once at the end of this playbook
        file: path=/tmp/clients.tar.gz
              state=absent
        delegate_to: "{{ groups['servers'] | last }}"
        run_once: true

      - name: sleep for few seconds
        pause: seconds=20

0 个答案:

没有答案