FreeKB - Ansible file module (create directory mkdir)
Ansible - file module (create directory mkdir)

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The file module with state: directory is used to create a directory on a managed node (e.g. target system). In this example, the /tmp/example directory will be created if it does not exist.

- name: mkdir /tmp/example
  file:
    path: /tmp/example
    state: directory

 

Directory created

If the directory was successfully created, the play should indicate changed.

TASK [mkdir /tmp/example]
changed: [server1.example.com]

 

Directory not created

If the directory was not created, the play should indicate ok. This is the expected behavior if the directory already exists. If the directory already exists, the directory will not be created, overwritten, or changed.

TASK [mkdir /tmp/example]
ok: [server1.example.com]

 

Owner / Group / Mode

When the owner, group or mode options are used, if the directory already exists, the directory will be updated with the owner, group and mode.

- name: update /tmp/example owner group mode
  file:
    path: /tmp/example
    state: directory
    owner: john.doe
    group: admins
    mode: "0770"

 

Permission denied

If permission denied is returned when running the play, refer to our article on resolving permission denied.

 


You may also want to use x.stat.exists to determine if the directory exists, and x.stat.isdir to determine if the object is a directory.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 39821 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |