Improve ansible code for network xml creation
[yardstick.git] / ansible / roles / infra_create_network / tasks / create_xml.yml
1 # Copyright (c) 2017-2018 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ---
15 - name: Define XML file name
16   set_fact:
17     xml_file: "{{ '/tmp/'+item.name+'.xml' }}"
18
19 - name: Delete the file, if exists
20   file:
21     path: "{{ xml_file }}"
22     state: absent
23
24 - name: Create a new empty file
25   file:
26     path: "{{ xml_file }}"
27     state: touch
28
29 - name: Add root "network" node
30   blockinfile:
31     path: "{{ xml_file }}"
32     marker: ""
33     content: |
34       <network>
35       </network>
36
37 - name: Add new children nodes to "network" node
38   xml:
39     path: "{{ xml_file }}"
40     xpath: /network
41     add_children:
42       - name: "{{ item.name }}"
43       - forward:
44           mode: nat
45       - bridge:
46           name: "{{ item.name }}"
47           stp: "on"
48           delay: "0"
49       - ip:
50           address: "{{ item.host_ip }}"
51           netmask: "{{ item.netmask }}"
52     pretty_print: yes
53
54 - name: Add new children nodes to "forward" node
55   xml:
56     path: "{{ xml_file }}"
57     xpath: /network/forward
58     add_children:
59       - nat
60     pretty_print: yes
61
62 - name: Add new children nodes to "nat" node
63   xml:
64     path: "{{ xml_file }}"
65     xpath: /network/forward/nat
66     add_children:
67       - port:
68           start: "1024"
69           end: "65535"
70     pretty_print: yes
71
72 - name: Define the networks
73   virt_net:
74     command: define
75     name: "{{ item.name }}"
76     xml: "{{ lookup('file', xml_file) }}"
77
78 - name: Set autostart to yes
79   virt_net:
80     autostart: yes
81     name: "{{ item.name }}"
82
83 - name: Start the networks
84   virt_net:
85     command: start
86     name: "{{ item.name }}"
87
88 - name: Remove XML file
89   file:
90     path: "{{ xml_file }}"
91     state: absent