리셋 되지 말자

[Ansible] ubuntu에서 ansible playbook을 이용한 nginx 설치 및 삭제 본문

Infra

[Ansible] ubuntu에서 ansible playbook을 이용한 nginx 설치 및 삭제

kyeongjun-dev 2021. 6. 11. 16:22

Vagrantfile

Vagrant.configure("2") do |config|
    #ansible-client001
    config.vm.define "ansible-client001" do |cfg|
        cfg.vm.box = "ubuntu/bionic64"
        cfg.vm.provider "virtualbox" do |vb|
            vb.name = "ansible-client001"
            vb.cpus = 2
            vb.memory = 2048
        end
        cfg.vm.host_name = "ansible-client001"
        cfg.vm.network "private_network", ip: "192.168.56.101", :adapter => 2
    end

    #ansible-server
    config.vm.define "ansible-server" do |cfg|
        cfg.vm.box = "ubuntu/bionic64"
        cfg.vm.provider "virtualbox" do |vb|
            vb.name = "ansible-server"
            vb.cpus = 2
            vb.memory = 2048
        end
        cfg.vm.host_name = "ansible-server"
        cfg.vm.network "private_network", ip: "192.168.56.100", :adapter => 2
        cfg.vm.provision "shell", inline: "apt install -y software-properties-common && apt-add-repository ppa:ansible/ansible && apt update -y && apt install -y ansible"
    end
end
  

ssh 설정은 생략 ping 모듈 동작 확인

 

- ping 테스트

vagrant@ansible-server:~$ ansible clients -m ping
192.168.56.101 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}

 

 

playbook

- install_webserver.yml

vagrant@ansible-server:~$ cat install_webserver.yml 
---
- name: install nginx on the client
  hosts: clients
  become: yes

  tasks:
  - name: update apt repo and cache
    apt: update_cache=yes force_apt_get=yes
  - name: install nginx
    action: apt name=nginx state=present
  - name: start nginx
    service: name=nginx state=started

 

- remove_webserver.yml

vagrant@ansible-server:~$ cat remove_webserver.yml 
---
- name: remove nginx on the client
  hosts: clients
  become: yes

  tasks:
  - name: stop nginx
    service:
      name: nginx
      state: stopped
  - name: remove  nginx
    apt:
      name: nginx
      state: absent

 

playbook 실행

- install and start

vagrant@ansible-server:~$ ansible-playbook install_webserver.yml 

PLAY [install nginx on the client] **********************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************
ok: [192.168.56.101]

TASK [update apt repo and cache] ************************************************************************************************************
changed: [192.168.56.101]

TASK [install nginx] ************************************************************************************************************************
changed: [192.168.56.101]

TASK [start nginx] **************************************************************************************************************************
ok: [192.168.56.101]

PLAY RECAP **********************************************************************************************************************************
192.168.56.101             : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

- nginx 설치 확인

vagrant@ansible-client001:~$ dpkg -l | grep -i nginx
ii  libnginx-mod-http-geoip          1.14.0-0ubuntu1.9                   amd64        GeoIP HTTP module for Nginx
ii  libnginx-mod-http-image-filter   1.14.0-0ubuntu1.9                   amd64        HTTP image filter module for Nginx
ii  libnginx-mod-http-xslt-filter    1.14.0-0ubuntu1.9                   amd64        XSLT Transformation module for Nginx
ii  libnginx-mod-mail                1.14.0-0ubuntu1.9                   amd64        Mail module for Nginx
ii  libnginx-mod-stream              1.14.0-0ubuntu1.9                   amd64        Stream module for Nginx
ii  nginx                            1.14.0-0ubuntu1.9                   all          small, powerful, scalable web/proxy server
ii  nginx-common                     1.14.0-0ubuntu1.9                   all          small, powerful, scalable web/proxy server - common files
ii  nginx-core                       1.14.0-0ubuntu1.9                   amd64        nginx web/proxy server (standard version)

 

- nginx 동작 확인

 

- stop and remove

vagrant@ansible-server:~$ ansible-playbook remove_webserver.yml 

