问题
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.
My directory structure looks like:
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
the playbook under consideration can be found at https://github.com/srihas619/vagrant-ansible
回答1:
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
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
来源:https://stackoverflow.com/questions/40623847/how-to-run-ansible-plays-for-some-tasks-on-host-when-playbook-is-run-via-vagrant