Tuesday, 13 August 2019

File separation in ansible by Raj Gupta


[ansible@ip-172-31-80-19 ~]$ vi variable.yml
[ansible@ip-172-31-80-19 ~]$ cat variable.yml
var1: var1
var2: var2
var3: var3
var4: var4
var5: var5
var6: var6
var7: var7
var8: var8
var9: var9
var10: var10
var11: var11
var12: var12
var13: var13
var14: var14
var15: var15

Now we are going to include this file in below

[ansible@ip-172-31-80-19 ~]$ vi include.yml
[ansible@ip-172-31-80-19 ~]$ cat include.yml
-
  name: this is our first playbook.
  hosts: all
  vars_files:
    - variable.yml
  tasks:
    -
      name: 'task 1'
      command: touch /tmp/var/{{ var1 }}.txt
    -
      name: 'task 2'
      command: touch /tmp/var/{{ var2 }}.txt
    -
      name: 'task 3'
      command: touch /tmp/var/{{ var3 }}.txt
    -
      name: 'task 4'
      command: touch /tmp/var/{{ var4 }}.txt
    -
      name: 'task 5'
      command: touch /tmp/var/{{ var5 }}.txt
    -
      name: 'task 6'
      command: touch /tmp/var/{{ var6 }}.txt
    -
      name: 'task 7'
      command: touch /tmp/var/{{ var7 }}.txt
    -
      name: 'task 8'
      command: touch /tmp/var/{{ var8 }}.txt
    -
      name: 'task 9'
      command: touch /tmp/var/{{ var9 }}.txt
    -
      name: 'task 10'
      command: touch /tmp/var/{{ var10 }}.txt
    -
      name: 'task 11'
      command: touch /tmp/var/{{ var11 }}.txt
    -
      name: 'task 12'
      command: touch /tmp/var/{{ var12 }}.txt
    -
      name: 'task 13'
      command: touch /tmp/var/{{ var13 }}.txt
    -
      name: 'task 14'
      command: touch /tmp/var/{{ var14 }}.txt
    -
      name: 'task 15'
      command: touch /tmp/var/{{ var15 }}.txt

[ansible@ip-172-31-80-19 ~]$ ansible-playbook include.yml -i inventory.txt

Now Run the below command on client 

[ansible@ip-172-31-85-190 tmp]$ mkdir var
[ansible@ip-172-31-85-190 tmp]$ ls
18.txt  Gupta4000.txt  Raj400.txt  Raj4.txt  Raj.txt  var
[ansible@ip-172-31-85-190 tmp]$ cd var/
[ansible@ip-172-31-85-190 var]$ ls
var10.txt  var11.txt  var12.txt  var13.txt  var14.txt  var15.txt  var1.txt  var2.txt  var3.txt  var4.txt  var5.txt  var6.txt  var7.txt  var8.txt  var9.txt
[ansible@ip-172-31-85-190 var]$

-----------------------------------------------------------------------------------------

Now keep the task also in other file

[ansible@ip-172-31-80-19 ~]$ vi task.yml
[ansible@ip-172-31-80-19 ~]$ cat task.yml
    -
      name: 'task 1'
      command: touch /tmp/var/{{ var1 }}.txt
    -
      name: 'task 2'
      command: touch /tmp/var/{{ var2 }}.txt
    -
      name: 'task 3'
      command: touch /tmp/var/{{ var3 }}.txt
    -
      name: 'task 4'
      command: touch /tmp/var/{{ var4 }}.txt
    -
      name: 'task 5'
      command: touch /tmp/var/{{ var5 }}.txt
    -
      name: 'task 6'
      command: touch /tmp/var/{{ var6 }}.txt
    -
      name: 'task 7'
      command: touch /tmp/var/{{ var7 }}.txt
    -
      name: 'task 8'
      command: touch /tmp/var/{{ var8 }}.txt
    -
      name: 'task 9'
      command: touch /tmp/var/{{ var9 }}.txt
    -
      name: 'task 10'
      command: touch /tmp/var/{{ var10 }}.txt
    -
      name: 'task 11'
      command: touch /tmp/var/{{ var11 }}.txt
    -
      name: 'task 12'
      command: touch /tmp/var/{{ var12 }}.txt
    -
      name: 'task 13'
      command: touch /tmp/var/{{ var13 }}.txt
    -
      name: 'task 14'
      command: touch /tmp/var/{{ var14 }}.txt
    -
      name: 'task 15'
      command: touch /tmp/var/{{ var15 }}.txt

[ansible@ip-172-31-80-19 ~]$

Now include both file in below

[ansible@ip-172-31-80-19 ~]$ vi include.yml
[ansible@ip-172-31-80-19 ~]$ cat include.yml
-
  name: this is our first playbook.
  hosts: all
  vars_files:
    - variable.yml
  tasks:
    - include: task.yml
[ansible@ip-172-31-80-19 ~]$

[ansible@ip-172-31-80-19 ~]$ ansible-playbook include.yml -i inventory.txt

Now Run the below command on client 

[ansible@ip-172-31-85-190 tmp]$ mkdir var
[ansible@ip-172-31-85-190 tmp]$ ls
18.txt  Gupta4000.txt  Raj400.txt  Raj4.txt  Raj.txt  var
[ansible@ip-172-31-85-190 tmp]$ cd var/
[ansible@ip-172-31-85-190 var]$ ls
var10.txt  var11.txt  var12.txt  var13.txt  var14.txt  var15.txt  var1.txt  var2.txt  var3.txt  var4.txt  var5.txt  var6.txt  var7.txt  var8.txt  var9.txt
[ansible@ip-172-31-85-190 var]$

No comments:

Post a Comment