在远程剧本中运行 ansible 本地任务 [英] running an ansible local task in a remote playbook

查看:24
本文介绍了在远程剧本中运行 ansible 本地任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

- name:获取本地repo的分支名称local_action: git 分支 |awk '/^\*/{打印 $2}'注册:分支名称

我尝试了很多变体都没有成功

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

TASK: [获取本地仓库的分支名称] ****************************************<127.0.0.1>REMOTE_MODULE git 分支 |awk '/^\*/{打印 $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 到/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']失败:[portal-dev] =>{失败":真}msg: 这个模块需要 key=value 参数 (['branch', '|', 'awk', '/^\\*/{print $2}'])致命:所有主机都已经失败——正在中止

更新:

我已经听从了 bkan 的建议(如下),并且更进一步,但是

 - name:获取本地仓库的分支名称local_action: 命令 git branch |(awk '/^\*/{print $2}')须藤:没有注册:分支名称

现在 git 命令启动但不正确(见下面的错误).

请注意,此命令完美地作为shell"运行,但不幸的是,没有 local_shell 等效于 local_action ...

失败:[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"}标准错误:用法:git branch [选项] [-r |-a] [--合并 |--未合并]或: git branch [options] [-l] [-f] [<起点>]或: git branch [options] [-r] (-d | -D) ...或: git branch [options] (-m | -M) [

] ...

解决方案

local_action 的格式为:

local_action: <参数>

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

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

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

I'm trying to get this task to run locally (on the machine that is running the playbook) :

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

I tried plenty of variations with no success

all other tasks are meant to run on the target host, which is why running the whole playbook local is not an option

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

update:

I have followed bkan's suggestion (bellow), and got a bit further, but

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

now the git command gets launched but not correctly (see error below).

note that this command runs perfectly as a "shell" but unfortunately there is no local_shell equivalent of 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>

...

解决方案

The format for local_action is:

local_action: <module_name> <arguments>

In your example, Ansible thinks you are trying to use the git module and throws an error because you don't have the correct arguments for the git module. Here is how it should look:

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

Source: http://docs.ansible.com/playbooks_delegation.html#delegation

这篇关于在远程剧本中运行 ansible 本地任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