0

我们有一个剧本来在 Mincrosoft azure 中创建一个 Linux VM,其职责是执行安装后的操作,例如安装一些应用程序包。我们的剧本运行良好并且能够在 azure 中部署 VM,但是在部署后配置 VM 的第二个角色没有在 VM 上运行,因为我们无法将 vm(IP/主机名)传递给第二个角色角色。

我们要实现的是使用 ansible playbook/role 部署 VM,在机器部署后运行角色以配置业务特定任务。

小路:

下面是所有 ansible 戏剧和角色的路径。这里roles的文件夹包含所有安装后的任务,我相信会有更好的方法在那里保持test-creatVM-sur.yml角色本身,但作为一个学习者我有点挣扎..

$ ls -l /home/azure1/ansible_Dir
-rw-r-----. 1 azure1 hal 1770 Sep 17 17:03 test-creatVM-sur.yml
-rw-r-----. 1 azure1 hal  320 Sep 17 22:30 licence-test.yml
drwxr-x---. 6 azure1 hal 4096 Sep 17 21:46 roles

我的主要播放文件:

$ cat license-test.yml

---
- name: create vm
  hosts: localhost
  connection: local
  become: yes
  become_method: sudo
  become_user: root

  vars:
    Res_Group: "some_value"
    LOCATION: "some_value"
    VNET: "some_value"
    IMAGE_ID: "some_value"
    SUBNET: "some_value"
    KEYDATA: "some_value"
    DISK_SIZE: 100
    DISK_TYPE: Premium_LRS

  tasks:
    - name: include task
      include_tasks:
        file: creattest_VM.yml  <-- This portion works fine

- hosts: "{{ VM_NAME }}" <-- this portion does not work as it's not able to fetch the newly created VM name.
  become: yes
  become_method: sudo
  become_user: root

  roles:
    - azure_license
...

在 azure 中创建 VM 的Play ( test-creatVM-sur.yml) 如下:

---
- name: Create Network Security Group that allows SSH
  azure_rm_securitygroup:
    resource_group: "{{ Res_Group }}"
    location: "{{ LOCATION }}"
    name: "{{ VM_NAME }}-nsg"
    rules:
      - name: SSH
        protocol: Tcp
        destination_port_range: 22
        access: Allow
        priority: 100
        direction: Inbound

- name: Create virtual network interface card
  azure_rm_networkinterface:
    resource_group: "{{ Res_Group }}"
    location: "{{ LOCATION }}"
    name: "{{ VM_NAME }}-nic1"
    subnet: "{{ SUBNET }}"
    virtual_network: "{{ VNET }}"
    security_group: "{{ VM_NAME }}-nsg"
    enable_accelerated_networking: True
    public_ip: no
    state: present

- name: Create VM
  azure_rm_virtualmachine:
    resource_group: "{{ Res_Group }}"
    location: "{{ LOCATION }}"
    name: "{{ VM_NAME }}"
    vm_size: Standard_D4s_v3
    admin_username: automation
    ssh_password_enabled: false
    ssh_public_keys:
      - path: /home/automation/.ssh/authorized_keys
        key_data: "{{ KEYDATA }}"
    network_interfaces: "{{ VM_NAME }}-nic1"
    os_disk_name: "{{ VM_NAME }}-osdisk"
    managed_disk_type: "{{ DISK_TYPE }}"
    os_disk_caching: ReadWrite
    os_type: Linux
    image:
      id: "{{ IMAGE_ID }}"
      publisher: redhat
    plan:
      name: rhel-lvm78
      product: rhel-byos
      publisher: redhat

- name: Add disk to VM
  azure_rm_manageddisk:
    name: "{{ VM_NAME }}-datadisk01"
    location: "{{ LOCATION }}"
    resource_group: "{{ Res_Group }}"
    disk_size_gb: "{{ DISK_SIZE }}"
    managed_by: "{{ VM_NAME }}"

- name: "wait for 3 Min"
  pause:
    minutes: 3


...

编辑:

我设法将变量定义为一个单独name_vars的下include_vars

---
- name: create vm
  hosts: localhost
  connection: local
  become: yes
  become_method: sudo
  become_user: root
  tasks:
    -  include_vars:  name_vars.yml
    -  include_tasks: creattest_VM.yml

- name: Apply license hardening stuff
  hosts: "{{ VM_NAME }}"
  become: yes
  become_method: sudo
  become_user: root

  roles:
    - azure_license
...

它在做了一些肮脏的黑客攻击后工作,但这看起来不合适,因为我正在创建一个test要放在那里的库存VM_NAME以及-e下面的一个额外变量。

$ ansible-playbook -i test -e VM_NAME=mylabhost01.hal.com licence-test.yml -k -u my_user_id

任何帮助都感激不尽。

4

0 回答 0