enabling extglob with ansible



是否有办法使extglob从可见?

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

but I got error:

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

命令在终端正常运行,而不是在可见任务中正常运行。在阅读了一些文章后,它需要启用extglob,所以我可以使用!(vendor)模式来排除供应商目录。

从可见任务运行拷贝时出错

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.

我的可见任务做复制,如果我删除!(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

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

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'"

您可能需要这样做,以便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

最新更新