Ansible dynamic inventory - AWS EC2

Ansible aws_ec2 plugin 활용, AWS EC2 동적 Inventory 구성 Ansible plugin : aws_ec2 https://docs.ansible.com/ansible/2.9/plugins/inventory/aws_ec2.html{:target="_blank"} Get inventory hosts from Amazon Web Services EC2 Requirements python package : boto3, bo YAML 설정 파일 이름이 aws_ec2.(yml|yaml) 로 끝나야 함 $ ansible --version ansible 2.9.6 config file = /etc/ansible/ansible.cfg configured module search path = ['/home/cdecl/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3/dist-packages/ansible executable location = /usr/bin/ansible python version = 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0] # install boto3, bo $ pip3 install boto3 기본설정 aws_ec2.yaml plugin : 프러그인 이름, 필수 regions : 지정하면 더 빠른 조회 plugin: aws_ec2 regions: - ap-northeast-2 # .aws/credendial 파일이 없으면 aws_access_key_id: AKIA3************** aws_secret_access_key: 1oxRajyBX********************** ~/.aws/credentials [default] aws_access_key_id = AKIA3************** aws_secret_access_key = 1oxRajyBX********************** Inventory 확인 기본 aws_ec2 그룹으로 생성 # json 형태로 ec2 node 정보 및 group 정보 리스트 $ ansible-inventory -i aws_ec2.yaml --list ... "all": { "children": [ "aws_ec2", "ungrouped" ] }, "aws_ec2": { "hosts": [ "ip-10-211-20-159.ap-northeast-2.compute.internal", "ip-10-211-20-229.ap-northeast-2.compute.internal", "ip-10-211-20-45.ap-northeast-2.compute.internal" ] } } # inventory 정보를 그래프 형태로 표시 $ ansible-inventory -i aws_ec2.yaml --graph @all: |--@aws_ec2: | |--ip-10-211-20-159.ap-northeast-2.compute.internal | |--ip-10-211-20-229.ap-northeast-2.compute.internal | |--ip-10-211-20-45.ap-northeast-2.compute.inte # ansible 명령으로 확인 $ ansible -i aws_ec2.yaml --list-hosts all hosts (3): ip-10-211-20-229.ap-northeast-2.compute.internal ip-10-211-20-159.ap-northeast-2.compute.internal ip-10-211-20-45.ap-northeast-2.compute.internal 필터 및 그룹화 예제 : https://docs.ansible.com/ansible/2.9/plugins/inventory/aws_ec2.html#examples{:target="_blank"} filters : 필터로 원하는 항목만 가져옴 AWS CLI 명령 describe-instances –filters 항목 참고 https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html#options{:target="_blank"} keyed_groups : Inventory 그룹화 항목 plugin: aws_ec2 regions: - ap-northeast-2 filters: # tag 정보가 AutoDelete:False 인 인스턴스 tag:AutoDelete: "False" # running 상태 인스턴스 instance-state-name: 'running' keyed_groups: # tag 를 기준으로 그룹화, "prefix_key_value" 형식의 그룹이름 - prefix: tag key: tags # tag 로 그룹화 $ ansible-inventory -i aws_ec2.yaml --graph @all: |--@aws_ec2: | |--ip-10-211-20-159.ap-northeast-2.compute.internal | |--ip-10-211-20-229.ap-northeast-2.compute.internal | |--ip-10-211-20-45.ap-northeast-2.compute.internal |--@tag_AutoDelete_False: | |--ip-10-211-20-159.ap-northeast-2.compute.internal | |--ip-10-211-20-229.ap-northeast-2.compute.internal | |--ip-10-211-20-45.ap-northeast-2.compute.internal ... |--@tag_Name_kube01: | |--ip-10-211-20-45.ap-northeast-2.compute.internal |--@tag_Name_kube02: | |--ip-10-211-20-159.ap-northeast-2.compute.internal |--@tag_Name_kube03: | |--ip-10-211-20-229.ap-northeast-2.compute.internal |--@ungrouped: # ansible 명령으로 확인 $ ansible -i aws_ec2.yaml tag_AutoDelete_False --list-hosts hosts (3): ip-10-211-20-229.ap-northeast-2.compute.internal ip-10-211-20-159.ap-northeast-2.compute.internal ip-10-211-20-45.ap-northeast-2.compute.internal $ ansible -i aws_ec2.yaml tag_Name_kube01 --list-hosts hosts (1): ip-10-211-20-45.ap-northeast-2.compute.internal Host 연결을 위한 inventory parameters 추가 multiple inventory로 정보 추가 https://docs.ansible.com/ansible/2.9/user_guide/intro_inventory.html#connecting-to-hosts-behavioral-inventory-parameters{:target="_blank"} # aws.host [win:children] tag_AutoDelete_False [win:vars] ansible_user=rundeck ansible_password=<passowrd> ansible_connection=winrm ansible_port=5985 $ ansible-inventory -i aws_ec2.yaml -i aws.host --vars --list { "_meta": { "hostvars": { "ip-10-211-20-159.ap-northeast-2.compute.internal": { "ami_launch_index": 1, "ansible_connection": "winrm", "ansible_password": "<passowrd>", "ansible_port": 5985, "ansible_user": "ansible", "architecture": "x86_64", ... ansible-inventory --list 옵션으로 hosts 파일 형식 구성 $ ansible-inventory -i aws_ec2.yaml --list | jq -r '._meta.hostvars[] | "\(.private_ip_address) \(.tags.Name) "' 10.211.20.159 kube02 10.211.20.229 kube03 10.211.20.45 kube01 Ansible EC2 Dynamic inventory minimum IAM policies https://stackoverflow.com/questions/30519470/ansible-ec2-dynamic-inventory-minimum-iam-policies{:target="_blank"} { "Version": "2012-10-17", "Statement": [ { "Sid": "Demo201505282045", "Effect": "Allow", "Action": [ "ec2:Describe*", "route53:ListHostedZones", "route53:ListResourceRecordSets" ], "Resource": "*" } ] }

