![Ansible 未在 Packer Build 中执行](https://linux22.com/image/685591/Ansible%20%E6%9C%AA%E5%9C%A8%20Packer%20Build%20%E4%B8%AD%E6%89%A7%E8%A1%8C.png)
我目前正在尝试使用 Packer 的 Ansible 本地配置进行部署,但出于某种奇怪的原因,它似乎没有执行剧本中的任务。在安装了 Ansible 的先决条件后,Packer 上传了以下内容:
库存文件(“主机”)
[localhost]
localhost ansible_connection=local
以及剧本文件(“local.yml”):
---
- hosts: localhost
connection: local
become: yes
become_method: sudo
tasks:
- name: Making sure that ansible is actually executing this playbook
shell: echo "hello world"
- name: install packages
package: name={{ item }} state=present
with_items:
- nginx
- git
- curl
- libmysqlclient-dev
- libmysqlclient18
- mysql-common
- nodejs
- tcpdump
输出如下(针对 Ubuntu 14.04 实例运行 Ansible):
amazon-ebs: Executing Ansible: cd /tmp/packer-provisioner-ansible-local && ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook /tmp/packer-provisioner-ansible-local/local.yml -c local -i /tmp/packer-provisioner-ansible-local/hosts
==> amazon-ebs: Stopping the source instance...
==> amazon-ebs: Waiting for the instance to stop.
正如您所看到的,没有 Ansible 输出,我认为我应该得到(如果我错了,请有人纠正我)。
我手动运行了剧本,成功了。目前,我不确定我是否遗漏了什么。
答案1
事实证明,问题在于我在运行 ansible 之前执行了重启命令。
"sudo apt-get -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold' -y upgrade",
"sudo chmod -R o+rx /usr/local/lib/python2.7/dist-packages",
"sudo shutdown -r now"
]
}, {
"type": "ansible-local",
"inventory_file": "hosts",
"playbook_file": "local.yml"
}]
这意味着在执行 Ansible 时实例就会重新启动。