ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ansible 설치 및 기본 옵션
    OS/ansible 2022. 4. 19. 10:54

    Master : 192.168.72.131

    node1 : 192.168.72.132

    node2 : 192.168.72.133

     

    ================================================

     

    [ansible 설치]


    yum install -y epel-release

    yum install -y ansible

    ansible 사용할 계정생성 및 sudo 권한 부여
    useradd adam
    passwd 1234
    vi /etc/sudoers.d/adam
    admin ALL=(ALL) NOPASSWD: ALL
    :wq!

    controll 서버에 ssh key 생성
    ssh-keygen

    서버에 key 등록
    ssh-copy-id 원격서버계정@원격서버ip
    ssh-copy-id adam@192.168.72.132
    또는
    ssh-copy-id 원격서버ip

     

    등록된 모든 서버들 핑 체크
    ansible all -m ping

     

    =================================================

     

    [ssh 포트변경]


    node1,2 ssh 포트 변경
    /etc/ssh/sshd_config
    Port 22222

    인벤토리사용 - 원격서버 ip등록, ssh포트를 변경(22222)
    vi /etc/ansible/hosts

    [all:vars]
    ansible_port=22222

    [hosts]
    192.168.72.132 #hosts01
    192.168.72.133 #hosts02

    :wq!

     

    =================================================

     

    [ansible계정에 sudo권한주기]
    sudo vi /etc/ansible/ansible.cfg
    [privilege_escalation]
    become=True
    :wq!

     

    =================================================

     

    [ansible 디폴트 인벤토리 파일 지정 - 같은경로의 study.yaml, common.yaml 파일 참조]
    sudo vi /etc/ansible/ansible.cfg
    [defaults]
    inventory =./study.yaml, common.yaml
    :wq!

     

    =================================================


    ansible 옵션
    -i : 적용될 노드들을 선택
    -m : 사용하는 모듈
    -k : 암호 물어보도록
    --list-hosts : 적용되는 노드 확인

    특정인벤토리의 특정 서버 핑체크
    ansible -i study.ini 192.168.72.132 -m ping

    ansible 명령어 적용 리스트 확인
    ansible -i study.yaml all -m ping --list-hosts

    ansible shell 사용
    ansible all -m shell -a "df -h"
    ** -a는 shell의 cd ls df 와 같은 명령구문 사용
    ansible -i study.yaml all -m shell -a "systemctl status httpd"

    경로의 파일리스트
    ansible all -m command -a 'ls -l /'

    host01/02 'df -Th' 명령어를 전송하여 결과값을 csv 파일 생성
    ansible all -m command -a 'df -Th' > dfth.csv

    계정생성 및 비밀번호설정
    ansible all -m user -a "name=mary update_password=always password={{ 'mary1234' | password_hash('sha512') }}"

    sudo권한주기
    ansible all -m shell -a "echo 'mary  ALL=(ALL)   ALL' > /etc/sudoers.d/mary"

    계정 삭제
    ansible all -m user -a 'name="mary" state=absent'

    **state=absent : 삭제
      state=present : 설치

     

    =================================================


    [ansible - yum 사용]
    [설치]
    ansible all -m yum -a "name=httpd state=present"
    [삭제]
    ansible all -m yum -a "name=httpd state=absent"
    [shell]
    ansible -i study.yaml all -m shell -a "systemctl status httpd"
    ansible -i study.yaml all -m shell -a "yum install -y httpd"


     

    기타 여러가지 응용가능하니 참고

    'OS > ansible' 카테고리의 다른 글

    ansible-playbook 02 tar  (0) 2022.04.19
    ansible-playbook 01 apache(httpd), nginx 설치  (0) 2022.04.19
    ansible inventory .yaml 작성법  (0) 2022.04.19
    ansible inventory .ini파일 작성법  (0) 2022.04.19

    댓글

Designed by Tistory.