通过Vagrant Provisioner运行剧本时,如何在主机上为某些任务运行ansible剧本? [英] How to run ansible plays for some tasks on host when playbook is run via Vagrant provisioner?

查看:88
本文介绍了通过Vagrant Provisioner运行剧本时,如何在主机上为某些任务运行ansible剧本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用安装了Vagrant的Ubuntu 16.04主机.我想记录一组任务的开始时间和结束时间.这些任务被定义为角色.我创建了一个角色 host_test 角色,以确保能够使用local_action在主机上touch一个文件.但是我相信,由于这本剧本是通过Vagrant进行配置的,因此当ansible扮演 host_test 角色时,它的控制权在vagrant框内而不是在它外面(我的意图是登录主机).因此,该文件不是在主机上创建的.

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

这篇关于通过Vagrant Provisioner运行剧本时,如何在主机上为某些任务运行ansible剧本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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