Weitere ähnliche Inhalte Ähnlich wie OpenStackSDK with Ansible (20) Mehr von Hideki Saito (20) Kürzlich hochgeladen (12) OpenStackSDK with Ansible2. CONFIDENTIAL
自己紹介
氏名: さいとう ひでき
Twitter: @saito_hideki
所属1: 日本OpenStackユーザ会
所属2: Ansibleユーザ会
所属3: Ansible Organization
所属4: Red Hat K.K.
Upstreamの仕事: Ansible / AWX のメンテナンス
Downstreamの仕事: Ansible Towerのメンテナンス
趣味: AnsibleとOpenStack
10. CONFIDENTIAL
OpenStack Client Library - shade(1/3)
shadeは、OpenStackのCI/CDインフラを支えるインフラチームが、自身で利用す
ることを目的に開発したクライアントライブラリです。
インフラチームが自身で利用することを目的としているため、Ansible向けにデザイ
ンされた機能が多いのが特徴です。
> https://docs.openstack.org/shade/latest/
OpenStackの各コンポーネントが持っているクライアントライブラリに依存すること
なく、それぞれのコンポーネントのAPIを利用できます。
openstacksdkに置き換えられることになっており、新機能の追加は行われていま
せん。
11. CONFIDENTIAL
OpenStack Client Library - shade(2/3)
$ pip install shade
clouds:
openstack:
auth:
auth_url: http://192.168.202.31/identity/v3
username: "sdkadmin"
password: "changeme"
project_id: f81135218bd647a08f9b87e568d55f39
project_name: "OSDT201901"
user_domain_name: "Default"
region_name: "RegionOne"
interface: "public"
identity_api_version: 3
> shadeのインストール:
> OpenStackに対する接続設定ファイル(clouds.yaml)の作成:
12. CONFIDENTIAL
OpenStack Client Library - shade(3/3)
#!/usr/bin/env python
import shade
cloud = shade.openstack_cloud(cloud='openstack')
name = 'test01'
flavor = cloud.get_flavor('m1.nano')
image = cloud.get_image('cirros-0.4.0-x86_64-disk')
public_net = cloud.get_network('public')
testing_net = cloud.get_network('testing')
security_groups = ['OSDT201900_TESTING']
key_name = 'OSDT201900_KEY'
router = cloud.create_router(ext_gateway_net_id=public_net.id) #(1)
cloud.add_router_interface(router=router, subnet_id=testing_net.subnets[0]) #(2)
cloud.create_server(
name=name, image=image, flavor=flavor, wait=True, auto_ip=True,
network=testing_net, security_groups=security_groups, key_name=key_name) #(3)
> サンプルコード - 仮想ルータと仮想マシンインスタンスの作成:
13. CONFIDENTIAL
OpenStack Client Library - openstacksdk(1/4)
Openstacksdkのコンセプトはshadeと同様です。
各コンポーネント毎に用意されているPython向けのクライアントライブラリ
(python-novaclient, python-neutronclientなど)を統一し、利用者がOpenStackの
機能をコンポーネント横断で簡単に利用できるよう配慮されています。
> https://docs.openstack.org/openstacksdk/latest/
現在では、shadeに代わる統一されたクライアントライブラリとしてAnsibleのダイナ
ミックインベントリやモジュールから利用されています。
出典: https://github.com/openstack/shade/blob/master/README.rst
shade has been superceded by openstacksdk and no longer takes new features.
14. CONFIDENTIAL
OpenStack Client Library - openstacksdk(2/4)
$ pip install openstacksdk
clouds:
openstack:
auth:
auth_url: http://192.168.202.31/identity/v3
username: "sdkadmin"
password: "changeme"
project_id: f81135218bd647a08f9b87e568d55f39
project_name: "OSDT201901"
user_domain_name: "Default"
region_name: "RegionOne"
interface: "public"
identity_api_version: 3
> shadeのインストール:
> OpenStackに対する接続設定ファイル(clouds.yaml)の作成
15. CONFIDENTIAL
OpenStack Client Library - openstacksdk(3/4)
#!/usr/bin/env python
import openstack.cloud
conn = openstack.connect(cloud='openstack')
name = 'test02'
flavor = conn.get_flavor('m1.nano')
image = conn.get_image('cirros-0.4.0-x86_64-disk')
public_net = conn.get_network('public')
testing_net = conn.get_network('sdk-testing')
security_groups = ['OSDT201901_TESTING']
key_name = 'OSDT201901_KEY'
router = conn.create_router(ext_gateway_net_id=public_net.id)
conn.add_router_interface(router=router, subnet_id=testing_net.subnets[0])
conn.create_server(
name=name, image=image, flavor=flavor, wait=True, auto_ip=True,
network=testing_net, security_groups=security_groups, key_name=key_name)
> サンプルコード - 仮想ルータと仮想マシンインスタンスの作成:
openstackモジュールをimportしてコネクションを生成その
後の処理は、shadeとほぼ変わらない
16. CONFIDENTIAL
OpenStack Client Library - openstacksdk(4/4)
openstacksdkを利用することで、このようなAPIシーケンスを意識することなく、OpenStackのコントロールプ
レーンを管理できます。
client Identity Compute Image Network
認証・認可
flavor情報取得
OSイメージ情報取得
ネットワーク情報取得
ルータの作成
仮想マシンの作成
floating_ipの払い出し
ネットワーク接続
floating_ipの割り当て
19. CONFIDENTIAL
紀元後
紀元前
openstacksdkの活用例 - オペレーションの自動化(2/3)
Ansibleの歴史とOpenStack ~ Genesis - Van Halen時代
2012/04/03
0.0.2
2012/04/23
0.3
Baluchitherium
2012/05/23
0.4
Unchained
2012/07/04
0.5
Amsterdam
2012/08/06
0.6
Cabo
2012/09/06
0.7
Panama
2012/10/19
0.8
Cathedral
2012/11/30
v0.9
Dreams
2013/02/01
V1.0
Eruption
2013/04/01
V1.1
Mean Street
2013/06/10
V1.2
Right Now
openstack/rax module
rax dynamic inventory
2013/09/13
V1.3.0
Top of the World
2013/11/21
V1.4
Could This Be Magic
2014/02/28
V1.5.0
Love Walks In
2013/04/04
Grizzly
2014/08/06
V1.7.0
Summer Nights
2014/05/05
V1.6.0
And the Cradle
Will Rock
ここからリリースブランチが
作成されはじめる
2014/11/25
V1.8.0
You Really Got Me
2015/03/25
V1.9.0
Dancing In
the Street
Openstack dynamic inventory
2013/09/27
Folsom
2012/02/23
0.0.1
(Genesis)
20. CONFIDENTIAL
openstack inventory plugin
openstacksdkの活用例 - オペレーションの自動化(3/3)
Ansibleの歴史とOpenStack ~ Led Zeppelin時代
2016/01/12
2.0.0.0
Over the Hills
and Far Away
2016/05/25
2.1.0.0
The Song Remains
the Same
2016/11/01
2.2.0.0
The Battle of
Evermore
2017/04/12
2.3.0.0
Ramble On
2017/09/18
2.4.0.0
Dancing Days
2018/03/22
2.5.0
Kashmir
2018/06/28
2.6.0
Heartbreaker
2018/10/04
2.7.0
In the Light
2019-05-16
2.8.0
How Many More Times
Openstack連携機能が
shadeを利用する形で再実装
2015/10/15
Liberty
Openstack連携機能が
openstacksdkを利用する形で再実装
2018/02/28
Queens
2019/10/16
2.9.0
2017/08/30
Pike
21. CONFIDENTIAL
AnsibleとOpenStackの連携- Dynamic Inventory Plugin(1/2)
# clouds.yaml
plugin: openstack
expand_hostvars: yes
fail_on_errors: yes
clouds:
openstack:
auth:
auth_url: http://192.168.202.31/identity/v3
username: "sdkadmin"
password: "changeme"
project_id: f81135218bd647a08f9b87e568d55f39
project_name: "OSDT201901"
user_domain_name: "Default"
region_name: "RegionOne"
interface: "public"
identity_api_version: 3
ansible-inventory
plugins/inventory/openstack.py
(2) openstack.config.loader
(4) openstack.cloud.inventory
@all:
|--@RegionOne:
| |--test02
|--@RegionOne_nova:
| |--test02
|--@instance-3e7f3594-c554-4180-bb61-d1d4f760e16a:
| |--test02
|--@nova:
| |--test02
|--@openstack:
| |--test02
|--@openstack_RegionOne:
| |--test02
|--@openstack_RegionOne_nova:
| |--test02
|--@ungrouped:
openstacksdkを利用して、OpenStack管理下にある仮想マシンインスタンスのリストを取得する。
(3)
(1)
(2) (4)
(5)
(6)
(7)
openstacksdk
22. CONFIDENTIAL
AnsibleとOpenStackの連携- Dynamic Inventory Plugin(2/2)
# ansible/lib/ansible/plugins/inventory/openstack.py
# https://github.com/ansible/ansible/blob/stable-2.8/lib/ansible/plugins/inventory/openstack.py
...
try:
# Due to the name shadowing we should import other way
import importlib
sdk = importlib.import_module('openstack')
sdk_inventory = importlib.import_module('openstack.cloud.inventory')
client_config = importlib.import_module('openstack.config.loader')
HAS_SDK = True
except ImportError:
HAS_SDK = False
...
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
...
if not source_data:
clouds_yaml_path = self._config_data.get('clouds_yaml_path')
if clouds_yaml_path:
config_files = (clouds_yaml_path + client_config.CONFIG_FILES)
else:
config_files = None
...
cloud_inventory = sdk_inventory.OpenStackInventory(
config_files=config_files, private=self._config_data.get('private', False))
...
source_data = cloud_inventory.list_hosts(
expand=expand_hostvars, fail_on_cloud_config=fail_on_errors)
23. CONFIDENTIAL
AnsibleとOpenStackの連携 - OpenStack Modules(1/3)
# clouds.yaml
clouds:
openstack:
auth:
auth_url: http://192.168.202.31/identity/v3
username: "sdkadmin"
password: "changeme"
project_id: f81135218bd647a08f9b87e568d55f39
project_name: "OSDT201901"
user_domain_name: "Default"
region_name: "RegionOne"
interface: "public"
identity_api_version: 3
openstacksdkを利用して、OpenStack管理下に仮想マシンインスタンスを作成する。
# create_instance.yaml
---
- hosts: localhost
tasks:
- name: launch an instance
os_server:
state: present
cloud: openstack
name: test03
image: cirros-0.4.0-x86_64-disk
key_name: OSDT201901_KEY
timeout: 200
flavor: m1.nano
nics:
- net-name: sdk-testing
security_groups:
- OSDT201901_TESTING
auto_ip: yes
openstacksdk
ansible-playbook
modules/os_server.py
module_utils/openstack.py
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
24. CONFIDENTIAL
AnsibleとOpenStackの連携 - OpenStack Modules(2/3)
# ansible/lib/ansible/modules/cloud/openstack/os_server.py
# https://github.com/ansible/ansible/blob/stable-2.8/lib/ansible/modules/cloud/openstack/os_server.py
...
from ansible.module_utils.openstack import (
openstack_find_nova_addresses, openstack_cloud_from_module,
openstack_full_argument_spec, openstack_module_kwargs)
...
def _create_server(module, cloud):
server = cloud.create_server(
ip_pool=module.params['floating_ip_pools'],
ips=module.params['floating_ips'],
auto_ip=module.params['auto_ip'],
boot_volume=module.params['boot_volume'],
boot_from_volume=module.params['boot_from_volume'],
terminate_volume=module.params['terminate_volume'],
reuse_ips=module.params['reuse_ips'],
wait=module.params['wait'], timeout=module.params['timeout'],
**bootkwargs
)
...
def main():
...
sdk, cloud = openstack_cloud_from_module(module)
try:
if state == 'present':
_get_server_state(module, cloud)
_create_server(module, cloud)
25. CONFIDENTIAL
AnsibleとOpenStackの連携 - OpenStack Modules(3/3)
# lib/ansible/module_utils/openstack.py
# https://github.com/ansible/ansible/blob/stable-2.8/lib/ansible/module_utils/openstack.py
...
def openstack_cloud_from_module(module, min_version='0.12.0'):
from distutils.version import StrictVersion
try:
# Due to the name shadowing we should import other way
import importlib
sdk = importlib.import_module('openstack')
sdk_version = importlib.import_module('openstack.version')
...
cloud_config = module.params.pop('cloud', None)
try:
if isinstance(cloud_config, dict):
...
return sdk, sdk.connect(**cloud_config)
else:
return sdk, sdk.connect(
cloud=cloud_config,
auth_type=module.params['auth_type'],
auth=module.params['auth'],
region_name=module.params['region_name'],
verify=module.params['validate_certs'],
cacert=module.params['ca_cert'],
key=module.params['client_key'],
api_timeout=module.params['api_timeout'],
interface=module.params['interface'],
)
...