通过 ansible 创建 openstack 虚拟机并部署应用(实战篇)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 前文 通过 ansible 创建 openstack 虚拟机并部署应用(配置篇) 接下来我们实战一下。 目录结构 ── ansible.cfg├── group_vars│ ├── all.yml├── openstack.

前文 通过 ansible 创建 openstack 虚拟机并部署应用(配置篇)

接下来我们实战一下。

目录结构

── ansible.cfg
├── group_vars
│ ├── all.yml
├── openstack.py
├── README.md
├── roles
│ ├── newtouch.MySQL
│ ├── newtouch.SystemConfig
│ ├── newtouch.UpdateRootPass
│ └── openstack.CreateServer
├── setup_mysql.yml

group_vars/all.yml 里需要设置

ansible_user: root
ansible_ssh_pass: xxxxxxxxxxx

前提是 openstack 集群里的虚拟机镜像,root 密码相同。

默认值配置文件 roles/openstack.CreateServer/defaults/main.yml

role 的默认值,根据实际情况,填入相关信息。以 vm_ 开头的变量是为了测试用,预设的。

auth:
  auth_url: http://192.168.205.2:5000/v2.0
  username: admin
  password: xxxxxxxxxxxxxxxxxx
  project_name: Haibin_Lee

# image_id
ubuntu_14_disk_50_v2: 5c67bf65-f699-49e8-955a-72152fb690f4
centos7_50_v2: f9391999-373b-4f0d-9c76-07c19e4e86e5

flavor:
  - {name: 1CPU_1G, id: e3aff42c-f260-4dea-ada5-1241b5853652}
  - {name: 2CPU_4G, id: 45b099ac-4ae4-4526-a6e2-f34cc2934e1b}

vm_state: present
vm_name: db1
vm_image: '{{ ubuntu_14_disk_50_v2 }}'
vm_key: lihaibin
vm_flavor: 1CPU_1G
vm_network: public
vm_group: poc

任务代码 roles/openstack.CreateServer/tasks/main.yml

- block:
    - name: Set the new instance's name
      debug:
        msg: "The new instance's name is {{ vm_name }}-{{ ansible_date_time.epoch }}"
    - name: Creating new instance
      os_server:
        state: '{{ vm_state }}'
        auth:
          auth_url: '{{ auth.auth_url }}'
          username: '{{ auth.username }}'
          password: '{{ auth.password }}'
          project_name: '{{ auth.project_name }}'
        name: "{{ vm_name }}-{{ ansible_date_time.epoch }}"
        image: '{{ vm_image }}'
        flavor: '{{ vm_flavor }}'
        network: '{{ vm_network }}'
        meta:
          hostname: "{{ vm_name }}-{{ ansible_date_time.epoch }}"
    - name: Gathering the new instance's facts
      os_server_facts:
        auth:
          auth_url: '{{ auth.auth_url }}'
          username: '{{ auth.username }}'
          password: '{{ auth.password }}'
          project_name: '{{ auth.project_name }}'
        server: "{{ vm_name }}-{{ ansible_date_time.epoch }}"
    - name: Get the new instance's IP address
      block:
        - set_fact:
            vm_ip: "{{ openstack_servers[0]['accessIPv4'] }}"
        - debug:
            msg: "The new instance's IP address is {{ vm_ip }}"
    - name: Waiting for the new instance up and run
      wait_for:
        host: '{{ vm_ip }}'
        port: 22
        search_regex: OpenSSH
        sleep: 10
        timeout: 3000
    - name: Add the new instance into inventory
      add_host:
        name: "{{ vm_name }}-{{ ansible_date_time.epoch }}"
        ansible_host: "{{ vm_ip }}"
  rescue:
    - name: Remove the new instance when failed
      os_server:
        state: absent
        auth:
          auth_url: '{{ auth.auth_url }}'
          username: '{{ auth.username }}'
          password: '{{ auth.password }}'
          project_name: '{{ auth.project_name }}'
        name: "{{ vm_name }}-{{ ansible_date_time.epoch }}"

这里我使用了 {{ vm_name }}-{{ ansible_date_time.epoch }} 作为新机器的名称,这样可以避免名称重复。如果创建过程中遇到任何失败,会执行删除虚拟机的操作,避免产生垃圾资源。

剧本 setup_mysql.yml

这个剧本的重点在于 post_tasks 里

  1. 在新虚拟机创建完成之后,我们还是要通过 openstack-connector 作为跳板机来访问新虚拟机,这样才能保证 {{ vm_name }}-{{ ansible_date_time.epoch }} 这个值是可以被读取到的。
  2. 基于这个模板,在 post_tasks 里,include_role 部分可以随意发挥,把你要执行的 role 加进来。这样你可以做到一键创建虚拟机并部署应用到虚拟机。
  3. 最后我们通过 openstack-connector 里的 sendmail,发出一封邮件,包含机器的相关信息。
