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

How to enforce running a playbook on a Vagrant machine?

我写了一个安装 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

但出于某种原因,这是我得到的输出,docker 未安装在 vagrant 机器上:

$ 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 机器上播放剧本?

I wrote a playbook which installs 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

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

Playbooks 包含一个播放列表,播放是 YAML 字典,要让 Ansible 工作,至少必须包含 hosts 键。在典型的游戏中,任务列表在 tasks 键中定义。

因此,要使您的文件成为剧本,您至少需要:

- 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指的是你的Vagrantfile中定义的机器名(v.name = "default")不是Ansible-默认。


Is there a command which lets me play a playbook on a running Vagrant machine?

您可以运行 Vagrant 文件中定义的剧本:

vagrant provision

到运行另一个,你只需要使用ansible-playbook,但你必须指向Vagrant的库存文件,你还必须使用vagrant作为remote_user:

ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory playbook.yml