I want to copy a directory from my Salt master to my Salt Minion. All the files are executable. I want to then execute the files on my Salt minion.
I want to achieve this using a Salt state. This is what I have so far:
copy_scripts:
file.recurse:
- name: /root/scripts
- source: salt://files/scripts
- user: root
- group: root
- file_mode: 744
This puts the files on my Salt minion. How can I execute all the scripts inside?
You can do this with cmd.script. cmd.script will copy the file (script?) to the minion and run it in a single state.
https://docs.saltstack.com/en/latest/ref/states/all/salt.states.cmd.html#salt.states.cmd.script
You'd need a state for every script you wanted to run.
bonus points: You can use Jinja to loop through a list to generate the states automatically https://docs.saltstack.com/en/latest/topics/tutorials/states_pt3.html
After copying the directory to the desired path, you can use cmd.run to execute these scripts.
I have a project file like
windows:
- server:
layout: even-vertical
panes:
- ssh -t {pass value in here} tail -f -n 100 /var/log/app.log
-
I would like to pass the SSH host in as I start the session. Something like
mux project for.bar
Can this be done
Checkout this section Tmuxinator's readme.
You can also pass arguments to your projects, and access them with
ERB. Simple arguments are available in an array named #args.
Eg:
$ tmuxinator start project foo
~/.tmuxinator/project.yml
name: project root: ~/<%= #args[0] %>
... You can also pass key-value pairs using the format key=value.
These will be available in a hash named #settings.
Eg:
$ tmuxinator start project workspace=~/workspace/todo
~/.tmuxinator/project.yml
name: project root: ~/<%= #settings["workspace"] %>
...
I'm writing an Ansible play to automate new user creation in 100+ Unix servers. I've got the part right where it creates an user and assigns password. But our organization hardening policy demands, whenever a new user is added, username must be updated in "AllowUsers" parameter of sshd_config file. I'm new to Ansible and have no clue how to get this done.
Here's "AllowUsers" section of sshd_config file.
AllowUsers root user1 user2 user2
This is how it should be after adding a new user "testuser"
AllowUsers root user1 user2 testuser
I searched for solution that doesn't do anything if the user already is on the list. This is how it should work in Ansible. My solution at first searches for the user and only if the user is not on the list it will be added.
tasks:
- name: Check if bamboo user already is in SSHD AllowUsers list
command: grep -P '^[ \t]*AllowUsers[ \t]+([-\w ]+[ \t]+)*bamboo([ \t]+.+)*$' /etc/ssh/sshd_config
register: allow_users_exists
changed_when: no
ignore_errors: yes
- name: Allow bamboo user SSH login
lineinfile:
regexp: ^[ \t]*AllowUsers([ \t]+.*)$
line: AllowUsers bamboo\1
dest: /etc/ssh/sshd_config
backrefs: yes
validate: sshd -t -f %s
when: allow_users_exists.rc != 0
notify:
- reload sshd
handlers:
- name: reload sshd
service:
name: sshd
state: reloaded
In this special case I'm searching for static user "bamboo". You could use a variable instead like this:
command: grep -P '^[ \t]*AllowUsers[ \t]+([-\w ]+[ \t]+)*{{ username | regex_escape() }}([ \t]+.+)*$' /etc/ssh/sshd_config
and
line: AllowUsers {{ username }}\1
Results
In:
AllowUsers ubuntu #sdfd
Out:
AllowUsers bamboo ubuntu #sdfd
In:
AllowUsers ubuntu
Out:
AllowUsers bamboo ubuntu
In:
AllowUsers ubuntu bamboo
Out:
AllowUsers ubuntu bamboo
with lineinfile module match regexp of the line say "^AllowUsers .+" and construct the line with new user name. some sample example
- command: grep "^AllowUsers " /etc/ssh/sshd_config
register: old_user_list
- lineinfile:
regexp: "^AllowUsers .+"
line: "{{ old_user_list.stdout }} {{new-user-name}}"
when: old_user_list.rc == 0
How do you create a directory www at /srv on a Debian-based system using an Ansible playbook?
You want the file module. To create a directory, you need to specify the option state: directory :
- name: Creates directory
file:
path: /src/www
state: directory
You can see other options at https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html
You can even extend the file module and even set the owner,group & permission through it. (Ref: Ansible file documentation)
- name: Creates directory
file:
path: /src/www
state: directory
owner: www-data
group: www-data
mode: 0775
Even, you can create the directories recursively:
- name: Creates directory
file:
path: /src/www
state: directory
owner: www-data
group: www-data
mode: 0775
recurse: yes
This way, it will create both directories, if they didn't exist.
Additional for all answers here, there is lot of situations when you need to create more then one directory so it is a good idea to use loops instead creating separate task for each directory.
- name: creates multiple directories in one task
file:
path: "{{ item }}"
state: directory
loop:
- /srv/www
- /dir/foo
- /dir/bar
you can create using:
Latest version 2<
- name: Create Folder
file:
path: /srv/www/
owner: user
group: user
mode: 0755
state: directory
Older version
- name: Create Folder
file:
path=/srv/www/
owner=user
group=user
mode=0755
state=directory
Refer - http://docs.ansible.com/ansible/file_module.html
Directory can be created using file module only, as directory is nothing but a file.
# create a directory if it doesn't exist
- file:
path: /etc/some_directory
state: directory
mode: 0755
owner: foo
group: foo
- name: Create a directory
ansible.builtin.file:
path: /etc/some_directory
state: directory
mode: '0755'
- file:
path: /etc/some_directory
state: directory
mode: 0755
owner: someone
group: somegroup
That's the way you can actually also set the permissions, the owner and the group. The last three parameters are not obligatory.
You can create a directory. using
# create a directory if it doesn't exist
- file: path=/src/www state=directory mode=0755
You can also consult
http://docs.ansible.com/ansible/file_module.html
for further details regaridng directory and file system.
Just need to put condition to execute task for specific distribution
- name: Creates directory
file: path=/src/www state=directory
when: ansible_distribution == 'Debian'
You can use the statement
- name: webfolder - Creates web folder
file: path=/srv/www state=directory owner=www-data group=www-data mode=0775`
enter code here
- name: creating directory in ansible
file:
path: /src/www
state: directory
owner: foo
you can refer to ansible documentation
If you want to create a directory in windows:
- name: create folder in Windows
win_file:
path: C:\Temp\folder\subfolder
state: directory
See the win_file module for more information.
to create directory
ansible host_name -m file -a "dest=/home/ansible/vndir state=directory"
We have modules available to create directory , file in ansible
Example
- name: Creates directory
file:
path: /src/www
state: directory
you can use the "file" module in this case, there are so many arguments that you can pass for a newly created directory like the owner, group, location, mode and so on.....
please refer to this document for the detailed explanation on the file module...
https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module
remember this module is not just for creating the directory !!!
To check if directory exists and then run some task (e.g. create directory) use the following
- name: Check if output directory exists
stat:
path: /path/to/output
register: output_folder
- name: Create output directory if not exists
file:
path: /path/to/output
state: directory
owner: user
group: user
mode: 0775
when: output_folder.stat.exists == false
You can do it as one of the following ways:
Example 1: If Parent Directory already exists:
- name: Create a new directory www at given path
ansible.builtin.file:
path: /srv/www/
state: directory
mode: '0755'
Example 2: If Parent Directory does not exist:
- name: Create a new directory www at given path recursively
ansible.builtin.file:
path: /srv/www/
state: directory
mode: '0755'
recurse: yes
Here in Example 2, it will recursively create both directories if they are not present.
You can see the Official Documentation for further info on file_module
You can directly run the command and create directly using ansible
ansible -v targethostname -m shell -a "mkdir /srv/www" -u targetuser
OR
ansible -v targethostname -m file -a "path=/srv/www state=directory" -u targetuser
---
- hosts: all
connection: local
tasks:
- name: Creates directory
file: path=/src/www state=directory
Above playbook will create www directory in /src path.
Before running above playbook. Please make sure your ansible host connection should be set,
"localhost ansible_connection=local"
should be present in /etc/ansible/hosts
for more information please let me know.
Use file module to create a directory and get the details about file module using command "ansible-doc file"
Here is an option "state" that explains:
If directory, all immediate subdirectories will be created if they do not exist, since 1.7 they will be created with the supplied permissions.
If file, the file will NOT be created if it does not exist, see the [copy] or [template] module if you want that behavior.
If link, the symbolic link will be created or changed. Use hard for hardlinks.
If absent, directories will be recursively deleted, and files or symlinks will be unlinked.
Note that file will not fail if the path does not exist as the state did not change.
If touch (new in 1.4), an empty file will be created if the path does not
exist, while an existing file or directory will receive updated file
access and modification times (similar to the way touch works from
the command line).
Easiest way to make a directory in Ansible.
name: Create your_directory if it doesn't exist.
file:
path: /etc/your_directory
OR
You want to give sudo privileges to that directory.
name: Create your_directory if it doesn't exist.
file:
path: /etc/your_directory
mode: '777'
Hello good afternoon team.
I share the following with you.
- name: Validar Directorio
stat:
path: /tmp/Sabana
register: sabana_directorio
- debug:
msg: "Existe"
when: sabana_directorio.stat.isdir == sabana_directorio.stat.isdir
- name: Crear el directorio si no existe.
file:
path: /tmp/Sabana
state: directory
when: sabana_directorio.stat.exists == false
With which you can validate if the directory exists before creating it
I see lots of Playbooks examples and I would like to mention the Adhoc commands example.
$ansible -i inventory -m file -a "path=/tmp/direcory state=directory ( instead of directory we can mention touch to create files)
You need to use file module for this case. Below playbook you can use for your reference.
---
- hosts: <Your target host group>
name: play1
tasks:
- name: Create Directory
files:
path=/srv/www/
owner=<Intended User>
mode=<Intended permission, e.g.: 0750>
state=directory
here is easier way.
- name: create dir
command: mkdir -p dir dir/a dir/b
How do I symlink all dotfiles on a dir to my home dir using zsh? i.e. I have a dir with .gitconfig in it, and I'd like to symlink it to ~/.gitconfig.
Thanks!
If you want the sym-links at your local dir to your home dir:
cd my-own-local-dir
for ifile in .*(.); do # notice the '(.)' matching only normal files
# rm -fv $ifile # COMMENTED OUT, removing the original...
ln -vs ~/$ifile $ifile # sym-links to your home dir...
done
You should try dotbot. It supports to manage dotfiles by using a yaml file. Easy for mapping :D
For example:
- link:
~/.config/terminator:
create: true
path: config/terminator
~/.vim: vim
~/.vimrc:
relink: true
path: vimrc
~/.zshrc:
force: true
path: zshrc