본문 바로가기

CICD

Ansible Playbook 작성법

Ansible Playbook 작성 예시

해당 Playbook 의 내용은 문법 및 key 값 참고용으로 작성 된 내용입니다.

- name: First setting # playbook의 이름
  hosts: all # 실행할 그룹명
  become: yes # 대상 호스트에 root 사용자로 작업을 수행여부
  connection: local # 대상 호스트에 연결 방법 (ssh / local)
  gather-facts: no # facts를 수집여부, 미 수집시 성능 향상

  tasks:
  - name: Create /data001/docker_storage dir  # /data001/docker_storage Dir Create
    file:
      path: /data001/docker_storage
      state: directory
      force: no

  - name: Update and upgrade apt packages
    apt:
      upgrade: yes
      update_cache: yes
      cache_valid_time: 3600 # If latest update within 1 Day, skip

  - name: Install packages # Install need package
    vars:
      ansible_python_interpreter: /usr/bin/python3
    apt:
      name:
        - net-tools
        - unzip
        - tree
      state: latest
  
  - name: Install docker-compose # docker를 설치합니다.
    shell:
      cmd: curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" -o /etc/apt/keyrings/docker.asc
      warn: false
  
  - name: Chmod docker #docker의 모드를 변경
    file:
      path: "/usr/local/bin/docker"
      mode: 0755

  - name: start docker
    service: # service 모듈 사용
      name: docker
      state: started # 서비스 시작
  
  - name: url download
    get_url:
      url: hppts://****.com/file
      dest: /path/download/
      
   - name: Script File Move
    copy:
      src: /data001/ansible/{{item}}
      dest: /data001
      mode: 0777 # Permission
    with_items:
    - awscli_install.sh
    - jenkins_install.sh

  - name: Script Start
    command: sh /data001/awscli_install.sh

  - name: Script Delete # Script File Delete
    file:
      path: /data001/aws/{{item}}
      state: absent
    with_items:
    - awscli_install.sh
    - jenkins_install.sh
    
  - name:  Install AWSCLI V2 zip
    get_url:
      url: "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip"
      dest: /data001