使用Ansible URI模块创建OpenStack云主机
创建一个Ansible playbook文件,例如 create_instance.yml,并添加以下内容:
- name: Create instance on OpenStack
hosts: localhost
gather_facts: no
vars:
openstack_server: "http://openstack.example.com"
username: "admin"
password: "password"
project_name: "admin"
domain_name: "Default"
region_name: "RegionOne"
server_name: "new-instance"
flavor_id: "ecs-2C-4G-40GB"
key_name: "wanghongwei public key"
image_id: "05432f95-a1d9-4c55-b1f3-5025b864424d"
network_id: "6df6996b-920c-41d1-bb79-436a8a5b3f56"
tasks:
- name: Get OpenStack token
uri:
url: "{{ openstack_server }}:5000/v3/auth/tokens"
method: POST
body_format: json
body:
auth:
identity:
methods:
- password
password:
user:
name: "{{ username }}"
domain:
name: "{{ domain_name }}"
password: "{{ password }}"
scope:
project:
name: "{{ project_name }}"
domain:
name: "{{ domain_name }}"
headers:
Content-Type: "application/json"
validate_certs: false
return_content: true
status_code: 201
register: token_response
- name: Set OpenStack token as a fact
set_fact:
openstack_token: "{{ token_response.x_subject_token }}"
when: token_response.status == 201
- name: Create OpenStack server
uri:
url: "{{ openstack_server }}:8774/v2.1/servers"
method: POST
headers:
Content-Type: "application/json"
X-Auth-Token: "{{ openstack_token }}"
body_format: json
body:
server:
name: "{{ server_name }}"
flavorRef: "{{ flavor_id }}"
imageRef: "{{ image_id }}"
key_name: "{{ key_name }}"
networks:
- uuid: "{{ network_id }}"
return_content: true
status_code: 202
register: create_result
- name: Print OpenStack instances
debug:
var: create_result.json
运行Ansible playbook:
ansible-playbook create_instance.yml
作者:wanghongwei
版权声明:本作品遵循<CC BY-NC-ND 4.0>版权协议,商业转载请联系作者获得授权,非商业转载请附上原文出处链接及本声明。