Programming

ansible에서 존재하지 않는 디렉토리를 자동으로 만드는 쉬운 방법은 무엇입니까?

procodes 2020. 8. 23. 10:07
반응형

ansible에서 존재하지 않는 디렉토리를 자동으로 만드는 쉬운 방법은 무엇입니까?


내 Ansible 플레이 북에서 여러 번 거기에 파일을 만들어야합니다.

 - name: Copy file
   template:
     src: code.conf.j2
     dest: "{{project_root}}/conf/code.conf"

이제 여러 번 confdir이 없습니다. 그런 다음 먼저 해당 디렉토리를 만들기 위해 더 많은 작업을 만들어야합니다.

어떤 옵션과 함께 존재하지 않는 경우 디렉토리를 자동 생성하는 쉬운 방법이 있습니까?


지금이게 유일한 방법

- name: Ensures {{project_root}}/conf dir exists
  file: path={{project_root}}/conf state=directory
- name: Copy file
  template:
    src: code.conf.j2
    dest: "{{project_root}}/conf/code.conf"

전체 경로로 성공하려면 recurse = yes를 사용하십시오.

- name: ensure custom facts directory exists
    file: >
      path=/etc/ansible/facts.d
      recurse=yes
      state=directory

AFAIK,이 작업을 수행 할 수 있는 유일한 방법 state=directory옵션 을 사용하는 것입니다. template모듈은 대부분의 copy옵션을 지원하고 차례로 대부분의 옵션을 지원 하지만 file이와 같은 것을 사용할 수는 없습니다 state=directory. 더욱이, 그것은 상당히 혼란 스러울 것입니다 ( {{project_root}}/conf/code.conf디렉토리라는 것을 의미할까요? 아니면 그것이 {{project_root}}/conf/먼저 생성되어야 한다는 것을 의미할까요?) .

그래서 지금은 이전 file작업 을 추가하지 않고는 이것이 가능하지 않다고 생각합니다 .

- file: 
    path: "{{project_root}}/conf"
    state: directory
    recurse: yes

Ansible> = 2.0을 실행 하는 경우 경로의 디렉토리 부분을 추출하는 데 사용할 수 있는 dirname 필터도 있습니다. 이렇게하면 하나의 변수를 사용하여 전체 경로를 유지하여 두 작업이 동기화되지 않도록 할 수 있습니다.

예를 들어 다음과 dest_path같이 변수에 정의 된 플레이 북 이있는 경우 동일한 변수를 재사용 할 수 있습니다.

- name: My playbook
  vars:
    dest_path: /home/ubuntu/some_dir/some_file.txt
  tasks:

    - name: Make sure destination dir exists
      file:
        path: "{{ dest_path | dirname }}"
        state: directory
        recurse: yes

    # now this task is always save to run no matter how dest_path get's changed arround
    - name: Add file or template to remote instance
      template: 
        src: foo.txt.j2
        dest: "{{ dest_path }}"

최신 문서에 따르면 상태가 디렉토리로 설정되면 부모 디렉토리를 생성 하기 위해 매개 변수 recurse사용할 필요가 없으며 파일 모듈이 처리합니다.

- name: create directory with parent directories
  file:
    path: /data/test/foo
    state: directory

이 상위 디렉토리에 만들 수있는 요리 충분한 데이터테스트foo는

please refer the parameter description - "state" http://docs.ansible.com/ansible/latest/modules/file_module.html


you can create the folder using the following depending on your ansible version.

Latest version 2<

- name: Create Folder
  file: 
   path: "{{project_root}}/conf"
   recurse: yes
   state: directory

Older version:

- name: Create Folder
  file: 
      path="{{project_root}}/conf"
      recurse: yes
      state=directory

Refer - http://docs.ansible.com/ansible/latest/file_module.html

참고URL : https://stackoverflow.com/questions/22472168/whats-the-easy-way-to-auto-create-non-existing-dir-in-ansible

반응형