Linux

[Devops] Ansible 실습

cumo 2025. 3. 13. 12:41

1. Ansible을 사용한 서버 구축 자동화

ansible을 사용하여 클라우드 서버 A로 부터 B ,C의 인스턴스에 서버를 자동으로 구축 할 수 있도록

코드 작성

---
- name:
  hosts: all
  tasks:
    - name: Install httpd php*
      dnf:
        name: 
          - httpd
          - php
        state: latest
          
    - name: start httpd
      service:
        name: httpd
        state: started
    - name: enable php
      service:
        name: httpd
        enabled: yes
  
    - name: open firewall
      firewalld:
        service: "{{ item }}"
        permanent: yes
        state: enabled
        immediate: yes
      with_items:
        - http
        - ssh
        - telnet 
        - ftp
    - name: wget wordpress
      shell: wget https://ko.wordpress.org/latest-ko_KR.zip

    - name: unzip wordpress
      shell: unzip latest-ko_KR.zip

    - name: move wordpress
      shell: mv wordpress /var/www/
      
    - name: change owner
      shell: chown -R apache:apache /var/www/html/
      
    - name: change permission
      shell: chmod -R 755 /var/www/html/
      
    - name: link wordpress
      shell: ln -s /var/www/html/wordpress 
      
    - name: restart httpd
      service:
        name: httpd
        state: restarted