2016-12-10 49 views
1

我寫了一本安裝Docker的劇本。如何強制在Vagrant機器上運行劇本?

--- 

- 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 

但由於某些原因,這就是輸出I get和碼頭工人沒有流浪機器上安裝:

$ 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機器上播放劇本?

回答

2

我寫了一本安裝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鍵中定義。

因此,對於你的文件是一個劇本,你會至少需要:default這裏指的是在您的Vagrantfile(v.name = "default")中定義的計算機的名稱沒有什麼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 

注意 -默認。


有沒有可以讓我發揮的劇本正在運行的流浪機器上的命令?

可以運行在流浪文件中定義與劇本:

vagrant provision 

要運行一個又一個,你只需要使用ansible-playbook,但你必須指向流浪的清單文件,還必須使用vagrant作爲remote_user

ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory playbook.yml 
相關問題