What's the easy way to auto create non existing dir in ansible

前端 未结 7 657
悲哀的现实
悲哀的现实 2020-12-23 15:37

In my Ansible playbook many times i need to create file there

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


        
相关标签:
7条回答
  • 2020-12-23 16:02

    Right now, this is the only way

    - 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"
    
    0 讨论(0)
  • 2020-12-23 16:03

    According to the latest document when state is set to be directory, you don't need to use parameter recurse to create parent directories, file module will take care of it.

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

    this is fare enough to create the parent directories data and test with foo

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

    0 讨论(0)
  • 2020-12-23 16:10

    copy module creates the directory if it's not there. In this case it created the resolved.conf.d directory

    - 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
    
    0 讨论(0)
  • 2020-12-23 16:17

    To ensure success with a full path use recurse=yes

    - name: ensure custom facts directory exists
        file: >
          path=/etc/ansible/facts.d
          recurse=yes
          state=directory
    
    0 讨论(0)
  • 2020-12-23 16:21

    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

    0 讨论(0)
  • 2020-12-23 16:25

    If you are running Ansible >= 2.0 there is also the dirname filter you can use to extract the directory part of a path. That way you can just use one variable to hold the entire path to make sure both tasks never get out of sync.

    So for example if you have playbook with dest_path defined in a variable like this you can reuse the same variable:

    - 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 }}"
    
    0 讨论(0)
提交回复
热议问题