使用ansible启用extglob

时间:2015-10-20 09:13:27

标签: shell 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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

您可能需要执行以下操作,以便cp- name: copy files sudo: yes shell: shopt -s extglob && cp -pav --parents `git diff --name-only master feature/deploy_2186 !(vendor)` /tmp/current 命令实际在同一个shell实例中运行:

[Serializable]