[ansible] 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


답변

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 }}"


답변

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


답변

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

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

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

매개 변수 설명을 참조하십시오- ” state
http://docs.ansible.com/ansible/latest/modules/file_module.html


답변

ansible 버전에 따라 다음을 사용하여 폴더를 만들 수 있습니다.

최신 버전 2 <

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

이전 버전 :

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

참조-http: //docs.ansible.com/ansible/latest/file_module.html


답변

복사 모듈은 디렉토리가 없으면 생성합니다. 이 경우 resolved.conf.d 디렉토리를 생성했습니다.

- name: put fallback_dns.conf in place
  copy:
    src: fallback_dns.conf
    dest: /etc/systemd/resolved.conf.d/
    mode: '0644'
    owner: root
    group: root
  become: true
  tags: testing