在 Ansible 主机上运行命令 [英] Run command on the Ansible host
问题描述
是否可以在 Ansible 主机上运行命令?
Is it possible to run commands on the Ansible host?
我的情况是,我想从内部托管的 git 服务器(并且无法在公司防火墙外访问)进行结账.然后我想将结帐(压缩包)上传到生产服务器(外部托管).
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).
目前,我正在考虑运行一个脚本来执行结账、压缩包,然后运行部署脚本 - 但如果我可以将它集成到 Ansible 中,那就更好了.
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.
推荐答案
是的,您可以在 Ansible 主机上运行命令.您可以指定一个 play 中的所有任务都在 Ansible 主机上运行,也可以将单个任务标记为在 Ansible 主机上运行.
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.
如果要在 Ansible 主机上运行整个 play,则在 play 中指定 hosts: 127.0.0.1
和 connection:local
,例如:>
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
请参阅 Ansible 文档中的本地手册更多详情.
See Local Playbooks in the Ansible documentation for more details.
如果您只想在 Ansible 主机上运行单个任务,您可以使用 local_action
指定一个任务应该在本地运行.例如:
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
参见控制任务运行的位置:委派和本地操作有关更多详细信息,请参阅 Ansible 文档.
See Controlling where tasks run: delegation and local actions in the Ansible documentation for more details.
您可以通过将其添加到您的库存中来避免在您的游戏中输入connection:local
:
You can avoid having to type connection: local
in your play by adding this to your inventory:
localhost ansible_connection=local
(在这里,您将使用localhost"而不是127.0.0.1"来指代该剧).
(Here you'd use "localhost" instead of "127.0.0.1" to refer to the play).
在较新版本的 ansible 中,您不再需要将上述行添加到您的清单中,ansible 假定它已经存在.
In newer versions of ansible, you no longer need to add the above line to your inventory, ansible assumes it's already there.
这篇关于在 Ansible 主机上运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!