Ansible Xen配置

时间:2017-03-07 09:04:00

标签: ansible provisioning xen

我昨天开始使用Ansible并尝试在我的私有XenServer上配置VM。

我遵循了本指南: XenGuide Sharknet.us

因此,我可以使用ansible ping来访问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 / all file:

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 }}"

有人知道这个错误吗?

1 个答案:

答案 0 :(得分:0)

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
  <title></title>
  <link href="stylesheets/StyleSheet1.css" type="text/css" rel="Stylesheet" />

  <asp:ContentPlaceHolder ID="head" runat="server">
  </asp:ContentPlaceHolder>
  <style type="text/css">
    .auto-style2 {
      height: 550px;
    }
    
    .auto-style3 {
      height: 454px;
      margin-top: 40px;
    }
    
    .auto-style4 {
      height: 133px;
      margin-top: 81px;
    }
    
    .auto-style5 {
      margin-left: 57px;
    }
    
    .auto-style6 {
      width: 123px;
    }
  </style>
</head>

<body style="height: 600px">
  <form id="form1" runat="server" class="auto-style2">
    <div class="auto-style2">
      <div class="auto-style3">

        <div style="margin-top :80px">
          <div style="width:100%; float:left; margin-top: -20px;">
            <div style="float:left; width:44%; text-align:justify; margin-left:20px; 
   margin-top:-20px;">

              <asp:Image ID="Image1" Height="40" Width="150" ImageAlign="Middle" runat="server" ImageUrl="~/images/ Logo.png" />
            </div>
          </div>
        </div>
        <br />
        <br />

        <div>
          <ul id="pri" class="nav" style="height: 10px; margin-top: 30px">
            <li class="active"><a href="AfterLogin.aspx">Home</a></li>
            <li><a href="rubrics.aspx">Rubrics</a></li>
            <li><a href="CreateUser.aspx">Create User</a></li>
            <li><a href="#">Reports</a>
              <ul id="sec" class="nav" style="margin-left: 3px">
                <li><a href="a">a</a></li>
                <li><a href="b">b</a></li>
                <li><a href="c">c</a></li>

              </ul>
            </li>
            <li class="auto-style6"><a href="AdminMessaging.aspx">Messages</a></li>

            <li class="auto-style5">
              <asp:LinkButton runat="server" id="logout" onclick="logout_Click">Sign Out</asp:LinkButton>
            </li>
          </ul>

        </div>

        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">





        </asp:ContentPlaceHolder>


      </div>
    </div>

  </form>
  <div id="p1" class="auto-style4">
    <div style="height:10px;"></div>

    <div style="color:#F5F5F5;text-align:center;">
      Copyright © xyz
    </div>
  </div>

应该成为

CREATE TABLE #Table1
    ([Date] datetime, [Type] varchar(1), [ClassA] INT, [ClassB] INT)
;

INSERT INTO #Table1
    ([Date], [Type], [ClassA], [ClassB])
VALUES
    ('2015-02-05 00:00:00', 'A', '1', NULL),
    ('2015-04-05 00:00:00', 'B', '2', NULL),
    ('2015-04-05 00:00:00', 'B', NULL, '3'),
    ('2015-05-05 00:00:00', 'A', '4', NULL),
    ('2015-05-05 00:00:00', 'B', '5', NULL)
;

SELECT [Date] ,[Type], MAX(CLASSA) CLASSA,  MAX(ClassB)ClassB FROM #Table1
GROUP BY [Date] ,[Type]

同样适用于没有变量引号的所有其他任务