- 한번의 명령어로 다수의 시스템에 작업
1. uptime 확인
- all : ansible 클라이언트의 모든 노드를 대상
- -m 옵션 : 사용할 모듈을 지정하기 위한 옵션
- shell : -m 옵션의 값으로 shell을 지정하면 shell 명령어를 사용 가능
- -a 옵션 : argument의 약어 shell에서 사용할 명령어를 지정하는 옵션
- "uptime" : -a 옵션의 값으로 모든 ansible 클라이언트에게 uptime 명령어를 실행하게 함
- -k 옵션 : SSH 접속을 위한 암호 입력을 가능하게 함
$ ansible all -m shell -a "uptime" -k SSH password: 8.8.8.8 | CHANGED | rc=0 >> 00:41:11 up 58 days, 36 min, 1 user, load average: 0.00, 0.01, 0.05
2. 디스크 용량 확인
- all : ansible 클라이언트의 모든 노드를 대상
- -m 옵션 : 사용할 모듈을 지정하기 위한 옵션
- shell : -m 옵션의 값으로 shell을 지정하면 shell 명령어를 사용 가능
- -a 옵션 : argument의 약어 shell에서 사용할 명령어를 지정하는 옵션
- "df -h" : -a 옵션의 값으로 모든 ansible 클라이언트에게 df -h명령어를 실행하게 함 → df 명령어에 -h 옵션을 붙이려면 ""(큰 따옴표)를 사용해야 함
# 이미 ssh 접속에 성공했기 때문에 -k 옵션 사용 X $ ansible all -m shell -a "df -h" 8.8.8.8 | CHANGED | rc=0 >> Filesystem Size Used Avail Use% Mounted on devtmpfs 63G 0 63G 0% /dev tmpfs 63G 4.0K 63G 1% /dev/shm tmpfs 63G 163M 63G 1% /run tmpfs 63G 0 63G 0% /sys/fs/cgroup /dev/sda2 243G 9.4G 221G 5% / /dev/sda1 477M 149M 299M 34% /boot /dev/sdb1 1.5T 77M 1.4T 1% /cache1 tmpfs 13G 0 13G 0% /run/user/0
3. 메모리 상태 확인
- all : ansible 클라이언트의 모든 노드를 대상
- -m 옵션 : 사용할 모듈을 지정하기 위한 옵션
- shell : -m 옵션의 값으로 shell을 지정하면 shell 명령어를 사용 가능
- -a 옵션 : argument의 약어 shell에서 사용할 명령어를 지정하는 옵션
- "free -h" : -a 옵션의 값으로 모든 ansible 클라이언트에게 free -h명령어를 실행하게 함 → free 명령어에 -h 옵션을 붙이려면 ""(큰 따옴표)를 사용해야 함
$ ansible all -m shell -a "free -h" 8.8.8.8 | CHANGED | rc=0 >> total used free shared buff/cache available Mem: 125G 5.2G 110G 162M 9.6G 119G Swap: 31G 0B 31G
4. 새로운 유저 생성
- all : ansible 클라이언트의 모든 노드를 대상
- -m 옵션 : 사용할 모듈을 지정하기 위한 옵션
- user : -m 옵션의 값으로 user을 지정하면 linux의 user 생성
- -a 옵션 : argument의 약어로 id와 password를 지정
- "name=hippo password=1234" : 생성되는 user의 이름을 hippo로 하고, 비밀번호를 1234로 함 → ""(큰 따옴표)를 사용해야 id와 password를 같이 지정 가능
$ ansible all -m user -a "name=hippo password=1234" -k SSH password: [WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for this module to work properly. 8.8.8.8 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1000, "home": "/home/hippo", "name": "hippo", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1000 }
※ 경고 발생 → [WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for this module to work properly.
- password=1234로 하면 비밀번호가 암호화 되지 않아서 문제 발생
- 1234의 암호화된 password가 저장되어야 ssh 접속할 때 1234로 로그인 가능
5. 파일 전송
- 특정 파일을 모든 서버에 전달하는 방법
- all : ansible 클라이언트의 모든 노드를 대상
- -m 옵션 : 사용할 모듈을 지정하기 위한 옵션
- copy 모듈 : ansible 클라이언트에게 파일을 전달하기 위해 사용하는 모듈
- -a 옵션 : argument의 약어 copy에서 사용할 파일과 경로를 지정하는 옵션
- src : 출발지를 의미 → /root/ 디렉토리에 있는 "sudo-1.9.5-3.el6.x86_64.rpm" 파일 전달
- dest : 도착지를 의미 → ansible 클라이언트의 /root/ 디렉토리에 파일 저장
ansible all -m copy -a "src=./sudo-1.9.5-3.el6.x86_64.rpm dest=/root/" -k SSH password: 8.8.8.8 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "4fda41de00dddeb32445e1ea4d4dae1ba2c24d9a", "dest": "/root/sudo-1.9.5-3.el6.x86_64.rpm", "gid": 0, "group": "root", "md5sum": "801fda76d88f8c619a2a931f2e5e29d1", "mode": "0644", "owner": "root", "size": 2235944, "src": "/root/.ansible/tmp/ansible-tmp-1613809840.71-45665-17231553755106/source", "state": "file", "uid": 0 }
6. 서비스 설치
- lshw 설치
- all : ansible 클라이언트의 모든 노드를 대상
- -m 옵션 : 사용할 모듈을 지정하기 위한 옵션
- yum 모듈 : ansible 클라이언트의 yum을 이용하여 yum 업데이트 및 패키지 설치
- -a 옵션 : argument의 약어로 설치하거나 업데이트할 패키지를 지정하기 위해 옵션 사용
- name : 패키지의 이름
- state=present → 패키지를 설치
- state : absent → 패키지를 삭제
$ ansible all -m yum -a "name=lshw state=present" -k 8.8.8.8 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "changes": { "installed": [ "lshw" ] }, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror\\nLoading mirror speeds from cached hostfile\\n * base: mirror.kakao.com\\n * epel: mirror.krmir.org\\n * extras: mirror.kakao.com\\n * updates: mirror.kakao.com\\nResolving Dependencies\\n--> Running transaction check\\n---> Package lshw.x86_64 0:B.02.18-17.el7 will be installed\\n--> Finished Dependency Resolution\\n\\nDependencies Resolved\\n\\n================================================================================\\n Package Arch Version Repository Size\\n================================================================================\\nInstalling:\\n lshw x86_64 B.02.18-17.el7 base 324 k\\n\\nTransaction Summary\\n================================================================================\\nInstall 1 Package\\n\\nTotal download size: 324 k\\nInstalled size: 941 k\\nDownloading packages:\\nRunning transaction check\\nRunning transaction test\\nTransaction test succeeded\\nRunning transaction\\n Installing : lshw-B.02.18-17.el7.x86_64 1/1 \\n Verifying : lshw-B.02.18-17.el7.x86_64 1/1 \\n\\nInstalled:\\n lshw.x86_64 0:B.02.18-17.el7 \\n\\nComplete!\\n" ] }
'Ansible(앤서블)' 카테고리의 다른 글
ansible 서버에서 비밀번호 없이 로그인 → authorized_keys 파일 저장 (0) | 2022.07.24 |
---|---|
ansible의 플레이북(playbook) (0) | 2022.07.24 |
ansible 명령어 옵션 정리 (계속 업데이트 예정) (0) | 2022.07.24 |
ansible 설치 및 구성 (0) | 2022.07.24 |
ansible 사용할 때 적용하는 ansible.cfg 내용 (정리 중) (0) | 2022.06.24 |