memo

ansible 자동화 기능 관련

slow333 2023. 1. 14. 11:41


1. control server에 ansible 설치 필요
  - centos : epel-release 설치 필요
  - ubuntu : 불필요

2. 인증 방법
  - 사전에 ssh 인증을 통해 자동 연결 가능 : root 계정만
    ssh-keygen   => copy-ssh-id root@IP (.ssh 폴더에서) 

3. /etc/ansible/hosts   파일 수정
편집
[centos]
name_define ansible-host=192.168.33.34

또는 IP만
[ubuntu]
192.168.34.56

3. cli 기반으로 명령을 직접 수행 또는 
  - ssh 사전 인증 필요
    $ ansible all -m shell -a "ls -al" -u root

  - 암호를 통해 접속 가능 : sudo로 실행해야 함.( 옵션 -k)
    $ sudo ansible all -m shell -a "ls -al" -k
    또는
    # ansible all -m shell -a "ls -al" -k

3. playbook을 통해 수행
  - vi test.yml => 생성
  - ansible-playbook file_name.yml -k 
  - ansible-playbook file_name.yml -u root

  - 기본 문법 : 
    - name : 이름을 정의 한글 안됨
      module_name: name="모듈에 따른 기본 내용" state= present(설치), absent(제거), latest(최신버젼)

예제) 
---
- name : Install nginx on centos7.9
  hosts: centos
  gather_facts: no
  become: yes

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

- name: install nginx on ubuntu
  hosts: ubuntu
  gather_facts: no
  become: yes

  tasks:
    - name: install nginx web server
      apt: pkg=nginx state=present update_cache=yes
    - name: upload default index.html for web server
      get_url: url=https://www.nginx.com dest=/usr/share/nginx/html/ mode=0644 validate_certs=no

- name: Setup for the Ansible's Environment
  hosts: centos:ubuntu:rhel
  gather_facts: no

  tasks:
    - name: Create vim env's directories & files
      shell: "{{ item }}"
      with_items:
        - "touch ~/.vimrc"

    - name: Install vim-enhanced
      yum:
        name: vim-enhanced
        state: present

    - name: Install git
      yum:
        name: git
        state: present

    - name: Configure vimrc
      lineinfile:
        path: ~/.vimrc
        line: "{{ item }}"
      with_items:
        - "set number"
        - "set hlsearch"
        - "set autoindent"
        - "set ts=4"
        - "set sts=4"
        - "set cindent"
        - "set laststatus=2"
        - "set shiftwidth=4"
        - "set showmatch"
        - "set smartcase"
        - "set smarttab"
        - "set smartindent"
        - "set ruler"
        - "set fileencodings=utf8,euc-kr"
        - "syntax on"

    - name: Configure Bashrc
      lineinfile:
        path: ~/.bashrc
        line: "{{ item }}"
      with_items:
        - "alias ll='ls -alF'"
        - "alias la='ls -A'"
        - "alias l='ls -CFlh'"
        - "alias lo='ls -o'"
        - "alias lh='ls -lh'"
        - "alias rm='rm -i'"
        - "alias cp='cp -i'"
        - "alias mv='mv -i'"
        - "alias fw='firewall-cmd'"
        - "alias scst='sudo systemctl start'"
        - "alias scsp='sudo systemctl stop'"
        - "alias scre='sudo systemctl restart'"
        - "alias scsu='sudo systemctl status'"
        - "alias scen='sudo systemctl enable'"
        - "alias cc='clear'"
        - "alias apti='sudo apt-get install -y'"
        - "alias yumi='yum install -y'"
        - "alias vi='vim'"
        - 'PS1="\e[1;31m\]\u : \[\e[1;36m\]\w\[\e[m\] # "'

- name : firewall open http and https on centos7.9
  hosts: servers
  gather_facts: no

  tasks:
    - name: http service open
      shell: "firewall-cmd --permanent --add-service=http"
    - name: https service open
      shell: "firewall-cmd --permanent --add-service=https"
    - name: 80 port open
      shell: "firewall-cmd --permanent --add-port=80/tcp"
    - name: firewalld reload
      shell: "firewall-cmd --reload"

- name : setup centos timezone
  hosts: centos:ubuntu
  gather_facts: no


  tasks:
    - name: to Asia/Seoul
      timezone: name=Asia/Seoul

'memo' 카테고리의 다른 글

메타데이터  (0) 2023.01.15
CPU 검사를 우회 하도록 윈도 레지스트리 편집  (0) 2023.01.15
docker 기본 명령  (0) 2023.01.14
서버 용량산정  (0) 2023.01.14
파일 강제 삭제  (0) 2023.01.14