Friday, 9 August 2019

Ansible Script Modules by Raj Gupta

Script Modules :- This are used to run the Script

[ansible@ip-172-31-95-179 ~]$ cat testScript.sh
#!/bin/bash
pwd >> /tmp/fileCreatedByScript.txt

[ansible@ip-172-31-95-179 ~]$ vi script.yml
[ansible@ip-172-31-95-179 ~]$ cat script.yml
-
  name: "this is our first playbook."
  hosts: all
  tasks:
    -
      name: "create a dummy file on websever1"
      script: testScript.sh
[ansible@ip-172-31-95-179 ~]$ ansible-playbook script.yml -i inventory.txt

PLAY [this is our first playbook.] *************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [172.31.84.42]

TASK [create a dummy file on websever1] ********************************************************************************************************************************
changed: [172.31.84.42]

PLAY RECAP *************************************************************************************************************************************************************
172.31.84.42               : ok=2    changed=1    unreachable=0    failed=0

[ansible@ip-172-31-95-179 ~]$

Now run the below command on client

[ansible@ip-172-31-84-42 ~]$ cd /tmp
[ansible@ip-172-31-84-42 tmp]$ ls
fileCreatedByScript.txt  test.txt  test.yml
[ansible@ip-172-31-84-42 tmp]$ cat fileCreatedByScript.txt
/home/ansible

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

The use of creates parameter is that if /tmp/test6.txt will be available on client machine then it will not going to run script modules ....it will run only if value of creates parameter is not present on client .

[ansible@ip-172-31-95-179 ~]$ vi script1.yml
[ansible@ip-172-31-95-179 ~]$ cat script1.yml
-
  name: "this is our first playbook."
  hosts: all
  tasks:
    -
      name: "create a dummy file on websever1"
      script: testScript.sh creates=/tmp/test6.txt
[ansible@ip-172-31-95-179 ~]$ ansible-playbook script1.yml -i inventory.txt

PLAY [this is our first playbook.] *************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [172.31.84.42]

TASK [create a dummy file on websever1] ********************************************************************************************************************************
changed: [172.31.84.42]

PLAY RECAP *************************************************************************************************************************************************************
172.31.84.42               : ok=2    changed=1    unreachable=0    failed=0

[ansible@ip-172-31-95-179 ~]$

In our case script run because test6.txt is not there on client.

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

removes parameter is opposite to above creates parameter 

[ansible@ip-172-31-95-179 ~]$ vi script2.yml
[ansible@ip-172-31-95-179 ~]$ cat script2.yml
-
  name: "this is our first playbook."
  hosts: all
  tasks:
    -
      name: "create a dummy file on websever1"
      script: testScript.sh removes=/tmp/test6.txt chdir=/home
[ansible@ip-172-31-95-179 ~]$ ansible-playbook script2.yml -i inventory.txt

PLAY [this is our first playbook.] *************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [172.31.84.42]

TASK [create a dummy file on websever1] ********************************************************************************************************************************
skipping: [172.31.84.42]

PLAY RECAP *************************************************************************************************************************************************************
172.31.84.42               : ok=1    changed=0    unreachable=0    failed=0

[ansible@ip-172-31-95-179 ~]$

chdir is used to change the directory in my case it will run under home directory 

No comments:

Post a Comment