使用 ansible 启用 extglob

enabling extglob with ansible

有没有办法从 ansible 启用 extglob?

- name: copy files
  sudo: yes
  shell: shopt -s extglob

但我收到错误:

failed: [host] => {"changed": true, "cmd": "shopt -s extglob", "delta": "0:00:00.001410", "end": "2015-10-20 09:10:36.438309", "rc": 127, "start": "2015-10-20 09:10:36.436899", "warnings": []}
stderr: /bin/sh: 1: shopt: not found

FATAL: all hosts have already failed -- aborting

我需要 extglob 才能执行 运行 这个命令。此命令排除目录 vendor 被复制。

cp -pav --parents `git diff --name-only master feature/deploy_2186 !(vendor)` /tmp/current

该命令 运行 正确地来自终端,但不是来自 ansible 任务。阅读一些文章后,它需要启用 extglob,所以我可以使用 !(vendor) 模式来排除供应商目录。

从 ansible 任务 运行 复制时出错

failed: [host] => {"changed": true, "cmd": "cp -pav --parents `git diff --name-only master new_release !(vendor)` /tmp/current", "delta": "0:00:00.003255", "end": "2015-10-20 09:22:16.387262", "rc": 1, "start": "2015-10-20 09:22:16.384007", "warnings": []}
stderr: fatal: ambiguous argument '!': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
cp: missing destination file operand after '/tmp/current'
Try 'cp --help' for more information.

我的 ansible 任务是复制,如果我删除它 !(vendor) 它工作得很好,但它里面有供应商:

- name: copy files
  shell: cp -pav --parents `git diff --name-only master feature/deploy_2186 !(vendor)` /tmp/current
  args:
    chdir: /var/www
  tags: release

您可能需要做这样的事情,以便 shoptcp 命令实际上是 运行 在同一个 shell 实例中:

- name: copy files
  sudo: yes
  shell: shopt -s extglob && cp -pav --parents `git diff --name-only master feature/deploy_2186 !(vendor)` /tmp/current

有 3 种方法可以解决这个问题。

1) 将命令放入 shell 脚本 copy.sh 并设置 shell: copy.sh.

#!/bin/bash
shopt -s extglob
cp -pav --parents `git diff --name-only master feature/deploy_2186 !(vendor)` /tmp/current

2) 使用 grep -v 而不是 extglob:

shell: cp -pav --parents `git diff --name-only master feature/deploy_2186 * | grep -v /vendor/` /tmp/current

3) 使用bash 设置extglob 和运行 cp 命令。您需要将两行单独的行传递给 ansible 任务变量。由于语法是 Yaml,它归结为在 shell 字符串中嵌入一个换行符。自己测试一下。

shell: |
  bash -c 'shopt -s extglob
           cp -pav --parents `git diff --name-only master feature/deploy_2186 !(vendor)` /tmp/current'

shell: "bash -c 'shopt -s extglob \n cp -pav --parents `git diff --name-only master feature/deploy_2186 !(vendor)` /tmp/current'"