在远程剧本中运行ansible本地任务

时间:2014-08-05 17:12:22

标签: ansible ansible-playbook

我试图让这个任务在本地运行(在运行playbook的机器上):

- name: get the local repo's branch name
  local_action: git branch | awk '/^\*/{print $2}'
  register: branchName

我尝试了很多变化而没有成功

所有其他任务都是在目标主机上运行,​​这就是为什么在本地运行整个playbook不是一个选项

TASK: [get the local repo's branch name] ************************************** 
<127.0.0.1> REMOTE_MODULE git branch | awk '/^\*/{print $2}'
<127.0.0.1> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172 && echo $HOME/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172']
<127.0.0.1> PUT /tmp/tmpQVocvw TO /home/max/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172/git
<127.0.0.1> EXEC ['/bin/sh', '-c', '/usr/bin/python /home/max/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172/git; rm -rf /home/max/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172/ >/dev/null 2>&1']
failed: [portal-dev] => {"failed": true}
msg: this module requires key=value arguments (['branch', '|', 'awk', '/^\\*/{print $2}'])

FATAL: all hosts have already failed -- aborting

更新

我已经遵循了bkan的建议(吼叫),并且进一步了解,但是

  - name: get the local repo's branch name
    local_action: command git branch | (awk '/^\*/{print $2}')
    sudo: no
    register: branchName

现在git命令已启动但未正确启动(请参阅下面的错误)。

请注意,此命令可以完美地运行为&#34; shell&#34;但不幸的是没有local_shell等效于local_action ...

failed: [portal-dev] => {"changed": true, "cmd": ["git", "branch", "|", "(awk", "/^\\*/{print $2})"], "delta": "0:00:00.002980", "end": "2014-08-05 18:00:01.293632", "rc": 129, "start": "2014-08-05 18:00:01.290652"}
stderr: usage: git branch [options] [-r | -a] [--merged | --no-merged]
   or: git branch [options] [-l] [-f] <branchname> [<start-point>]
   or: git branch [options] [-r] (-d | -D) <branchname>...
   or: git branch [options] (-m | -M) [<oldbranch>] <newbranch>

...

1 个答案:

答案 0 :(得分:43)

local_action的格式为:

local_action: <module_name> <arguments>

在您的示例中,Ansible认为您正在尝试使用git模块并抛出错误,因为您没有git模块的正确参数。以下是它的外观:

local_action: shell git branch | awk '/^\*/{print $2}'

来源:http://docs.ansible.com/playbooks_delegation.html#delegation