如何在Vagrant机器上强制运行剧本?

时间:2016-12-10 15:26:23

标签: vagrant ansible ansible-playbook vagrantfile

我写了一个安装Docker的剧本。

---

- name: Install dependencies
  apt: name={{ item }} state=present update_cache=yes
  with_items:
    - linux-image-generic-lts-{{ ansible_distribution_release }}
    - linux-headers-generic-lts-{{ ansible_distribution_release }}
  become: true

- name: Add Docker repository key
  apt_key:
    id: 58118E89F3A912897C070ADBF76221572C52609D
    keyserver: hkp://p80.pool.sks-keyservers.net:80
    state: present
  register: add_repository_key
  become: true

- name: Add Docker repository
  apt_repository:
    repo: 'deb https://apt.dockerproject.org/repo {{ ansible_distribution_release }} main'
    state: present
  become: true

- name: Install Docker
  apt:
    name: docker
    state: latest
    update_cache: yes
  become: true

- name: Verify the service is running
  service:
    name: docker
    enabled: yes
    state: started
  become: true

我正在使用配置为使用该剧本的流浪汉机器。

Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.ssh.insert_key = false
  config.vm.synced_folder "./", "/tmp/project",create: true
  config.vm.network :forwarded_port, guest: 80, host: 80 , auto_correct: true

  config.vm.provider :virtualbox do |v|
    # Name of machine
    v.name = "default"
    # Machine memory
    v.memory = 1024
    # Number of cpu's
    v.cpus = 2
    # This option makes the NAT engine use the host's resolver mechanisms to handle DNS requests
    v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    # Enabling the I/O APIC is required for 64-bit guest operating systems; it is also required if you want to use more than one virtual CPU in a virtual machine.
    v.customize ["modifyvm", :id, "--ioapic", "on"]
  end

  config.vm.provision "ansible" do |ansible|
    # Sets the playbook to use when machine is up'ed
    ansible.playbook = "deploy/main.yml"
  end

end

但由于某种原因,我得到的输出和没有安装在流浪汉机器上的码头工具:

$ vagrant up --provision
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Running provisioner: ansible...
    default: Running ansible-playbook...

PLAY RECAP *********************************************************************

这是正确的方法吗?

是否有一个命令可以让我在正在运行的Vagrant机器上播放剧本?

1 个答案:

答案 0 :(得分:3)

  

我写了一个安装docker的剧本。

tasks

不,你还没写过剧本。您编写了一个包含Ansible任务列表的YAML文件。

Playbooks包含播放列表,播放是YAML词典,为了Ansible工作,至少必须包含- hosts: default tasks: - name: Install dependencies apt: name={{ item }} state=present update_cache=yes with_items: - linux-image-generic-lts-{{ ansible_distribution_release }} - linux-headers-generic-lts-{{ ansible_distribution_release }} become: true 键。在典型的游戏中,任务列表在default键中定义。

因此,您的文件至少需要一个剧本:

v.name = "default"

注意vagrant provision 这里是指您的Vagrantfile(ansible-playbook)中定义的计算机的名称,而不是任何Ansible-default。 < / p>

  

是否有一个命令可以让我在正在运行的Vagrant机器上播放剧本?

您可以使用以下命令运行Vagrant文​​件中定义的playbook:

vagrant

要运行另一个,您只需使用remote_user,但必须指向Vagrant的广告资源文件,您还必须使用ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory playbook.yml 作为{{1}}:

{{1}}