- hosts: openstack-connector
  vars:
    new_password: newtouch
    mysql_user: root
    mysql_password: newtouch
    bind_address: 0.0.0.0
    max_connections: 256
    innodb_buffer_pool_size: "{{ (ansible_memtotal_mb * 0.5) | round | int }}M"
    owner_email: "haibin.li@newtouch.cn"
  roles:
    - openstack.CreateServer
  post_tasks:
    - block:
        - include_role:
            name: newtouch.SystemConfig
        - include_role:
            name: newtouch.MySQL
        - include_role:
            name: newtouch.UpdateRootPass
      delegate_to: "{{ vm_name }}-{{ ansible_date_time.epoch }}"
      delegate_facts: True
    - name: Send mail with instance info
      mail:
        host: localhost
        from: 'ansible@newtouch.com>'
        to: '{{ owner_email }}'
        subject: "Server {{ vm_name | upper }}-{{ ansible_date_time.epoch }} has been successfully created."
        body: |
          The server {{ vm_name | upper }}-{{ ansible_date_time.epoch }} info:

          - IP: {{ vm_ip }}
          - SYSTEM USER: root
          - SYSTEM PASS: {{ new_password }}
          - MYSQL USER:  {{ mysql_user }}
          - MYSQL PASS:  {{ mysql_password }}

          Please keep this email safe.
        charset: utf8

本剧本在 Ansible Tower 里也通过测试

我们通过问卷方式获取需要的变量值,一个简单的数据库虚拟机就可以实现一键创建了。开发人员再也不用等待运维人员的协助。

__2017_09_01_19_24_57

目录
相关文章
|
3月前
|
NoSQL 前端开发 应用服务中间件
若依框架---部署到虚拟机
若依框架---部署到虚拟机
48 0
|
3月前
|
存储 安全 Java
【数据结构】栈的使用|模拟实现|应用|栈与虚拟机栈和栈帧的区别
【数据结构】栈的使用|模拟实现|应用|栈与虚拟机栈和栈帧的区别
28 0
|
4月前
|
Web App开发 Java Linux
Nexus【部署 02】最新版本 nexus-3.35.0-02-unix.tar.gz 安装配置启动及测试(JDK版本+虚拟机参数配置说明)
Nexus【部署 02】最新版本 nexus-3.35.0-02-unix.tar.gz 安装配置启动及测试(JDK版本+虚拟机参数配置说明)
117 0
|
1月前
|
存储 Kubernetes 云计算
云计算基础与实战:从虚拟机到容器化应用
云计算基础与实战:从虚拟机到容器化应用
32 0
|
3月前
|
Shell
openstack 查询网络的port 关联的虚拟机
在OpenStack中,可以通过以下步骤查询网络的端口关联的虚拟机: 打开命令行终端,并使用OpenStack的命令行工具(如openstack命令行客户端)登录到OpenStack平台。 执行以下命令来获取所有端口的列表: bash openstack port list 这将显示所有端口的列表,包括端口ID、网络ID、MAC地址等信息。 3. 从端口列表中,找到与虚拟机相关的端口。通常情况下,虚拟机的端口具有与虚拟机实例相关的标识符,例如MAC地址或端口ID。 4. 执行以下命令来获取虚拟机实例的详细信息: bash openstack instance show <instanc
|
3月前
|
数据中心 容器
容器与虚拟机的区别:以Web应用部署为例
容器与虚拟机的区别:以Web应用部署为例
39 0
|
3月前
|
存储 Kubernetes 安全
虚拟机测试Windows Server 2016原地升级2019,应用和数据完美保留
Windows Server 2016可以无缝升级到2019版本,确保应用程序和数据在原地升级过程中完整保留。
104 0
|
4月前
|
存储 Ubuntu KVM
Ubuntu部署OpenStack踩坑指南:还要看系统版本?
Ubuntu部署OpenStack踩坑指南:还要看系统版本?
92 0
Ubuntu部署OpenStack踩坑指南:还要看系统版本?
|
4月前
|
机器学习/深度学习 Ubuntu Linux
如何在Ubuntu 23.10部署KVM并创建虚拟机?
如何在Ubuntu 23.10部署KVM并创建虚拟机?
70 0
|
4月前
|
云计算
云计算|OpenStack|社区版OpenStack安装部署文档(十--- 调整配额限制和解决建虚拟机报错---No valid host was found---Rocky版)
云计算|OpenStack|社区版OpenStack安装部署文档(十--- 调整配额限制和解决建虚拟机报错---No valid host was found---Rocky版)
61 0