在可选主机上运行命令 [英] Run command on the Ansible host

查看:124
本文介绍了在可选主机上运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在可执行主机上运行命令吗?



我的情况是,我想从内部托管的git服务器(并且isn不能在公司防火墙外访问)。然后我想将checkout(tarballed)上传到生产服务器(从外部托管)。



目前,我正在运行一个执行结账的脚本,tarballs,然后运行部署脚本 - 但是如果我可以将它集成到Ansible中,那将是更可取的。

解决方案

是,您可以在可执行主机上运行命令。您可以指定播放中的所有任务在可执行主机上运行,​​也可以将各个任务标记为在可选主机上运行。



如果要运行整个在可执行主机上播放,然后在播放中指定 hosts:127.0.0.1 connection:local

   - 名称:完全在可托管主机上运行的游戏
主机:127.0.0.1
连接:local
tasks:
- name:检出git仓库
git:repo = git://foosball.example.org/path/to/repo.git dest = / local /路径

请参阅

如果您只想在可执行主机上运行单个任务,您可以使用 local_action 指定任务应在本地运行。例如:

   - 名称:示例剧本
主机:webservers
任务:
- ...

- 名称:检出git仓库
local_action:git repo = git://foosball.example.org/path/to/repo.git dest = / local / path

请参阅委托在可复制文档中了解更多详情。



编辑:您可以避免不必输入

   c $ c> localhost ansible_connection = local 

(这里您可以使用localhost而不是127.0.0.1参考游戏)



编辑:在较新版本的ansible中,您不再需要将上述行添加到您的广告资源中,ansible假定它已经存在。 p>

Is it possible to run commands on the Ansible host?

My scenario is that I want to take a checkout from a git server that is hosted internally (and isn't accessible outside the company firewall). Then I want to upload the checkout (tarballed) to the production server (hosted externally).

At the moment, I'm looking at running a script that does the checkout, tarballs it, and then runs the deployment script - but if I could integrate this into Ansible that would be preferable.

解决方案

Yes, you can run commands on the Ansible host. You can specify that all tasks in a play run on the Ansible host, or you can mark individual tasks to run on the Ansible host.

If you want to run an entire play on the Ansible host, then specify hosts: 127.0.0.1 and connection:local in the play, for example:

- name: a play that runs entirely on the ansible host
  hosts: 127.0.0.1
  connection: local
  tasks:
  - name: check out a git repository
    git: repo=git://foosball.example.org/path/to/repo.git dest=/local/path

See Local Playbooks in the Ansible documentation for more details.

If you just want to run a single task on your Ansible host, you can use local_action to specify that a task should be run locally. For example:

- name: an example playbook
  hosts: webservers
  tasks:
  - ...

  - name: check out a git repository
    local_action: git repo=git://foosball.example.org/path/to/repo.git dest=/local/path

See Delegation in the Ansible documentation for more details.

Edit: You can avoid having to type connection: local in your play by adding this to your inventory:

localhost ansible_connection=local

(Here you'd use "localhost" instead of "127.0.0.1" to refer to the play).

Edit: In newer versions of ansible, you no longer need to add the above line to your inventory, ansible assumes it's already there.

这篇关于在可选主机上运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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