Szukaj…


Wprowadzenie

OpenStack to platforma oprogramowania typu open source do przetwarzania w chmurze. Instancje Linuksa można uruchamiać / zatrzymywać ręcznie za pomocą graficznego interfejsu internetowego lub zautomatyzować dzięki modułowi chmury openstack ansible.

Konfiguracja ansible może być trudna, ale po dobrze skonfigurowanym użyciu jest naprawdę łatwa i wydajna w testowaniu i środowisku ciągłej integracji.

Parametry

parametry Komentarze
hosts: localhost Polecenia OpenStack są uruchamiane z naszego hosta lokalnego
gather_facts: False Nie musimy zbierać informacji na temat naszego lokalnego hosta
auth_url: https://openstack-identity.mycompany.com/v2.0 użyj adresu URL V2.0
stan: obecny „obecny” / „nieobecny”, aby utworzyć / usunąć instancję
validate_certs: False przydatne, jeśli https używa certyfikatów z podpisem własnym
sieć: „{{NetworkName}}” (opcjonalny)
auto_ip: tak (opcjonalny)

Uwagi

Sprawdź swoją wersję Ansible

Sprawdź, czy zainstalowane są odpowiednie wersje oprogramowania:

  • ansible> = 2.0
  • python> = 2.6
  • moduł cieni dla Pythona
$ansible --version
ansible 2.2.0.0

$python --version
Python 2.7.5

Zainstaluj „cień” komponentu Pythona używanego do pilotowania openstack.

$pip install shade

Uwaga: jeśli używasz firmowego serwera proxy, zawsze dobrze jest znać właściwą składnię pip

$pip install --proxy proxy_ip:proxy_port shade

Zbierz informacje z GUI OpenStack, aby skonfigurować Ansible


Nasz najemca openstack jest już ustawiony:

  • wirtualna sieć LAN daje instancjom prywatne adresy IP
  • router wirtualny mapuje publiczny adres IP na prywatny adres IP
  • wygenerowano klucz bezpieczeństwa
  • mamy domyślną konfigurację zapory ogniowej dla ssh i portu 80
  • jesteśmy w stanie uruchomić instancję dzięki interfejsowi sieciowemu OpenStack

Zbierzmy wszystkie potrzebne informacje z tego interfejsu internetowego.

Informacje dotyczące uwierzytelnienia można znaleźć w pliku openstack.rc. ten plik można pobrać za pomocą interfejsu internetowego OpenStack w [dostęp i bezpieczeństwo / dostęp API].

$cat openstack.rc
#!/bin/bash
 
# To use an OpenStack cloud you need to authenticate against the Identity
# service named keystone, which returns a **Token** and **Service Catalog**.
# The catalog contains the endpoints for all services the user/tenant has
# access to - such as Compute, Image Service, Identity, Object Storage, Block
# Storage, and Networking (code-named nova, glance, keystone, swift,
# cinder, and neutron).
#
# *NOTE*: Using the 2.0 *Identity API* does not necessarily mean any other
# OpenStack API is version 2.0. For example, your cloud provider may implement
# Image API v1.1, Block Storage API v2, and Compute API v2.0. OS_AUTH_URL is
# only for the Identity API served through keystone.
export OS_AUTH_URL=https://openstack-identity.mycompany.com/v3

# With the addition of Keystone we have standardized on the term **tenant**
# as the entity that owns the resources.
export OS_TENANT_ID=1ac99fef77ee40148d7d5ba3e070caae
export OS_TENANT_NAME="TrainingIC"
export OS_PROJECT_NAME="TrainingIC"

# In addition to the owning entity (tenant), OpenStack stores the entity
# performing the action as the **user**.
export OS_USERNAME="UserTrainingIC"

# With Keystone you pass the keystone password.
echo "Please enter your OpenStack Password: "
read -sr OS_PASSWORD_INPUT
export OS_PASSWORD=$OS_PASSWORD_INPUT

# If your configuration has multiple regions, we set that information here.
# OS_REGION_NAME is optional and only valid in certain environments.
export OS_REGION_NAME="fr"
# Don't leave a blank variable, unset it if it was empty
if [ -z "$OS_REGION_NAME" ]; then unset OS_REGION_NAME; fi 

Otrzymujemy OS_AUTH_URL, OS_TENANT_NAME, OS_USERNAME.

Wersja interfejsu API uwierzytelniania: OS_AUTH_URL

Uwaga na wersję interfejsu API uwierzytelniania. Domyślnie v3 jest aktywowana, ale ansible potrzebuje v2.0. Otrzymujemy adres URL i ustawiamy V2.0 zamiast V3: https://openstack-identity.mycompany.com/v2.0

Informacje o VM

Utwórz instancję za pomocą interfejsu internetowego OpenStack i uzyskaj nazwę obrazu, smaku, klucza, sieci, grupy zabezpieczeń.

