问题描述
我正在使用安装了 Vagrant 的 Ubuntu 16.04 主机.我想记录一组任务的开始时间和结束时间.这些任务被定义为角色.我创建了一个角色 host_test 角色,以确保我能够使用 local_action
在我的主机上touch
一个文件.但我相信,由于这个剧本是通过 Vagrant 提供的,当 host_test 角色由 ansible 扮演时,它的控制是在 vagrant box 内部而不是外部(我的目的是登录主机).所以这个文件不是在主机上创建的.
I am using a Ubuntu 16.04 host with Vagrant installed on it. I would like to log the start time and end time of a set of tasks. These tasks are defined as roles. I created a role host_test role to ensure that I am able to touch
a file on my host using local_action
. But I believe as this playbook is being provisioned via Vagrant, by the time host_test role is played by ansible, Its control is inside the vagrant box rather than outside of it (my intention is to log in the host). So the file is not created on the host.
我的目录结构如下:
goal
├── playbook.yml
├── README.md
├── roles
│ ├── apache
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── templates
│ │ └── index.html
│ ├── host_test
│ │ └── main.yml
│ ├── mysql
│ │ └── tasks
│ │ └── main.yml
│ ├── package
│ │ └── tasks
│ │ └── main.yml
│ ├── php5
│ │ └── tasks
│ │ └── main.yml
│ └── vim
│ └── tasks
│ └── main.yml
└── Vagrantfile
正在考虑的剧本可以在 https://github.com/srihas619/vagrant-ansible 找到
the playbook under consideration can be found at https://github.com/srihas619/vagrant-ansible
推荐答案
快速搜索显示您可以为该任务使用回调插件 (Ansible 2.x+).示例计时器插件(官方文档中的代码)如果您需要自己的东西,这基本上是一个好的开始.或者,您可以先检查现有插件:https://github.com/ginsys/ansible-plugins/blob/devel/callback_plugins/timestamp.py
Quick search shows that you can use callback plugins (Ansible 2.x+) for that task. The sample timer plugin (code in official docs) is basically a good start if you need something own. Alternatively you can check existing plugins first:https://github.com/ginsys/ansible-plugins/blob/devel/callback_plugins/timestamp.py
或者,您可以简单地添加
Alternatively, you may simply add
callback_whitelist = profile_tasks
到您的 ansible.cfg
(在 [defaults]
中),它将打开分析器,也为每个任务添加时间戳:
to your ansible.cfg
(in [defaults]
) which would turn on profiler, timestamping each task too:
# ansible-playbook install-vim.yml
PLAY [local] *******************************************************************
TASK [install-vim : Install Vim for user root] *********************************
Thursday 17 November 2016 10:23:22 +0000 (0:00:00.037) 0:00:00.037 *****
ok: [127.0.0.1]
[...]
PLAY RECAP *********************************************************************
127.0.0.1 : ok=6 changed=0 unreachable=0 failed=0
Thursday 17 November 2016 10:23:37 +0000 (0:00:01.464) 0:00:15.695 *****
===============================================================================
install-vim : Install Git ----------------------------------------------- 5.06s
install-vim : Install Vim for user root --------------------------------- 4.56s
install-vim : Installing Pathogen --------------------------------------- 1.80s
install-vim : Setup ~root/.vimrc ---------------------------------------- 1.46s
install-vim : Install Solarized theme ----------------------------------- 1.40s
install-vim : Create folders -------------------------------------------- 1.38s
这篇关于当 playbook 通过 Vagrant provisioner 运行时,如何在主机上为某些任务运行 ansible play?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!