ansible을 사용하여 구성 파일의 특정 섹션에 데이터 삽입

ansible을 사용하여 구성 파일의 특정 섹션에 데이터 삽입

ansible을 사용하는 데 특별한 문제가 있습니다. 이 질문은 매우 이상하고 위험합니다. 파일의 특정 부분에 데이터를 삽입하는 코드를 작성했습니다. 즉, [database]say 뒤에 줄을 추가합니다 /etc/cinder/cinder.conf.

문제는 때때로 태그 뒤에 콘텐츠를 올바르게 추가 [database]하지만 때로는 파일의 한 줄을 보고 혼란스러워지고 # put ur infore here for [database] 실제로 있어야 할 위치 대신 그 아래에 필요한 줄을 추가한다는 것입니다.

   - name: Adding Entries in "/etc/cinder/cinder.conf"
     lineinfile:
      dest: "/etc/cinder/cinder.conf"
      insertafter:  "{{ item.inserts }}"
      state: present
      line: "{{ item.lines }}"
     with_items:
      - { inserts: '\[database\]', lines: 'rpc_backend = rabbit' }

이러한 상황은 프로덕션 환경에서는 매우 위험합니다! 데이터를 올바르게 추가하는 방법은 무엇입니까?

답변1

주석 일치를 방지하려면 정규식을 줄 시작 부분에 고정하세요.

- { inserts: '^\[database\]', lines: 'rpc_backend = rabbit' }

답변2

ini_file모듈을 사용할 수 있습니다general collection:

- name: Adding Entries in "/etc/cinder/cinder.conf"
  community.general.ini_file:
    path: "/etc/cinder/cinder.conf"
    section: database
    option: rpc_backend
    value: rabbit
    backup: true

컬렉션을 설치하려면 다음을 실행하세요.

$ ansible-galaxy collection install community.general

requirements.yaml또는 다음과 같이 추가하십시오 .

collections:
  - name: community.general
    version: 5.0.1

그런 다음 실행

$ ansible-galaxy install -r requirements.yaml

관련 정보