Utwórz plik ./group_vars/all ze wszystkimi potrzebnymi informacjami.

$vi ./group_vars/all
# Authentication
AuthUserName: UserTrainingIC
AuthPassword: PasswordTrainingIC
TenantName: TrainingIC

# VM infos
ImageName: CentOS-7-x86_64-GenericCloud-1607
FlavorName: m1.1cpu.1gb
InfraKey: KeyTrainingIC
NetworkName: NetPrivateTrainingIC
SecurityGroup: default

Napisz odpowiadający podręcznik, aby utworzyć instancję

Użyjmy polecenia „os_server” z modułu „Cloud” [ http://docs.ansible.com/ansible/os_server_module.html] . Zmienne są zdefiniowane w ./group_vars/all.

$vi launch_compute.yml
- name: launch a compute instance
  hosts: localhost
  gather_facts: False
  tasks:
  - name: Create and launch the VM
    os_server:
      auth:
        auth_url: https://openstack-identity.mycompany.com/v2.0
        username: "{{ AuthUserName }}"
        password: "{{ AuthPassword }}"
        project_name: "{{ TenantName }}"
      state: present
      validate_certs: False
      name: "MyOwnPersonalInstance"
      image: "{{ ImageName }}"
      key_name: "{{ InfraKey }}"
      timeout: 200
      flavor:   "{{ FlavorName }}"
      security_groups: "{{ SecurityGroup }}"
      network: "{{ NetworkName }}"
      auto_ip: yes
$ ansible-playbook  -s launch_compute.yml
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [launch a compute instance] ***********************************************
TASK [Create and launch the VM] ************************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0

Zbierz informacje o naszej nowej instancji

Użyj polecenia „os_server_facts” z modułu „Cloud” [ http://docs.ansible.com/ansible/os_server_module.html] . Zmienne są zdefiniowane w ./group_vars/all, a nazwa instancji znajduje się na serwerze: „MyOwnPersonalInstance”.

$vi get_compute_info.yml
- name: Get and print instance IP
  hosts: localhost
  gather_facts: False
  tasks:
  - name: Get VM infos
    os_server_facts:
      auth:
        auth_url: https://openstack-identity.mygroup/v2.0
        username: "{{ AuthUserName }}"
        password: "{{ AuthPassword }}"
        project_name: "{{ TenantName }}"
      validate_certs: False
      server: "MyOwnPersonalInstance"

  - name: Dump all
    debug:
      var: openstack_servers
$ansible-playbook  -s get_compute_info.yml
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [Get and print instance IP] ***********************************************
TASK [Get VM IP] ***************************************************************
ok: [localhost]
TASK [Affichage] ***************************************************************
ok: [localhost] => {
    "openstack_servers": [
        {
            "OS-DCF:diskConfig": "MANUAL",
            "OS-EXT-AZ:availability_zone": "fr",
            "OS-EXT-STS:power_state": 1,
            "OS-EXT-STS:task_state": null,
[...]
            "volumes": []
        }
    ]
}

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

To bardzo gadatliwe. Wyświetlanych jest wiele informacji. Zwykle tylko adres IP jest potrzebny do uzyskania dostępu do nowej instancji za pośrednictwem SSH.

Uzyskaj publiczny adres IP nowej instancji

Zamiast drukować wszystkie informacje, drukujemy tylko adres IP pierwszej instancji o nazwie „MyOwnPersonalInstance”. Zwykle to wszystko, czego potrzebujemy.

$vi get_compute_ip.yml
- name: Get and print instance IP
  hosts: localhost
  gather_facts: False
  tasks:
  - name: Get VM infos
    os_server_facts:
      auth:
        auth_url: https://openstack-identity.mycompany.com/v2.0
        username: "{{ AuthUserName }}"
        password: "{{ AuthPassword }}"
        project_name: "{{ TenantName }}"
      validate_certs: False
      server: "MyOwnPersonalInstance"

  - name: Dump IP
    debug:
      var: openstack_servers[0].interface_ip

Usuń naszą instancję

Aby usunąć naszą instancję, użyj ponownie komendy os_server ze wszystkimi informacjami uwierzytelniającymi i po prostu zamień „stan: obecny” na „stan: nieobecny”.

$vi stop_compute.yml
- name: launch a compute instance
  hosts: localhost
  gather_facts: False
  tasks:
  - name: Create and launch the VM
    os_server:
      auth:
        auth_url: https://openstack-identity.mygroup/v2.0
        username: "{{ AuthUserName }}"
        password: "{{ AuthPassword }}"
        project_name: "{{ ProjectName }}"
      state: absent
      validate_certs: False
      name: "{{ TPUser }}"
      timeout: 200


Modified text is an extract of the original Stack Overflow Documentation
Licencjonowany na podstawie CC BY-SA 3.0
Nie związany z Stack Overflow