July 16, 2022 · Byung Kyu KIM

Kubernetes install with kubespray

Kubespray 를 이용한 Kubernetes 설치 (Baremetal) 사전준비 서버 노드 준비 3대 Node 192.168.28.15 192.168.28.16 192.168.28.17 서버 노드 환경설정 : kubernetes-101 참고 Swap 영역을 비활성화 SELinux Disable 방화벽 Disable 브릿지 네트워크 할성화 설치 노드에서의 ssh 접속 허용 : SSH 키복사 ssh-copy-id 설치 준비 Git : Repository Clone Python3 : Inventory 및 환경 설정을 위한 스크립트 실행 Ansible : 원격 실행(설치) ansible-playbook Repository clone 및 Python package install $ git clone https://github.com/kubernetes-sigs/kubespray $ cd kubespray # Package install $ pip3 install -r requirements.txt 설치 방법 1 : inventory_builder inventory_builder Python script 활용 inventory 구성 및 설치 ...

September 13, 2021 · Byung Kyu KIM

Ansible-101

Ansible-101 Ansible Install Centos 7 기본적으로 Python 2.7 기반으로 설치 됨 추후 pywinrm 패키지가 필요할때, python2-pip 패키지 추가 설치 필요 sudo yum install ansible 대안으로 pip 으로 설치 하는 방법 (user) pip install ansible # --user 용어 정의 Inventory : 관리하는 원격 서버 목록 지정하지 않으면 Default Path 의 hosts 파일을 참조 /etc/ansible/hosts Module : Task 를 실행하는 방법 (모듈) https://docs.ansible.com/ansible/latest/modules/modules_by_category.html{:target="_blank"} e.g. - command, shell, copy, service Playbook : Task 실행 프로세스 정의서 Yaml 파일로 작성 멱등성(idempotence) : 연산을 여러 번 적용하더라도 결과가 달라지지 않는 성질을 의미한다. Ansbile Config Path : /etc/ansible/ansible.cfg Ignore ansible ssh authenticity : ssh 최초 접속시 인증을 host key 체크 생략 https://stackoverflow.com/questions/32297456/how-to-ignore-ansible-ssh-authenticity-checking{:target="_blank"} [defaults] host_key_checking = False Callback plugins minimal stdout [defaults] stdout_callback = minimal 참고 : How to disable strict host key checking in ssh? https://askubuntu.com/questions/87449/how-to-disable-strict-host-key-checking-in-ssh{:target="_blank"} Inventory 구성 Public key 등록 사용 방법 : 키를 생성하고 public 키를 관리되는 서버에 등록 후 사용 # Key 생성 ssh-keygen # Public key 등록 ssh-copy-id [server-ip] Inventory 서버 등록 : /etc/ansible/hosts 기본디렉토리의 Host 파일을 사용하지 않는다면 -i 옵션으로 Inventory 파일 지정 https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#list-of-behavioral-inventory-parameters{:target="_blank"} # ansible host 파일 예제 192.168.1.5 [db] 192.168.1.10 [webservers] 192.168.1.100 192.168.1.110 # hosts 파일에 등록된 경우 [apiserver] apiserver1 apiserver2 # host명으로 등록 server1 ansible_host=192.168.1.50 server2 ansible_host=192.168.1.51 Host 지정 방법 all : 해당 Inventory의 모든 서버 webservers : webservers 라고 정의된 서버 목록 192.168.1.100 : 192.168.1.100 서버 Ad-hook Execute ansible 명령을 통한 inline 실행 주요 인수 Usage: ansible <host-pattern> [options] -a MODULE_ARGS, --args=MODULE_ARGS module arguments -e EXTRA_VARS, --extra-vars=EXTRA_VARS set additional variables as key=value or YAML/JSON, if filename prepend with @ -f FORKS, --forks=FORKS specify number of parallel processes to use (default=5) -h, --help show this help message and exit -i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY specify inventory host path or comma separated host list. --inventory-file is deprecated -m MODULE_NAME, --module-name=MODULE_NAME module name to execute (default=command) -v, --verbose verbose mode (-vvv for more, -vvvv to enable connection debugging) --version show program's version number, config file location, configured module search path, module location, executable location and exit -b, --become run operations with become (does not imply password prompting) - Example hostname 확인 # webservers 목록 실행 # command 모듈, "hostname" 인수 ansible webservers -m command -a "hostname" # default module (생략가능) : -m command ansible webservers -a "hostname" # 모든 서버 ansible all -a "hostname" 기타 모듈 사용 # ping 모듈 ansible webservers -m ping # copy 모듈 : local → host ansible webservers -m copy -a "src=file.txt dest=/home/cdecl/web/" # service 모듈 : kubelet 서비스를 시작 ansible webservers -m service -a "name=kubelet state=started" Playbook 사용 일련의 Task(작업)를 기술하여, 프로세스를 실행하는 방법 ...

August 11, 2021 · Byung Kyu KIM