PLAY [remove nginx on the client] ***********************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************
ok: [192.168.56.101]

TASK [stop nginx] ***************************************************************************************************************************
changed: [192.168.56.101]

TASK [remove  nginx] ************************************************************************************************************************
changed: [192.168.56.101]

PLAY RECAP **********************************************************************************************************************************
192.168.56.101             : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

- nginx 미동작 확인

 

 

- nginx 삭제가 안됨 1

vagrant@ansible-client001:~$ dpkg -l | grep nginx
ii  libnginx-mod-http-geoip          1.14.0-0ubuntu1.9                   amd64        GeoIP HTTP module for Nginx
ii  libnginx-mod-http-image-filter   1.14.0-0ubuntu1.9                   amd64        HTTP image filter module for Nginx
ii  libnginx-mod-http-xslt-filter    1.14.0-0ubuntu1.9                   amd64        XSLT Transformation module for Nginx
ii  libnginx-mod-mail                1.14.0-0ubuntu1.9                   amd64        Mail module for Nginx
ii  libnginx-mod-stream              1.14.0-0ubuntu1.9                   amd64        Stream module for Nginx
ii  nginx-common                     1.14.0-0ubuntu1.9                   all          small, powerful, scalable web/proxy server - common files
ii  nginx-core                       1.14.0-0ubuntu1.9                   amd64        nginx web/proxy server (standard version)

 

- nginx 삭제가 안됨 2

vagrant@ansible-client001:~$ systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Fri 2021-06-11 07:20:26 UTC; 2min 0s ago
     Docs: man:nginx(8)
  Process: 19508 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=2)
  Process: 18762 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 18751 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 18763 (code=exited, status=0/SUCCESS)

 

- remove_webserver.yml 파일 수정 (autoremove 옵션 추가)

vagrant@ansible-server:~$ cat remove_webserver.yml 
---
- name: remove nginx on the client
  hosts: clients
  become: yes

  tasks:
  - name: stop nginx
    service:
      name: nginx
      state: stopped
  - name: remove  nginx
    apt: name=nginx state=absent autoremove=yes

참고 : https://sysnet4admin.blogspot.com/2017/06/ansible-playbook-nginx-3.html#.YMMELpozYaw

 

- nginx 삭제 결과

vagrant@ansible-client001:~$ dpkg -l | grep -i nginx
rc  libnginx-mod-http-geoip          1.14.0-0ubuntu1.9                   amd64        GeoIP HTTP module for Nginx
rc  libnginx-mod-http-image-filter   1.14.0-0ubuntu1.9                   amd64        HTTP image filter module for Nginx
rc  libnginx-mod-http-xslt-filter    1.14.0-0ubuntu1.9                   amd64        XSLT Transformation module for Nginx
rc  libnginx-mod-mail                1.14.0-0ubuntu1.9                   amd64        Mail module for Nginx
rc  libnginx-mod-stream              1.14.0-0ubuntu1.9                   amd64        Stream module for Nginx
rc  nginx-common                     1.14.0-0ubuntu1.9                   all          small, powerful, scalable web/proxy server - common files

nginx-common은 ubuntu에 직접 들어가서 지워도 삭제가 안된다고 한다

 

purge 옵션으로 삭제

sudo apt remove --purge nginx*

이렇게 삭제하면 다 삭제되기는 한다.

 

최종 remove_webserver.yml 수정

vagrant@ansible-server:~$ cat remove_webserver.yml 
---
- name: remove nginx on the client
  hosts: clients
  become: yes

  tasks:
  - name: stop nginx
    service:
      name: nginx
      state: stopped
  - name: remove  nginx
    apt: name=nginx state=absent autoremove=yes purge=yes

참고 : https://askubuntu.com/questions/457040/cannot-remove-nginx

 

- 삭제됐는지 확인

vagrant@ansible-client001:~$ dpkg -l | grep -i nginx
vagrant@ansible-client001:~$

삭제 되었다...

 

후기

편하긴 하지만, 어떤 도구던 잘 사용하기 위해서 공부가 필요하다...

Comments