Ansible을 사용하여 템플릿 파일 폴더 배포 create x template

템플릿과 동일한 이름을 사용하지만 각 파일에 대해 템플릿 모듈을 사용하는 대신 .j2 확장명을 사용하지 않고 템플릿 .j2 폴더로 가득 찬 폴더를 Linux 상자에 배포하는 쉬운 방법이 있습니까?

지금은 긴 목록이 있습니다.

- name: create x template
  template:
    src=files/x.conf.j2
    dest=/tmp/x.conf
    owner=root
    group=root
    mode=0755
  notify:
    - restart myService



답변

당신이 사용할 수있는 with_fileglob템플릿 디렉토리에서 파일의 목록을 얻을이 같은 J2 확장을 제거하기 위해 필터를 사용하는 ..

- name: create x template
  template:
    src: {{ item }}
    dest: /tmp/{{ item | basename | regex_replace('\.j2','') }}
  with_fileglob:
    - ../templates/*.j2


답변

Ansible의 제작자 인 Michael DeHaan은 CoderWall 에 매우 비슷한 문제에 대해 글을 올렸 습니다 . 필요에 따라 (예 : 권한 및 소유권) 조정하고 확장 할 수 있습니다. 게시물의 관련 부분은 다음과 같습니다.


with_items“및 단일 notify명령문 을 사용하여 단순화 할 수 있습니다 . 작업이 변경되면 플레이 북 실행이 끝날 때 서비스를 다시 시작해야하는 것과 동일한 방식으로 서비스에 알림이 전송됩니다.

 - name:  template everything for fooserv
   template: src={{item.src}} dest={{item.dest}}
   with_items:
      - { src: 'templates/foo.j2', dest: '/etc/splat/foo.conf' }
      - { src: 'templates/bar.j2', dest: '/etc/splat/bar.conf' }
   notify:
      - restart fooserv

둘 이상의 고유 한 인수를 취하는 작업이 item있으므로 ‘ template:‘행 에 ” ” 라고 말하지 않고 with_items해시 (사전) 변수와 함께 사용 하십시오. 원하는 경우 목록을 사용하여 조금 더 짧게 유지할 수도 있습니다. 이것은 스타일 선호도입니다.

 - name:  template everything for fooserv
   template: src={{item.0}} dest={{item.1}}
   with_items:
      - [ 'templates/foo.j2', '/etc/splat/foo.conf' ]
      - [ 'templates/bar.j2', '/etc/splat/bar.conf' ]
   notify:
      - restart fooserv

물론 그룹에 groupvars/webservers필요한 모든 변수를 정의하기 위한 ” “파일 webservers또는 varsfiles플레이 북 내의 ” “지시문 에서로드 된 YAML 파일 과 같이 다른 파일로 이동 한 목록을 정의 할 수도 있습니다 . 그렇게하면 어떻게 정리할 수 있는지보십시오.

- name: template everything for fooserv
  template: src={{item.src}} dest={{item.dest}}
  with_items: {{fooserv_template_files}}
  notify:
      - restart fooserv


답변

Russel의 답변은 효과가 있지만 개선이 필요합니다.

- name: create x template
- template: src={{ item }} dest=/tmp/{{ item | basename | regex_replace('.j2','') }}
- with_fileglob:
   - files/*.j2

모든 $의 전나무는 regex_replace의 잘못된 정규 표현식이므로 가야합니다. 두 번째로 모든 파일은 템플릿 디렉토리가 아닌 파일 디렉토리에 있어야합니다.


답변

파일 트리 작업에 도움이되는 파일 트리 조회 플러그인을 작성했습니다.

파일 트리에서 파일을 재귀하고 파일 속성을 기반으로 작업 (예 : 템플릿 또는 복사)을 수행합니다. 상대 경로가 반환되므로 대상 시스템에서 파일 트리를 쉽게 다시 만들 수 있습니다.

- name: Template complete tree
  template:
    src: '{{ item.src }}'
    dest: /web/{{ item.path }}
    force: yes
  with_filetree: some/path/
  when: item.state == 'file'

더 읽기 쉬운 플레이 북을 만듭니다.


답변

아래 명령은 템플릿에서 j2 파일에 대한 재귀 적 조회를 수행하고 대상으로 옮겼습니다. 누군가 대상으로 템플릿의 재귀 복사본을 찾는 데 도움이되기를 바랍니다.

     - name: Copying the templated jinja2 files
       template: src={{item}} dest={{RUN_TIME}}/{{ item | regex_replace(role_path+'/templates','') | regex_replace('\.j2', '') }}
       with_items: "{{ lookup('pipe','find {{role_path}}/templates -type f').split('\n') }}"


답변

디렉토리에서 실제 파일 목록을 자동으로 가져 와서 나중에 반복 할 수 있습니다.

- name:         get the list of templates to transfer
  local_action: "shell ls templates/* | sed 's~.*/~~g'"
  register:     template_files

- name:         iterate and send templates
  template:     src=templates/{{ item }} dest=/mydestination/{{ item }}
  with_items:
  - "{{ template_files.stdout.splitlines() }}"


답변