2017-03-07 71 views
-1

我昨天从Ansible入手,尝试在我的私有XenServer上配置虚拟机。Ansible Xen Provisioning

我跟着这个指南: XenGuide Sharknet.us

所以我可以从一个ansible平中游Xen的服务器。

我的项目文件夹的结构如下:

Project Folder 
|-- site.yml 
|-- hosts (inventory file) 
+-+ host_vars 
| |-- master01.lab.local 
| |-- master02.lab.local 
| |-- slave01.lab.local 
| |-- slave02.lab.local 
+-+ group_vars 
| |-- all 
+-+ roles 
| +-+ master 
| | |-- main.yml 
| +-+ slave 
| | |-- main.yml 
| +-+ vm 
| | |-- create_vm.yml 
| | |-- main.yml 

我的文件Site.yml:

[vm] 
master01.lab.local 
master02.lab.local 
slave01.lab.local 
slave02.lab.local 

[hypervisor] 
xen01.lab.local 

host_vars/master01.lab.local为例:

hostname: master01 
ram: 8GiB 
vcpus: 4 

groups_vars /全部文件:

centos_template_uid: 11fd3dc9-96cc-49af-b091-a2ca7e94c589 
primary_sr_uid: 6e074a6e-bf19-031d-7c65-c9ab2749a3da 
user_network_uid: 087af565-bd27-cd0a-93e4-724beeb27735 

角色/ VM/main.yml

- name: Check if VM exists 
    command: xe vm-list name-label={{ hostname }} 
    register: found 
    delegate_to: xen01.lab.local 

- include: create_vm.yml 
    when: found.stdout == "" 

- name: Wait for the Kickstart install to complete and the VM to reboot 
    local_action: wait_for host={{ hostname }}.lab.local port=22 delay=15 timeout=1200 state=started 

角色/ VM/create_vm.yml

- name: Prevent template from creating storage 
    command: xe template-param-remove uuid={{ centos_template_uid }} param-name=other-config param-key=disks 
    ignore_errors: yes 
    delegate_to: xen01.lab.loca 

- name: Create VM 
    command: xe vm-install template={{ centos_template_uid }} new-name-label="{{ hostname }}" sr-uuid={{ primary_sr_uid }} 
    register: vm 
    delegate_to: xen01.lab.local 

- name: Set the repository location 
    command: xe vm-param-set uuid={{ vm.stdout }} other-config:install-repository="http://ansible-repo.lab.local/" 
    delegate_to: xen01.lab.local 

- name: Set the location of the kickstart file 
    command: xe vm-param-set uuid={{ vm.stdout }} PV-args="ks=http://ansible-repo.lab.local/ks.cfg ksdevice=eth0" 
    delegate_to: xen01.lab.local 

- name: Assign a network 
    command: xe vif-create vm-uuid={{ vm.stdout }} network-uuid={{ user_network_uid }} mac={{ mac }} device=0 
    delegate_to: xen01.lab.local 

- name: Allocate VM storage 
    command: xe vdi-create name-label="{{ hostname }} storage" sr-uuid={{ primary_sr_uid }} type=system virtual-size=25GiB 
    register: disk 
    delegate_to: xen01.lab.local 

- name: Assign storage to VM 
command: xe vbd-create vdi-uuid={{ disk.stdout }} vm-uuid={{ vm.stdout }} type=Disk bootable=true device=0 
delegate_to: xen01.lab.local 

- name: Set the VM RAM limits 
    command: xe vm-memory-limits-set vm={{ vm.stdout }} static-min={{ ram }} static-max={{ ram }} dynamic-min={{ ram }} dynamic-max={{ ram }} 
    delegate_to: xen01.lab.local 

- name: Set the number of CPUs 
    command: xe vm-param-set VCPUs-max={{ vcpus }} uuid={{ vm.stdout }} 
    delegate_to: xen01.lab.local 

- name: Launch the VM 
    command: xe vm-start uuid={{ vm.stdout }} 
    delegate_to: xen01.lab.local 

如果我现在运行以下命令:

ansible-playbook -i hosts site.yml -K 

我得到的以下错误:

ERROR! Syntax Error while loading YAML. 

The error appears to have been in '/var/ansible/ansible-playbooks/provisioning/xen/roles/vm/tasks/main.yml': line 2, column 1, but may 
be elsewhere in the file depending on the exact syntax problem. 

The offending line appears to be: 
- name: Check if VM exists 
    command: xe vm-list name-label={{ hostname }} 
^ here 
We could be wrong, but this one looks like it might be an issue with 
missing quotes. Always quote template expression brackets when they 
start a value. For instance: 

with_items: 
    - {{ foo }} 

Should be written as: 

with_items: 
    - "{{ foo }}" 

有人知道这个错误吗?

回答

0

你应该引用字符串,包含变量,所以在你的情况下

command: xe vm-list name-label={{ hostname }} 

应该成为

command: xe vm-list name-label="{{ hostname }}" 

同样适用于所有其他任务,而无需引号变量