|
1 | 1 | --- |
2 | 2 | # tasks file for lxc |
3 | 3 |
|
4 | | -- name: add ansible repo |
| 4 | +- name: lxc | add ansible repo |
5 | 5 | apt_repository: repo=ppa:ansible/ansible |
6 | 6 |
|
7 | | -- name: install system packages |
| 7 | +- name: lxc | install system packages |
8 | 8 | apt: name={{item}} state=latest update_cache=yes |
9 | 9 | with_items: |
10 | 10 | - unzip |
11 | 11 | - lxc |
12 | 12 | - lxc-dev |
| 13 | + - htop |
13 | 14 | - dnsmasq |
14 | 15 | - iptables-persistent |
15 | 16 | - ansible |
| 17 | + - python-pip |
| 18 | + - python-dev |
| 19 | + - gcc |
16 | 20 |
|
17 | | -- name: create dnsmasq config file |
| 21 | +- name: lxc | install lxc-python |
| 22 | + pip: name=lxc-python2 state=latest |
| 23 | + |
| 24 | +- name: lxc | save running containers |
| 25 | + command: lxc-ls -1 --running |
| 26 | + register: running_containers |
| 27 | + |
| 28 | +- name: lxc | ensure dnsmasq config file exists |
18 | 29 | file: path=/etc/lxc/dnsmasq.conf state=touch |
19 | 30 |
|
20 | | -- name: set lxc domain name |
| 31 | +- name: lxc | set fixed ips for containers |
21 | 32 | lineinfile: |
22 | | - dest: /etc/default/lxc-net |
23 | | - regexp: "{{item.regexp}}" |
24 | | - line: "{{item.line}}" |
25 | | - with_items: |
26 | | - - regexp: ^LXC_DOMAIN= |
27 | | - line: LXC_DOMAIN="{{lxc_domain_name}}" |
28 | | - - regexp: ^LXC_DHCP_CONFILE= |
29 | | - line: LXC_DHCP_CONFILE=/etc/lxc/dnsmasq.conf |
30 | | - notify: |
31 | | - - restart lxc-net |
32 | | - |
33 | | -- name: setup lxc nameserver |
| 33 | + dest: /etc/lxc/dnsmasq.conf |
| 34 | + line: dhcp-host={{item.name}},{{item.ip}} |
| 35 | + with_items: "{{lxc_clones}}" |
| 36 | + register: lxc_net_hosts |
| 37 | + |
| 38 | +- name: lxc | configure lxc network |
| 39 | + template: src=lxc-net.j2 dest=/etc/default/lxc-net |
| 40 | + register: lxc_net_config |
| 41 | + |
| 42 | +- name: lxc | setup lxc nameserver |
34 | 43 | lineinfile: |
35 | 44 | dest: /etc/dnsmasq.d/lxc |
36 | 45 | regexp: ^server= |
37 | 46 | line: server=/lxc/{{lxc_ip_address}} |
38 | | - notify: |
39 | | - - restart lxc-net |
40 | | - |
41 | | -- name: create base container |
42 | | - command: > |
43 | | - lxc-create --name {{item.name}} -t {{item.template}} -- r {{item.release}} |
44 | | - --user {{item.user}} --password {{item.password}} --packages python2.7,python-pip |
45 | | - creates=/var/lib/lxc/{{item.name}}/config |
46 | | - with_items: "{{ lxc_containers }}" |
| 47 | + register: lxc_dnsmasq |
47 | 48 |
|
48 | | -- name: define cluster |
49 | | - lineinfile: |
50 | | - dest: /etc/lxc/dnsmasq.conf |
51 | | - line: dhcp-host={{item.name}}.{{lxc_domain_name}},{{item.ip}} |
52 | | - with_items: "{{lxc_clones}}" |
| 49 | +- name: lxc | stop running containers |
| 50 | + lxc_container: name={{ item }} state=stopped |
| 51 | + with_items: "{{ running_containers.stdout_lines }}" |
| 52 | + when: lxc_net_config.changed or lxc_net_hosts.changed or lxc_dnsmasq.changed |
53 | 53 |
|
54 | | -- name: restart lxc-net |
55 | | - service: name=lxc-net state=restarted |
| 54 | +- name: lxc | ensure lxc-net config changes applied |
| 55 | + service: name={{ item }} state=restarted |
| 56 | + with_items: |
| 57 | + - lxc-net |
| 58 | + - dnsmasq |
| 59 | + when: lxc_net_config.changed or lxc_net_hosts.changed or lxc_dnsmasq.changed |
56 | 60 |
|
57 | | -- name: restart dnsmasq |
58 | | - service: name=dnsmasq state=restarted |
| 61 | +- name: lxc | ensure lxc-net started |
| 62 | + service: name=lxc-net state=started enabled=yes |
59 | 63 |
|
60 | | -- name: create cluster nodes |
61 | | - command: > |
62 | | - lxc-clone -s {{item.template}} {{item.name}} creates=/var/lib/lxc/{{item.name}}/config |
63 | | - with_items: "{{lxc_clones}}" |
| 64 | +- name: lxc | restore running containers |
| 65 | + lxc_container: name={{ item }} state=started |
| 66 | + with_items: "{{ running_containers.stdout_lines }}" |
| 67 | + when: lxc_net_config.changed or lxc_net_hosts.changed or lxc_dnsmasq.changed |
64 | 68 |
|
65 | | -- name: start cluster |
66 | | - command: lxc-start -n {{item.name}} -d |
67 | | - with_items: "{{lxc_clones}}" |
| 69 | +- name: lxc | create base container |
| 70 | + lxc_container: |
| 71 | + name: "{{ item.name }}" |
| 72 | + template: ubuntu |
| 73 | + template_options: > |
| 74 | + --release {{ item.release }} |
| 75 | + --user {{ item.user }} |
| 76 | + --password {{ item.password }} |
| 77 | + --packages python2.7,python-pip |
| 78 | + state: stopped |
| 79 | + container_log: true |
| 80 | + with_items: "{{ lxc_containers }}" |
68 | 81 |
|
69 | | -- name: enable autostart of conatianers |
70 | | - lineinfile: |
71 | | - dest: /var/lib/lxc/{{item.name}}/config |
72 | | - line: lxc.start.auto = 1 |
73 | | - with_items: "{{lxc_clones}}" |
| 82 | +- name: lxc | create clones |
| 83 | + lxc_container: |
| 84 | + name: "{{ item.template }}" |
| 85 | + clone_name: "{{ item.name }}" |
| 86 | + state: stopped |
| 87 | + with_items: "{{ lxc_clones }}" |
74 | 88 |
|
75 | | -- name: flush existing port forwarings |
76 | | - command: iptables -t nat -F PREROUTING |
| 89 | +- name: lxc | setup clones |
| 90 | + lxc_container: |
| 91 | + name: "{{ item.name }}" |
| 92 | + container_config: |
| 93 | + - "lxc.start.auto = 1" |
| 94 | + state: restarted |
| 95 | + with_items: "{{ lxc_clones }}" |
77 | 96 |
|
78 | | -- name: forward ports to lxc machines |
79 | | - command: iptables -t nat -A PREROUTING -i eth1 -p tcp --dport {{item.source}} -j DNAT --to {{item.target}} |
| 97 | +- name: lxc | forward ports to lxc machines |
| 98 | + iptables: |
| 99 | + table: nat |
| 100 | + chain: PREROUTING |
| 101 | + in_interface: eth1 |
| 102 | + protocol: tcp |
| 103 | + destination_port: "{{ item.source }}" |
| 104 | + jump: DNAT |
| 105 | + to_destination: "{{ item.target }}" |
80 | 106 | with_items: "{{lxc_forwarded_ports}}" |
81 | 107 |
|
82 | | -- name: save port forwardings |
| 108 | +- name: lxc | save port forwardings |
83 | 109 | command: invoke-rc.d iptables-persistent save |
0 commit comments