Ansible Module 활용 - lineinfile > Ansible 자료실

본문 바로가기
사이트 내 전체검색

Ansible 자료실

실습 Ansible Module 활용 - lineinfile

페이지 정보

profile_image
작성자 AnsibleM
댓글 0건 조회 9,213회 작성일 21-08-27 18:08

본문

lineinfile 모듈은 특정 행이 파일에 있는지 확인하거나 역참조 정규식을 사용하여 기존 행을 대체합니다.
파일의 한 줄만 변경하려는 경우에 주로 유용하며, 유사한 모듈로는 replace, blockinfile 등이 있습니다.


1. lineinfile 모듈 활용 예시

1
2
3
4
5
- name: Ensure SELinux is set to enforcing mode
  ansible.builtin.lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: SELINUX=enforcing
cs
>SELinux 설정 변경

1
2
3
4
5
- name: Make sure group wheel is not in the sudoers configuration
  ansible.builtin.lineinfile:
    path: /etc/sudoers
    state: absent
    regexp: '^%wheel'
cs
>sudoers 파일에서 wheel 그룹 제거

1
2
3
4
5
6
7
8
9
- name: Replace a localhost entry searching for a literal string to avoid escaping
  lineinfile:
    path: /etc/hosts
    search_string: '127.0.0.1'
    line: 127.0.0.1 localhost
    owner: root
    group: root
    mode: '0644'
 
cs
> 특정 문자열 존재할 경우, 해당 행 대체

1
2
3
4
5
- name: Add a line to a file if the file does not exist, without passing regexp
  ansible.builtin.lineinfile:
    path: /tmp/testfile
    line: 192.168.1.99 foo.lab.net foo
    create: yes
cs
> 해당 파일 존재하지 않을 경우 생성


2. lineinfile 모듈 관련 옵션

backup : 타임스탬프 정보를 포함하는 백업 파일 생성 여부
create : 파일이 없는 경우 생성 여부
firstmatch : insertafter, insertbefore 옵션과 함께 사용되며 지정된 정규식과 일치하는 첫 번째 줄에서 동작함
insertafter : 해당 옵션이 지정될 경우, 지정된 정규식의 마지막 일치 항목 뒤에 행이 삽입됨 ( 첫 번째로 일치하는 줄에서 동작이 필요할 경우 firstmatch 옵션 사용 )
insertbefore : 해당 옵션이 지정될 경우, 지정된 정규식의 마지막 일치 항목 앞에 행이 삽입됨 ( 첫 번째로 일치하는 줄에서 동작이 필요할 경우 firstmatch 옵션 사용 )
line : 파일에 삽입하거나 대체할 행
regexp : 파일의 모든 줄에서 찾을 정규식. 정규식이 일치하지 않는 경우 insertafter, insertbefore 설정에 따라 파일에 추가됨.
( state=present 의 경우, 발견된 마지막 줄만 해당 정규식으로 교체
state=absent 의 경우, 해당 줄 제거 )
state : 라인의 존재 여부 ( present - 존재, absent - 삭제 )

댓글목록

등록된 댓글이 없습니다.

회원로그인

회원가입

사이트 정보

회사명 : (주)리눅스데이타시스템 / 대표 : 정정모
서울본사 : 서울특별시 강남구 봉은사로 114길 40 홍선빌딩 2층 / tel : 02-6207-1160
대전지사 : 대전광역시 유성구 노은로174 도원프라자 5층 / tel : 042-331-1161

접속자집계

오늘
1,803
어제
1,534
최대
3,935
전체
758,535
Copyright © www.linuxdata.org All rights reserved.