Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: How to chmod +x a file with Ansible?

Tags:

bash

ansible

What is the best way to chmod + x a file with ansible.

Converting the following script to ansible format.

mv /tmp/metadata.sh /usr/local/bin/meta.sh chmod +x /usr/local/bin/meta.sh 

This is what I have so far..

- name: move /tmp/metadata.sh to /usr/local/bin/metadata.sh   command: mv /tmp/metadata.sh /usr/local/bin/metadata.sh 
like image 241
Atlantic0 Avatar asked Nov 09 '16 11:11

Atlantic0


People also ask

Which parameter is used to change the permission of a file in Ansible?

The parameter “mode” sets the permissions in the UNIX way of the file/directory. The state defines the type of object we are modifying, the default is “file” but we could handle also directories, hardlink, symlink, or only update the access time with the “touch” option.

What is recurse in Ansible?

recurse. boolean. added in 1.1 of ansible.builtin. Recursively set the specified file attributes on directory contents. This applies only when state is set to directory .

Which Ansible modules can be used to change the contents of a file?

The lineinfile - module is the best option, if you just want to add, replace or remove one line. The replace - module is best, if you want to add, replace or delete several lines.


Video Answer


2 Answers

ansible has mode parameter in file module exactly for this purpose.

To add execute permission for everyone (i.e. chmod a+x on command line):

- name: Changing perm of "/foo/bar.sh", adding "+x"   file: dest=/foo/bar.sh mode=a+x 

Symbolic modes are supported since version 1.8, on a prior version you need to use the octal bits.

like image 56
heemayl Avatar answered Sep 28 '22 10:09

heemayl


The mode parameter should be specified when using the copy module.

Example:

- name: copy file and set permissions   copy:     src: script.sh     dest: /directory/script.sh     mode: a+x 
like image 39
Adam Avatar answered Sep 28 '22 09:09

Adam