본문 바로가기

카테고리 없음

ansible playbook

playbook 규칙
--- : 시작
name : 작업구분이름 (생략가능)
hosts : 적용대상
tasks : 적용작업
# : 주석(comment)
... : 종료(생략가능)

playbook 실행
play : 시작
TASK [Gathering Facts] : 처음 시작 되는 부분 , 문법 검사
TASK [작업 이름] : 실제로 작업
PLAY RECAP : 작업실행의 결과
- ok, changed, unreachable, failed, skippied, rescued, ignored
[root@ansible-server ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:4n4oxrbJEKsZaojg7qJ8TL8+UyjKFnosnAN6mNzvPBE root@ansible-server
The key's randomart image is:
+---[RSA 3072]----+
|                 |
|                 |
|                 |
|     E           |
|  .   + S        |
|o. = + o         |
|&+X.o +.         |
|%#+**B. .        |
|%BooBO*.         |
+----[SHA256]-----+
[root@ansible-server ~]# ssh-copy-id root@200.200.200.147
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@200.200.200.147's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@200.200.200.147'"
and check to make sure that only the key(s) you wanted were added.

[root@ansible-server ~]# ssh-copy-id root@200.200.200.148
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@200.200.200.148's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@200.200.200.148'"
and check to make sure that only the key(s) you wanted were added.

문법 검사

---
- name : Install nginx on RockyLinux9
  hosts : nginx
  gather_facts : no

  tasks :
  - name : install epel-release
    dnf : name=epel-release state=latest
  - name : install nginx web server
    dnf : name=nginx state=latest
  - name : upload default index.html for web server
    get_url : url=https://www.nginx.com dest=/usr/share/html mode=0644
  - name : start nginx web server
    systemd : name=nginx state=started

 

[root@ansible-server ~]# ls
aaa.yml  anaconda-ks.cfg  index.html  공개  다운로드  문서  바탕화면  비디오  사진  서식  음악
[root@ansible-server ~]# file aaa.yml
aaa.yml: ASCII text
[root@ansible-server ~]# mv aaa.yml nginx.yml
[root@ansible-server ~]# ls
anaconda-ks.cfg  index.html  nginx.yml  공개  다운로드  문서  바탕화면  비디오  사진  서식  음악
[root@ansible-server ~]# ansible-playbook nginx.yml 
[WARNING]: Could not match supplied host pattern, ignoring: nginx

PLAY [Install nginx on RockyLinux9] *******************************************************************************
skipping: no hosts matched

PLAY RECAP ********************************************************************************************************

[root@ansible-server ~]# vi /etc/ansible/hosts 
[nginx]
200.200.200.147
200.200.200.148

[root@ansible-server ~]# ansible-playbook nginx.yml 
[WARNING]: Could not match supplied host pattern, ignoring: nginx

PLAY [Install nginx on RockyLinux9] *******************************************************************************
skipping: no hosts matched

PLAY RECAP ********************************************************************************************************

[root@ansible-server ~]# vi /etc/ansible/hosts 
[root@ansible-server ~]# ansible-playbook nginx.yml 

PLAY [Install nginx on RockyLinux9] *******************************************************************************

TASK [install epel-release] ***************************************************************************************
changed: [200.200.200.147]
changed: [200.200.200.148]

TASK [install nginx web server] ***********************************************************************************
changed: [200.200.200.147]
changed: [200.200.200.148]

TASK [upload default index.html for web server] *******************************************************************
changed: [200.200.200.147]
changed: [200.200.200.148]

TASK [start nginx web server] *************************************************************************************
changed: [200.200.200.147]
changed: [200.200.200.148]

PLAY RECAP ********************************************************************************************************
200.200.200.147            : ok=4    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
200.200.200.148            : ok=4    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

---
- name : Install nginx on RockyLinux9
  hosts : nginx
  gather_facts : no

  tasks :
  - name : install epel-release
    dnf : name=epel-release state=latest
  - name : install nginx web server
    dnf : name=nginx state=latest
  - name : upload default index.html for web server
    get_url : url=https://www.nginx.com dest=/usr/share/nginx/html/index.html mode=0644
  - name : start nginx web server
    systemd : name=nginx state=started
[root@ansible-server ~]# ansible-playbook nginx.yml 

PLAY [Install nginx on RockyLinux9] *******************************************************************************

TASK [install epel-release] ***************************************************************************************
ok: [200.200.200.148]
ok: [200.200.200.147]

TASK [install nginx web server] ***********************************************************************************
ok: [200.200.200.147]
ok: [200.200.200.148]

TASK [upload default index.html for web server] *******************************************************************
changed: [200.200.200.148]
changed: [200.200.200.147]

TASK [start nginx web server] *************************************************************************************
ok: [200.200.200.148]
ok: [200.200.200.147]

PLAY RECAP ********************************************************************************************************
200.200.200.147            : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
200.200.200.148            : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0