본문 바로가기
INFRA/Operation

Azure Ansible 설치 및 리소스 배포

by BTC_지수 2022. 5. 30.

안녕하세요!! 

'하씨가문의 영광' 입니다!

오늘은 Azure Ansible 설치 및 리소스 배포를 해볼 차례입니다

바로 시작해볼까요!! 고고~~

1. 리소스 그룹생성

 

저는 'startansible1' 이름의 리소스 그룹을 만들었습니다 :>

 

2. Ansible 가상머신 생성

자! 이제 저는 Azure CLI를 통해 가상머신을 생성해보겠습니다.

CLI를 통해 다음과 같은 코드를 입력해줍니다.

az vm create \
--resource-group startAnsible1 \
--name myvm \
--image OpenLogic:CentOS:7.7:latest \
--admin-username azureuser \
--admin-password password@12345

다음으로 Azure CLI를 통해 가상머신에 접속해줍니다.

 ssh azureuser@<가상머신공용IP>
 yes
 가상머신 비밀번호 입력

 

3. 가상머신에서 Ansible 구성

필요한 패키지를 업데이트&다운해주고,  ansible을 설치해줍니다.

sudo yum update -y  
sudo yum install -y python3-pip
sudo pip3 install --upgrade pip
pip3 install "ansible==2.9.17"
pip3 install ansible[azure]
4. Azure CLI에서 Ansible 역할 생성

Azure CLI를 통해 역할을 생성해줍니다.

az ad sp create-for-rbac --name ansible \
            --role Contributor \
            --scopes /subscriptions/<자신의 구독ID 생성>

역할을 생성한 다음! 역할 부여를 해줍니다.

여기 '- appld' 는 위에서 역할 생성시 발급되니 다음과 같이 appld 를 입력해줍니다.

az role assignment create --assignee <appId> --role contributor
5. Azure 자격증명 생성
mkdir ~/.azure
vi ~/.azure/credentials

Credentials파일에 다음과 같이 차례대로 자격증명 내용을 입력해줍니다.

- 자신의 구독 ID 입력
- rbac를 생성하면 나오는 내용의 자신의 AppID 입력
- rbac를 생성하면 나오는 내용의 secret은 password 입력
- rbac를 생성하면 나오는 내용의 tenant는 tenant 입력
[default]
subscription_id=<자신의 구독 ID>
client_id=<principalName> 
secret=<password>  
tenant=<tenant name>
6. Ansible 버전 확인

Ansible이 잘 설치가 되었는지 버전을 확인해봅니다.

[azureuser@myvm ~]$ ansible --version
/home/azureuser/.local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
  from cryptography.exceptions import InvalidSignature
ansible 2.9.17
  config file = None
  configured module search path = ['/home/azureuser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/azureuser/.local/lib/python3.6/site-packages/ansible
  executable location = /home/azureuser/.local/bin/ansible
  python version = 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
7. Ansible를 통한 리소스 배포

다음 .yaml 파일에는 다음과 같은 내용이 포함되어있습니다.

- 리소스 배포 내용

- 리소스 그룹

- 네트워크, 서브넷, 네트워크 인터페이스, 공용IP

- 가상머신

[azureuser@myvm ~]$ vi rg_vn_subnet_pubip_nic_vm.yaml

- name: Create Azure VM
  hosts: localhost
  connection: local
  tasks:
  - name: Create resource group
    azure_rm_resourcegroup:
      name: myResourceGroup
      location: eastus
  - name: Create virtual network
    azure_rm_virtualnetwork:
      resource_group: myResourceGroup
      name: myVnet
      address_prefixes: "10.0.0.0/16"
  - name: Add subnet
    azure_rm_subnet:
      resource_group: myResourceGroup
      name: mySubnet
      address_prefix: "10.0.1.0/24"
      virtual_network: myVnet
  - name: Create public IP address
    azure_rm_publicipaddress:
      resource_group: myResourceGroup
      allocation_method: Static
      name: myPublicIP
    register: output_ip_address
  - name: Public IP of VM
    debug:
      msg: "The public IP is {{ output_ip_address.state.ip_address }}."
  - name: Create Network Security Group that allows SSH
    azure_rm_securitygroup:
      resource_group: myResourceGroup
      name: myNetworkSecurityGroup
      rules:
        - name: SSH
          protocol: Tcp
          destination_port_range: 22
          access: Allow
          priority: 1001
          direction: Inbound
  - name: Create virtual network interface card
    azure_rm_networkinterface:
      resource_group: myResourceGroup
      name: myNIC
      virtual_network: myVnet
      subnet: mySubnet
      public_ip_name: myPublicIP
      security_group: myNetworkSecurityGroup
  - name: Create VM
    azure_rm_virtualmachine:
      resource_group: myResourceGroup
      name: helloanvm
      vm_size: Standard_DS1_v2
      admin_username: azureuser
      admin_password: ~1q2w3e4r5t6y
      ssh_password_enabled: true
      network_interfaces: myNIC
      image:
        offer: CentOS
        publisher: OpenLogic
        sku: '7.5'
        version: latest
8. 리소스 배포 확인

리소스배포가 잘되었는지 확인해봅니다.

짜안~

마지막으로 만들어진 가상머신에 ssh로 접속해봅니다.

ssh azureuser@<가상머신공용IP>
9. 마지막, 리소스 정리

클라우드를 공부하면서 제일 중요한건 쓸데없는비용이 나가지않도록 정리를 잘해야겠지요?

배포까지 확인했다면 Portal 또는 remove.yaml을 생성하여 리소스까지 깔꼼하게 마지막정리까지 해줍니다.

 

 

여기까지 Ansible을 통해 간단한 Hands-On을 해보았는데요! 많은 도움이 되셨으면 좋겠습니다 :>

다음시간에 또 뵙겠습니다~~

댓글