Skip to content

Commit a11d2bd

Browse files
author
Artur Orlov
committed
Use ansible builtin modules for lxc and iptables, fixed IPs for containers
1 parent 4164b69 commit a11d2bd

File tree

6 files changed

+103
-59
lines changed

6 files changed

+103
-59
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.vagrant
2+
.kitchen/
3+
.kitchen.local.yml

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Role Variables
1010
# Basic settings for lxc networking
1111
lxc_domain_name: lxc
1212
lxc_ip_address: 10.0.3.1
13+
lxc_netmask: 255.255.0.0
14+
lxc_network: 10.10.0.0/16
15+
lxc_dhcp_range: 10.10.255.0,10.10.255.254
16+
lxc_dhcp_max: 253
1317

1418
# containers to be used as a template
1519
lxc_containers:
@@ -23,12 +27,12 @@ lxc_containers:
2327
lxc_clones:
2428
- name: sample
2529
template: ubuntu.trusty
26-
ip: 10.0.3.22
30+
ip: 10.10.3.22
2731

2832
# Expose ports from containers
29-
lxc_forwarded_ports: []
33+
lxc_forwarded_ports:
3034
- source: 80
31-
target: 10.0.3.22:80
35+
target: 10.10.3.22:80
3236
```
3337
3438
Example Playbook

defaults/main.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
---
22
# defaults file for lxc
33
lxc_domain_name: lxc
4-
lxc_ip_address: 10.0.3.1
4+
lxc_ip_address: 10.10.0.1
5+
lxc_netmask: 255.255.0.0
6+
lxc_network: 10.10.0.0/16
7+
lxc_dhcp_range: 10.10.255.0,10.10.255.254
8+
lxc_dhcp_max: 253
9+
510

611
lxc_containers:
712
- name: ubuntu.trusty

tasks/main.yml

Lines changed: 77 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,109 @@
11
---
22
# tasks file for lxc
33

4-
- name: add ansible repo
4+
- name: lxc | add ansible repo
55
apt_repository: repo=ppa:ansible/ansible
66

7-
- name: install system packages
7+
- name: lxc | install system packages
88
apt: name={{item}} state=latest update_cache=yes
99
with_items:
1010
- unzip
1111
- lxc
1212
- lxc-dev
13+
- htop
1314
- dnsmasq
1415
- iptables-persistent
1516
- ansible
17+
- python-pip
18+
- python-dev
19+
- gcc
1620

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
1829
file: path=/etc/lxc/dnsmasq.conf state=touch
1930

20-
- name: set lxc domain name
31+
- name: lxc | set fixed ips for containers
2132
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
3443
lineinfile:
3544
dest: /etc/dnsmasq.d/lxc
3645
regexp: ^server=
3746
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
4748

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
5353

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
5660

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
5963

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
6468

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

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

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

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 }}"
80106
with_items: "{{lxc_forwarded_ports}}"
81107

82-
- name: save port forwardings
108+
- name: lxc | save port forwardings
83109
command: invoke-rc.d iptables-persistent save

vagrant/Vagrantfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Vagrant.configure(2) do |config|
66
config.ssh.insert_key = false
77
config.ssh.private_key_path = "~/.vagrant.d/insecure_private_key"
88

9-
config.vm.network "private_network", ip: "192.168.12.34"
9+
config.vm.network "private_network", ip: "192.168.88.88"
1010
config.vm.provision "ansible" do |ansible|
1111
ansible.playbook = "test.yml"
1212
ansible.sudo = true

vagrant/test.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
lxc_clones:
66
- name: sample
77
template: ubuntu.trusty
8-
ip: 10.0.3.3
8+
ip: 10.10.3.3
9+
10+
- name: other
11+
template: ubuntu.trusty
12+
ip: 10.10.5.5
13+
914
lxc_forwarded_ports:
1015
- source: 2022
11-
target: 10.0.3.3:22
16+
target: 10.10.3.3:22
17+
18+
- source: 2055
19+
target: 10.10.5.5:22
1220
roles:
1321
- lxc
14-

0 commit comments

Comments
 (0)