惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
V
V2EX
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
Vercel News
Vercel News
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
小众软件
小众软件
L
Lohrmann on Cybersecurity
GbyAI
GbyAI
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
AWS News Blog
AWS News Blog
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
Kaspersky official blog
B
Blog RSS Feed
G
Google Developers Blog
量子位
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
雷峰网
雷峰网
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
F
Full Disclosure
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
The Hacker News
The Hacker News
U
Unit 42
S
SegmentFault 最新的问题
I
InfoQ
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
罗磊的独立博客
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes

Superuser

A Hybrid Private Cloud as an AI Factory – Superuser How India’s Payment Backbone Runs on Open Source – Superuser What AI Builders Can Learn From a Decade of Open Source CI – Superuser How Pinaka ZTi Builds Sovereign Clouds Governments Can Actually Trust – Superuser KDDI’s Private Cloud Supporting Communications – Superuser When Vietnam’s Cloud Infrastructure Is No Longer the Bottleneck – Superuser Sovereign AI Cloud – Superuser How France’s National Railway Runs on Open Source – Superuser Why Digital Sovereignty Depends on Invisible Labor – Superuser Ongoing Development and Next Steps for DOCOMO and Tacker – Superuser Why AI Agents Need Stronger Sandboxing and What the Kata Containers Community Is Doing About It – Superuser A Guide to Manila Security and Terraform Integration – Superuser A Strategic Growth Opportunity for Open Infrastructure – Superuser NTT DOCOMO’s Tacker Activity – Superuser NTT DOCOMO’s Journey of Virtualization for Mobile Networks – Superuser How Rapifuzz CyberKshetra Built a Scalable Cyber Range on OpenStack (and Cut Costs by 60%) Integration of the Octavia module (Load Balancer as a Service) in an OpenStack Cloud Environment – Part 2 – Superuser OpenStack Case Study: CloudVPS – Superuser The 10th China Open Source Hackathon Recap: Projects, Talks, and More – Superuser Inside CERN’s Open Data portal – Superuser Taking the OpenStack ops manuals to the next level – Superuser Making your first contact with OpenStack – Superuser How tech giant Tencent uses OpenStack – Superuser Managing port level security in OpenStack – Superuser Simple auto scaling environment with Heat – Superuser
Using Ansible 2.0 to launch a server on OpenStack – Superuser
Michael Solberg · 2016-09-10 · via Superuser

In my previous article, Using Ansible for continuous integration on OpenStack, we looked at how to use the nova_compute module in Ansible 1.9 to launch instances and configure them in a simple Ansible playbook. A set of new modules for interacting with OpenStack were released in Ansible 2.0. In this article, we’ll look at how to use the new modules to launch and configure a web server in a simple playbook.

The new modules in Ansible 2.0 use the Python Shade library. Shade isn’t currently packaged for CentOS or Red Hat Enterprise Linux and if you’re on one of those distributions, you’ll need to install it using pip. Shade is available as an operating system package for the latest versions of Ubuntu and Fedora and can be installed via the normal mechanisms.

The next biggest change in how these modules work is around authentication. With the nova_compute module, authentication details were specified as options in the task. With the new os_server module, authentication is pulled from the environment variables which are set by the “openrc” script. Most OpenStack installations will have an option to download the openrc for your specific tenant in Horizon under “Access & Security”. To set these variables, just source the openrc script before running the playbook like so:

$ . ./openrc.sh

The script you download from Horizon will ask for you to enter your OpenStack user password and then will load that into an environment variable. If you’re running your playbook non-interactively as a part of a CI work flow, you’ll want to specify a password in the openrc script by setting the OS_PASSWORD environment variable.

Once Shade is installed, writing and running the playbook are very similar to the previous example. Here’s an example play which will launch an instance in an OpenStack cloud:

- name: Deploy on OpenStack
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Deploy an instance
      os_server:
      state: present
      name: webserver
      image: centos-7-x86_64-genericcloud
      key_name: root
      wait: yes
      flavor: m1.small
      auto_floating_ip: yes
      network: private
      meta:
        hostname: webserver.localdomain

The play is a local action, similar to the nova_compute module, and we specify the same basic parameters. One thing that’s a little different is that there are several options for specifying the network settings for the instance. In this example, we’re attaching a VIF to the “private” network and requesting a floating IP. You can also specify multiple networks or you can create a port with the os_port module and use that for your network interface. To run the play, source your openrc script and call ansible-playbook on a file which has the above contents.

$ . ./openrc.sh
$ ansible-playbook openstack_deploy.yaml

This should deploy an instance on OpenStack named “webserver”. The output will look something like this:

PLAY [Deploy on OpenStack] 
****************************************************

TASK: [Deploy an instance] 
****************************************************
changed: [localhost]

PLAY RECAP 
********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0

To configure the instance after launch, we’ll use the same strategy we employed with the nova_compute module. The first play will launch the instance and add it to the in-memory inventory with the add_host module and then we’ll run a second play on the instance once it’s in the inventory. I’ve also added a wait condition to make sure that the instance is responding to SSH before we try to execute the second play.

- name: Deploy on OpenStack
  hosts: localhost
  gather_facts: false
  tasks:
  - name: Deploy an instance
    os_server:
      state: present
      name: webserver
      image: centos-7-x86_64-genericcloud
      key_name: root
      wait: yes
      flavor: m1.small
      auto_floating_ip: yes
      network: private
      meta:
        hostname: webserver.localdomain
      register: webserver

  - name: Wait for SSH on the Instance
    command: >
      ssh -oBatchMode=yes -oStrictHostKeyChecking=no
      centos@{{webserver.server.public_v4}} true
    register: result
    until: result|success
    retries: 30
    delay: 10

  - name: Add CentOS Instance to Inventory
    add_host: name=webserver groups=webservers
              ansible_ssh_host={{ webserver.server.public_v4 }}

- hosts: webservers
  remote_user: centos
  sudo: yes
  Tasks:
    - name: ensure apache is at the latest version
      yum: name=httpd state=latest
    - name: make sure apache is running
      service: name=httpd state=running

Another change to note in the example given above is that we’re referencing the facts of the newly created instance by the name of the instance (i.e. “webserver.server”). This is particularly helpful in plays which launch several instances – each instance is referenced by the unique name given to it. Assuming everything goes well, you should now have a running instance in OpenStack which has a floating IP assigned and has the Apache web server running on it. The second play can then be embellished to include downloading the latest build of your software from a Jenkins job or a code repository.

The os_server module is well documented with examples. The full list of OpenStack modules is also available, along with the modules used to interact with other cloud providers. Additional modules for things like launching Heat stacks were added to the “extras” collection in Ansible 2.2.

Michael Solberg contributed this original post.

Superuser is always interested in opinion pieces and how-tos. Please get in touch: [email protected]

Cover Photo // CC BY NC

Tags:

  • Author
  • Recent Posts