openstack-ansible/playbooks/roles/lxc_container_create/tasks/container_create.yml

144 lines
5.3 KiB
YAML

---
# Copyright 2014, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Obtain the Systems SSH-Key
set_fact:
lxc_container_ssh_key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
when: >
lxc_container_ssh_key is not defined
delegate_to: "{{ physical_host }}"
- name: Check for lxc volume group
shell: "(which vgs > /dev/null && vgs | grep -o '{{ lxc_container_vg_name }}') || false"
register: vg_result
failed_when: false
changed_when: vg_result.rc != 0
delegate_to: "{{ physical_host }}"
tags:
- lxc-container-vg-detect
- name: Set container backend "dir" if "lvm" not found
set_fact:
lxc_container_backing_store: dir
when: vg_result.rc != 0
delegate_to: "{{ physical_host }}"
tags:
- lxc-container-vg-detect
- name: Container service directories
file:
path: "{{ item }}"
state: "directory"
with_items:
- "/openstack/{{ inventory_hostname }}"
- "/openstack/backup/{{ inventory_hostname }}"
- "/openstack/log/{{ inventory_hostname }}"
delegate_to: "{{ physical_host }}"
tags:
- lxc-container-directories
- name: Create container
lxc-container:
name: "{{ inventory_hostname }}"
container_log: "true"
config: "{{ lxc_container_config }}"
template: "{{ lxc_container_template }}"
state: started
backing_store: "{{ lxc_container_backing_store }}"
fs_size: "{{ lxc_container_fs_size }}"
fs_type: "{{ lxc_container_fs_type }}"
vg_name: "{{ lxc_container_vg_name }}"
template_options: "--release {{ properties.container_release|default(lxc_container_release) }}"
container_command: |
mkdir -p ~/.ssh/
if [ ! -f "~/.ssh/authorized_keys" ];then
touch ~/.ssh/authorized_keys
fi
grep '{{ lxc_container_ssh_key }}' ~/.ssh/authorized_keys || echo '{{ lxc_container_ssh_key }}' | tee -a ~/.ssh/authorized_keys
# Create internal directories
mkdir -p /etc/network/interfaces.d
mkdir -p /var/backup
sed -i 's/PermitRootLogin.*/PermitRootLogin\ yes/g' /etc/ssh/sshd_config
service ssh restart
# Configure defined apt-repos
rm /etc/apt/sources.list
echo '# Do not edit this file. Add new sources to /etc/apt/sources.list.d/' | tee /etc/apt/sources.list
echo 'deb {{ lxc_container_template_main_apt_repo }} {{ lxc_container_release }} main restricted universe multiverse' | tee -a /etc/apt/sources.list
echo 'deb {{ lxc_container_template_main_apt_repo }} {{ lxc_container_release }}-updates main restricted universe multiverse' | tee -a /etc/apt/sources.list
echo 'deb {{ lxc_container_template_security_apt_repo }} {{ lxc_container_release }}-security main restricted universe multiverse' | tee -a /etc/apt/sources.list
while timeout 120 apt-get update && apt-get -y install python2.7; [ $? = 124 ]; do
sleep 5
done
rm /usr/bin/python
ln -s /usr/bin/python2.7 /usr/bin/python
echo -e '{{ lxc_container_default_interfaces }}' | tee /etc/network/interfaces
container_config:
- "lxc.aa_profile=lxc-openstack"
- "lxc.mount.entry=/openstack/backup/{{ inventory_hostname }} var/backup none defaults,bind,rw 0 0"
delegate_to: "{{ physical_host }}"
tags:
- lxc-container-create
- name: Container network interfaces
lxc-container:
name: "{{ inventory_hostname }}"
container_command: |
echo -e '{{ lxc_container_interface }}' | tee /etc/network/interfaces.d/{{ item.value.interface }}.cfg
with_dict: container_networks
delegate_to: "{{ physical_host }}"
tags:
- lxc-container-networks
- name: LXC host config for container networks
template:
src: "container-interface.ini.j2"
dest: "/var/lib/lxc/{{ inventory_hostname }}/{{ item.value.interface }}.ini"
owner: "root"
group: "root"
mode: "0644"
with_dict: container_networks
notify:
- Lxc container restart
delegate_to: "{{ physical_host }}"
tags:
- lxc-container-networks
- name: Container network configs
lineinfile:
dest: "/var/lib/lxc/{{ inventory_hostname }}/config"
line: "lxc.include = /var/lib/lxc/{{ inventory_hostname }}/{{ item.value.interface }}.ini"
backup: "true"
with_dict: container_networks
when: >
item.value.interface is defined
notify:
- Lxc container restart
delegate_to: "{{ physical_host }}"
tags:
- lxc-container-networks
- name: Create container service directories
lxc-container:
name: "{{ inventory_hostname }}"
container_command: |
mkdir -p '/var/log/{{ properties.service_name }}'
mkdir -p '/etc/{{ properties.service_name }}'
container_config:
- "lxc.mount.entry=/openstack/log/{{ inventory_hostname }} var/log/{{ properties.service_name }} none defaults,bind,rw 0 0"
when: properties.service_name is defined
delegate_to: "{{ physical_host }}"
tags:
- lxc-container-service-dirs