How to create a directory in ansible?

How to create a directory in ansible?

Lets see how to create a directory in ansible with example ansible playbook.

file module can be used to create a directory in ansible.

path and state parameters are mandatory and path parameter is used to specify dir path and state parameter is used to specify directory to create directory and all intermediate subdirectories will be created if they do not exist.

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:

	- name: initialize directory path
  	set_fact:
    	dir_path: "test1/testing2"

	- name: "Creating {{ dir_path }} directory"
  	become: true
  	become_user: root
  	file:
    	path: "{{ dir_path }}"
    	state: directory

	- name: Print the msg
  	debug:
    	msg: "Created {{ dir_path }} directory."

Output:

$ ansible-playbook create_directory.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ************************************************************************************************************************************************************

TASK [initialize directory path] ********************************************************************************************************************************************
ok: [localhost]

TASK [Creating test1/testing2 directory] ************************************************************************************************************************************
changed: [localhost]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "Created test1/testing2 directory."
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=3	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0   

Lets see whether directory is created or not,

$ ls -all .
drwxr-xr-x. 3 root   	root     	22 Nov 12 17:26 test1

$ ls -all test1
drwxr-xr-x. 2 root   	root      	6 Nov 12 17:26 testing2

Here mode is default, read, write and executable to the owner and others and group users will have only read and executable access.

d indicates the directory.

How to update mode (directory permission) in ansible?

We can use the mode parameter in ansible to set the directory permission as required.

Permissions the resulting file or directory should have.

similar to using the /usr/bin/chmod command to set modes.

mode can be octal format like 0644 or 01777 and also string format like '644' or '1777'.

Ansible 1.8, the mode may be specified as a symbolic mode (for example, u+rwx or u=rw,g=r,o=r).

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:

	- name: initialize directory path
  	set_fact:
    	dir_path: "test1/testing2"

	- name: "Creating {{ dir_path }} directory"
  	become: true
  	become_user: root
  	file:
    	path: "{{ dir_path }}"
    	mode: '647'
    	state: directory

	- name: Print the msg
  	debug:
    	msg: "Created {{ dir_path }} directory."

Output:

$ ansible-playbook create_directory.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ************************************************************************************************************************************************************

TASK [initialize directory path] ********************************************************************************************************************************************
ok: [localhost]

TASK [Creating test1/testing2 directory] ************************************************************************************************************************************
changed: [localhost]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "Created test1/testing2 directory."
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=3	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0  

Lets see the directory permission is assigned,

$ ls -all .
drw-r--rwx. 3 root   	root     	22 Nov 12 17:35 test1

$ ls -all test1
drw-r--rwx. 2 root   	root      	6 Nov 12 17:35 testing2

Here the directory is created as mentioned in the mode parameter. read and write to the owner, read only to group users and others will have read, write and executable permissions.




Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^