Tuesday, March 16, 2021

Ansible - Working with file module

Ansible Modules
There are hundreds of ansible modules and they are group into catagories depending ontype of function modules functions.

Working with File module.
We will use file, copy, fetch synchronize and lineinfile together

using file module we can,
- create file, directory
- Change the ownership
- File permission and also ACL
- Add or delete lines within a file
- You can copy from one location to another (Control to managed node - fetch module)
and many more

There are lots of other modules which are bundled with file module.


Google for "ansible files"
https://docs.ansible.com/ansible/2.9/modules/list_of_files_modules.html


> ansible all -i hosts \
-m <name_of_module> \
-a "name=options" \
--become
--forks=7

> ansible all -i myhosts \
-m file \
-a "path=/data/db state=directory owner=admin group=admin mode=0755" \
--become \
--forks=7

directory is created. You can verify
> ansible  all -i hosts \
-m command \
-a "ls -ld /data
--become
--forks=7

Copy files to managed nodes using synchronize


Lets create some files under
$ touch file1 file2 file2

ansible all -i myhosts \
-m command \
-a "ls -ltr /data" \
--become
--forks=7

If you review the output, you see no value returned.

Now, lets google for ansible files and go to synchronize module

review the arguements
mandatory or required is dest
and another requirement arguement is src - can be absolutely or relative

$ ansible all -i myhosts \
-m synchronize \
-a "src=/home/jay/data dest=/data/data" \
--become \
--forks=7

Now, run this command. Depending the size of the files, it will copy content ove to managed nodes.

Verify it now
$ ansible -i my_hosts all -a "ls -l /data/data" --become --forks=7
$ ansible -i myhosts all -a "du s-h /data/data" --become --forks=7

it wil verify the size.

Its lookinto lineinfile module
google for ansible file module and look at lineinfile module
find required arguements

path
status
mode?
insertbefore/insertafter/regexp (pattern matching)
line

look at the example and review/analyze
look for adding user to sudoers or enabling selinux

inlinefile example

lets execute the command,

lets login to our managed node w1
$ ssh w1
$ which java
/usr/bin/java


We might have other versions
$ ls -ltr /etc/alternatives
$ ls -ltr /etc/alternatives/java

look at the location where softlink is pointing to.
lets copy the path
/usr/lib/jvm/java-1.8.0-oepnjdk-1.8.0.181-3.b13.el7_5.x86/jre/bin/java

now, lets set up this java path on user's environment variable
$ at .bash_profile

There is no java path setup

now, we know the path to java . Lets copy that path and exit out of the shell
Now, we are on control node

lets execute the ansible command
$ ansible -i myhosts all -m lineinfile -a "path=/home/jay/.bash_profile" \
line='export JAVA_HOME="/usr/lib/jvm/java-1.8.0-oepnjdk-1.8.0.181-3.b13.el7_5.x86/jre'" \
 --forks=7

now, login to the server and verify
$ ssh w2
$ cat .bash_profile

JAVA_HOME is added to the profile

exit and go back to control node.
Lets run and verify
$ ansible -i myhosts all -a "cat /home/jay/.bash_profile" --forks=7

you should be able to view the output.

There are hundreds of modules. Please explore and understand which one fits your requirement.



No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...