2023. 6. 17. 06:05ㆍAnsible_ad-hoc
설치 및 설정
# yum install -y epel-release - epel 리포지토리 추가
# yum install -y ansible - ansible 설치
# ansible --version - 버전 확인
# ssh-keygen Controller
# ssh-copy-id 'root'@10.0.0.2 - 원격서버계정@원격서버IP
# ssh 'root'@'10.0.0.2 - 원격서버계정@원격서버IP
인벤토리 작성
# vi /etc/ansible/hosts
각 호스트의 ip 등록
[db]
db-1.example.com
db-2.example.com
db-3.example.com
[app]
app-1.example.com
app-2.example.com
Ad-hoc
ansible all -m ping
# ansible -i /etc/ansible/hosts all(or test) -m ping -a --host-list
# ansible hosts options
# all 모든 호스트
# -m 실행 모듈 호출 -a 뒤에 인자값
# -i 인벤토리파일 지정, 지정하지않으면 /etc/ansible/hosts
# 일반 명령어
# ansible [pattern] -m [module] -a "[module options]"
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"
# 사용자 계정 생성
ansible 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"
# 패키지 설치 및 파일 복사
ansible all -m yum -a "name=httpd state=present"
curl nginx.org -o index.html
ansible all -m copy -a "src=index.html dest=/var/www/html/index.html" # index.html /var/www/html/html폴더 안에 복사
ansible all -m service -a "name=httpd state=started" # systemctl start httpd httpd 시작
ansible all -m shell -a "systemctl stop firewalld" # firewalld stop
ansible all -m file -a "path=/var/www/html/index.html state=absent" # index.html 삭제
ansible all -m yum -a 'name=httpd state=absent" # httpd 삭제
vi /etc/ansible/hosts - 기본 inventory 경로 지정
# ansible 실행 시 -i 옵션으로 inventory 파일 지정 가능
1. 단일 호스트 지정
node1
node2 node2.test.com
2. 그룹 지정
[web] - 그룹명
node1
node2
[db]
node3
3. ssh 포트 지정
node1.test.com:60000
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
node3.test.com ansible_connection=ssh anbible_user=test2
6. /etc/hosts 파일과 다르게 지정
node1 ansible_port=222 ansible_host=192.168.100.100
7. 호스트 변수 지정
node1 http_port=8080 maxRequestsPerChild=800
8. 그룹 변수
[busan]
host1
host2
[busan:vars]
ntp_server=time.bora.net
proxy=proxy.test.com
9. 그룹의 그룹
[s]
host1
host2
[t]
node1
node2
[ko:children]
s
t
'Ansible_ad-hoc' 카테고리의 다른 글
nginx + wordpress (0) | 2023.06.18 |
---|---|
DHCP, VSFTPD, Nginx. DNS, Infra 자동화 (0) | 2023.06.17 |
ssh key 자동배포 (0) | 2023.06.17 |
CentOS7 - Wordpress + MySQL5.7 + PHP7.3 Install (0) | 2023.06.17 |
RDS 배포 및 구성-yml 파일 (0) | 2023.06.17 |