Ansible

Ansible 모듈 사용해보기 (shell, user, file, copy ...등)

황동리 2024. 2. 5. 16:31
반응형

오늘은 모듈 shell, user, file, copy, fetch, yum, service, lineinfile에 대해 알아보았습니다.


Ad-hoc 명령어

module: user
ansible all -m user -a "name=a"
=> 모든 서버에 'a'라는 이름의 유저 생성


ansible all -m user -a "name=a state=absent"
=> 모든 서버에 'a'라는 이름의 유저 삭제
=> 다만 user 모듈로 유저 생성 후 삭제하면 /home, /var/spool/mail 디렉토리에 생성된 유저 디렉토리는 삭제 되지 않는다.


ansible all -m user -a "name=a update_password=always password={{ 'It1' | password_hash('sha512')}}"
=> 비밀번호 'It1'을 암호화 한 유저 'a'를 생성

module: file
ansible all -m file -a "path=/root/health.html state=absent"
=> path경로에 있는 파일을 삭제해준다.

module: copy
ansible all -m copy -a "src=/root/health.html dest=/var/www/html/health.html"
=> src 경로에 있는 파일을 dest 경로에 복사해준다.

module: fetch
ansible all -m fetch -a "src=/root/health.html dest=/var/www/html/health.html flat=yes"
=> src 경로에 있는 파일을 dest 경로에 복사해준다, 그러나 flat yes설정을 하지 않으면 dest 경로에 src 경로 전체가 복사가 됩니다.

module: yum
ansible all -m yum -a "name=httpd state=latest"
=> httpd패키지를 가장 최신버전으로 받는다.

module: service
ansible all -m service -a "name=httpd state=started"
=> httpd 서비스를 시작한다.


ansible all -m service -a "name=httpd state=stop"
=> httpd 서비스를 중지한다.


ansible all -m service -a "name=httpd state=restarted"
=> httpd 서비스를 재시작한다.

module: lineinfile
ansible all -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='DirectoryIndex index.html' line='DirectoryIndex health.html'"
=> path에 있는 설정파일에서 regexp문자열을 line문자열로 바꿔준다.


playbook 명령어

module: user

---
- name: create user and password
hosts: all 
tasks: 
	-name: create user 
    user: 
    	name: a **=> a 라는 이름의 유저 생성**
        update_password: always 
        password: "{{ 'It1' | password_hash('sha512') }}" **=> It1 패스워드 암호화**


---
- name: delete user
hosts: all
tasks: 
	-name: delete user 
    user: 
    	name: a
        state: absent **=> 유저 a 삭제**

module: file

---
- name: delete file
hosts: all
tasks: 
	-name: delete file 
    file: 
    	path: /var/www/html/health.html 
        state: absent **=> 위 경로의 파일 삭제**

module: copy

---
- name: copy file(ansible 마스터에서 원격지 slave들에게 파일 복사)
hosts: all
tasks: 
	-name: copy file 
    copy: 
    	src: /root/health.html **=> 복사하고 싶은 파일 경로**
        dest: /var/www/html/health.html **=> 복사 받고 싶은 파일 경로**

module: fetch

---
- name: fetch file(원격지 slave에게서 ansible 마스터에게 파일을 복사)
hosts: {원격지 서버}
tasks: 
	-name: fetch file 
     fetch: 
    	src: /var/www/html/health.html **=> 복사하고 싶은 파일 경로**
        dest: /root/health.html **=> 복사받고 싶은 파일 경로**

module: yum

---
- name: yum install httpd
hosts: all
tasks: 
	-name: yum install httpd
     yum: 
    	name: httpd **=> 받고 싶은 패키지 이름**
        state: latest(가장 최신버전) **=> 최신버전으로 받겠다는 뜻**        


---
- name: yum remove httpd
hosts: all
tasks: 
	-name: yum remove httpd
     yum: 
    	name: httpd **=> 삭제 하고 싶은 패키지 이름**
        state: removed **=> 삭제한다**

module: service

---
- name: httpd service start
hosts: all
tasks: 
	-name: httpd servcie start
     service: 
    	name: httpd **=> 동작하고자 하는 서비스 이름**
        state: started **=> 서비스 시작**


---
- name: httpd service stop
hosts: all
tasks: 
	-name: httpd servcie stop
     service: 
    	name: httpd **=> 동작하고자 하는 서비스 이름**
        state: stop **=> 서비스 종료**


---
- name: httpd service restart
hosts: all
tasks: 
	-name: httpd servcie restart
     service: 
    	name: httpd
        state: restarted **=> 서비스 재시작**

module: lineinfile

---
- name: 설정파일의 특정 줄의 문자 변경
hosts: all
tasks: 
	-name: 설정파일의 특정 줄의 문자 변경
     lineinfile: 
    	path: /etc/httpd/conf/httpd.conf **=> 설정 파일의 경로**
		regexp: 'DirectoryIndex index.html' **=> 변경하고자 하는 문자열 **
        line: 'DirectoryIndex health.html' **=> 변경될 문자열**
반응형