7.7

w0nyyy·2022년 7월 7일
0

ansible

--- 앤서블 환경 설정 자동화
# vi ansible_env.yml
- name: Setup for the Ansible's Environment
  hosts: localhost
  gather_facts: no
  
  tasks:
    - name: Add "/etc/ansible/hosts"
      blockinfile: 
        path: /etc/ansible/hosts
        block: |

         [centos]
         192.168.0.236
         192.168.0.240

         [ubuntu]
         192.168.0.219 ansible_python_interpreter=/usr/bin/python3
         192.168.0.247 ansible_python_interpreter=/usr/bin/python3

    - name: Configure Bashrc
      lineinfile:   
        path: /root/.bashrc
        line: "{{ item }}"
      with_items:
        - "alias ans='ansible'"	# 앤서블 별칭 달아주기 ans라고 쓰면됨
        - "alias anp='ansible-playbook'"

# ansible-playbook env.yml -k
# vi keypair_new.yml
- name: Create known_hosts between server and nodes
  hosts: all
  connection: local
  serial: 1
  gather_facts: no

  tasks:
    - name: ssh-keyscan for known_hosts file
      command: /usr/bin/ssh-keyscan -t ecdsa {{ ansible_host }} # 매직 변수 ansible_host 활용하여 hosts ip 호출
      register: keyscan

    - name: input key
      lineinfile:
        path: ~/.ssh/known_hosts
        line: "{{ item }}"
        create: yes
      with_items:
        - "{{ keyscan.stdout_lines }}"

- name: Create authorized_keys between server and nodes
  hosts: all
  connection: local
  gather_facts: no
  vars:
    ansible_password: kosa0401

  tasks:
    - name: ssh-keygen for authorized_keys file
      openssh_keypair: 
        path: ~/.ssh/id_rsa
        size: 2048
        type: rsa
        force: False # overwrite하지 않는다는 False라고 값을 넣거나 아니면 삭제하거나 하면 되겠습니다.

    - name: input key for each node
      connection: ssh
      authorized_key:
        user: root
        state: present
        key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

# anp keypair.yml

수동으로 키넣기

우분투에 키가 안들어가짐

[root@ansible-server bin]# ssh-copy-id root@192.168.0.247
[root@ansible-server bin]# ssh-copy-id root@192.168.0.219

[root@ansible-server bin]# ans 192.168.0.219 -m ping

각 서버에 nginx 설치

[root@ansible-server ~]# mkdir nginx && cd $_

ansible 모듈 서치

ansible get_url 이러고 구글 검색 궁금한 모듈들.

nfs 설치

- name: Setup for nfs server
  hosts: localhost
  gather_facts: no

  tasks:
    - name: make nfs_shared directory
      file:
        path: /root/nfs_shared
        state: directory
        mode: 0777

    - name: configure /etc/exports
      lineinfile:
        path: /etc/exports
        line: /root/nfs_shared 192.168.0.0/20(rw,sync)

    - name: Install NFS
      yum:
        name: nfs-utils
        state: present

    - name: nfs service start
      service:
        name: nfs-server
        state: restarted
        enabled: yes

- name: Setup for nfs clients
  hosts: centos
  gather_facts: no

  tasks:
    - name: make nfs_client directory
      file:
        path: /root/nfs
        state: directory

    - name: Install NFS
      yum:
        name: nfs-utils
        state: present

    - name: mount point directory as client
      mount:
        path: /root/nfs
        src: 192.168.0.192:/root/nfs_shared
        fstype: nfs
        state: mounted

- name: Setup for nfs clients U
  hosts: ubuntu
  gather_facts: no

  tasks:
    - name: make nfs_client directory
      file:
        path: /root/nfs
        state: directory

    - name: Install NFS-U
      apt:
        pkg: nfs-common
        state: present
        update_cache: yes

    - name: mount point directory as client
      mount:
        path: /root/nfs
        src: 192.168.0.192:/root/nfs_shared
        fstype: nfs
        opts: nfsvers=3
        state: mounted

도커

타입2와 유사한 경향이 있다.

0개의 댓글