-
Ansible: Ad-hoc & Inventory공부합시다!/Ansible 2022. 7. 5. 10:03728x90
환경 설정
Hostname Roles IP Address Guest OS CPU RAM HDD Cont Controller 10.0.0.1/24 CentOS7.9 1P1C 2,048 20G node1 Managed 10.0.0.11/24 node2 Managed 10.0.0.12/24 node3 Managed 10.0.0.13/24 1. 설치 및 설정
1.1. 설치
1.1.1. # yum install -y epel-release epel 리포지토리 추가
1.1.2. # yum install -y ansible 설치
1.1.3. # ansible --version 확인
1.2. 명령 전달은 위한 ssh 설정
1.2.1. # ssh-keygen Controller
1.2.2. # ssh-copy-id 원격서버계정@원격서버IP
1.2.3. # ssh 원격서버계정@원격서버IP
1.2.4. 위의 과정을 playbook으로 작성해서 배포하세요.
ssh.yml참조
1.3. 인벤토리 파일 작성
1.3.1. # vi /etc/ansible/hosts
# 각 호스트의 ip 등록모든 Ad-hoc 명령어는 playbook으로 만들어 보셔야 합니다. 꼭
1.3.2. # ansible all -m ping
# ansible -i /etc/ansible/hosts all(or test) -m ping --host-list
1.3.3. 명령구조
# ansible hosts options
# all 모든 호스트
# -m 실행 모듈 호출 -a 뒤에 인자값
# -k 암호 묻기
# -i 인벤토리파일 지정, 지정하지않으면 /etc/ansible/hosts
1.3.4. # 사용자 생성 및 httpd 배포 및 파일 전송 명령
1.3.4.1. 일반 명령어
# ansible all -m shell -a "ls -al"
# ansible all -m shell -a "uptime"
# ansible all -m shell -a "df -h"
# ansible all -m shell -a "free -h"
1.3.4.2. 사용자 계정 생성
# ansbile all -m user -a "name=a"
# ansible all -m user -a "name=a update_password=always password={{ '변경하고싶은 비밀번호' | password_hash('sha512') }}" -u ec2-user
# ansible all -m shell -a "echo 'It1' | passwd --stdin a"
# ansible all -m shell -a "tail -n 2 /etc/passwd"
# ansible all -m user -a "name=a state=absent"
# ansible all -m shell -a "tail -n 2 /etc/passwd"
1.3.4.3. 패키지 설치 및 파일 복사
# ansible all -m yum -a "name=httpd state=present"
# curl ngnix.org -o index.html
# ansible all -m copy -a "src=index.html dest=/var/www/html/index.html"
# ansbile all -m service -a "name=httpd state=started"
# ansible all -m shell -a "systemctl stop firewalld"
# ansible all -m file -a "path=/var/www/html/index.html state=absent"
# ansible all -m yum -a "name=httpd state=absent"
1.3.4.4. 위의 모든 과정을 하나의 playbook으로 작성할 수 있을까요?
1.4. 멱등성 : 연산을 여러번 적용하더라도 달라지지 않는 성질
1.4.1. 멱등성이 없는 경우
# echo "172.16.0.131" >> inven.list
# echo "172.16.0.132" >> inven.list
# echo "172.16.0.133" >> inven.list
# echo "172.16.0.133" >> inven.list
# cat inven.list
1.4.2. 멱등성이 있는 경우
# ansible localhost -c local -m lineinfile -a "path=inven.list lint=172.16.0.14"
# cat inven.list
# ansible localhost -c local -m lineinfile -a "path=inven.list lint=172.16.0.14"
# cat inven.list2. Inventory
2.1. 작업 대상 지정
2.2. 기본 경로 /etc/ansible/hosts
2.3. ansible 실행 시 -i 옵션으로 inventory 파일 지정 가능
2.4. Host Patten
행동 예제 행동 에제 모든 Host all 모든 Host * 합집합 dev:staging 교집합 staging:&Database 배제 dev:!que 와일드카드 *.test.com 범위 web[5:10].test.com 정규식 ~web\d+\.test.com\.(com|org) # vi /etc/ansible/hosts 1. 단일 호스트 지정 node1 node2 node2.test.com 2. 그룹 지정 [web] -> 그룹 node1 node2 [db] node3 3. ssh 포트 변경 node1.test.com:60021 4. 다수 서버 지정 [all] node[1:3] [db] db-[a:f].test.com 5. 호스트별 연결 및 사용자 지정 [target] node1.test.com ansible_connection=local node2.test.com ansible_connection=ssh anbible_user=test node2.test.com ansible_connection=ssh anbible_user=babo 6. /etc/hosts 파일과 다르게 지정 node1 ansible_port=2222 ansible_host=192.168.100.102 7. 호스트 변수 지정 node1 http_port=8080 maxRequestsPerChild=800 8. 그룹 변수 [busan] host1 host2 [busan:vars] ntp_server=time.bora.net proxy=porxy.test.com 9. 그룹의 그룹 [seoul] host1 host2 [test] node1 node2 [korea:children] seoul test
위에서 사용한 모든 Ad-hoc 명령어의 yaml 파일을 작성하세요!
꼭 해보셔야 합니다.
728x90'공부합시다! > Ansible' 카테고리의 다른 글
Ansible: web server + php7.3 배포 및 구성 (0) 2022.07.08 Ansible: ssh key 자동 배포 (2023년 1월 11일 Test) (0) 2022.07.07 Ansible: Playbook yaml (0) 2022.07.06 Ansible: Ansiable 개요 (0) 2022.07.04 Ansible: IaC개요 (0) 2022.07.03