[ansible@ip-172-31-80-19 ~]$ vi conditionl.yml
[ansible@ip-172-31-80-19 ~]$ cat conditionl.yml
-
name: this is our first playbook.
hosts: all
vars:
age: 18
tasks:
-
name: 'creating file using variable'
command: touch /tmp/18.txt
when: age == 18
[ansible@ip-172-31-80-19 ~]$ ansible-playbook conditionl.yml -i inventory.txt
PLAY [this is our first playbook.] *************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [172.31.85.190]
TASK [creating file using variable] ************************************************************************************************************************************
[WARNING]: Consider using the file module with state=touch rather than running touch. If you need to use command because file is insufficient you can add warn=False
to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
changed: [172.31.85.190]
PLAY RECAP *************************************************************************************************************************************************************
172.31.85.190 : ok=2 changed=1 unreachable=0 failed=0
[ansible@ip-172-31-80-19 ~]$
Now if you run the below command on client 
[ansible@ip-172-31-85-190 ~]$ cd /tmp
[ansible@ip-172-31-85-190 tmp]$ ls
18.txt
[ansible@ip-172-31-85-190 tmp]$
If we change the value of age other then 18 it will not create any file on client.
------------------------------------------------------------------------------------------------
[ansible@ip-172-31-80-19 ~]$ vi conditionl.yml
[ansible@ip-172-31-80-19 ~]$ cat conditionl.yml
-
  name: this is our first playbook.
  hosts: all
  vars:
    age: 19
  tasks:
    -
      name: 'creating file using variable'
      command: touch /tmp/Gupta.txt
      when: age == 18
    -
      name: 'creating file using variable'
      command: touch /tmp/Raj.txt
      when: age > 18
[ansible@ip-172-31-80-19 ~]$ ansible-playbook conditionl.yml -i inventory.txt
PLAY [this is our first playbook.] *************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [172.31.85.190]
TASK [creating file using variable] ************************************************************************************************************************************
skipping: [172.31.85.190]
TASK [creating file using variable] ************************************************************************************************************************************
 [WARNING]: Consider using the file module with state=touch rather than running touch.  If you need to use command because file is insufficient you can add warn=False
to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
changed: [172.31.85.190]
PLAY RECAP *************************************************************************************************************************************************************
172.31.85.190              : ok=2    changed=1    unreachable=0    failed=0
[ansible@ip-172-31-80-19 ~]$
Now Run the below command on client 
[ansible@ip-172-31-85-190 tmp]$ ls
Raj.txt
[ansible@ip-172-31-85-190 tmp]$
----------------------------------------------------------------------------------------------------------
[ansible@ip-172-31-80-19 ~]$ vi conditionl.yml
[ansible@ip-172-31-80-19 ~]$ cat conditionl.yml
-
  name: this is our first playbook.
  hosts: all
  vars:
    age: 15
  tasks:
    -
      name: 'creating file using variable'
      command: touch /tmp/Gupta4.txt
      when: age == 18
    -
      name: 'creating file using variable'
      command: touch /tmp/Raj4.txt
      when: age > 10 and age < 18
[ansible@ip-172-31-80-19 ~]$ ansible-playbook conditionl.yml -i inventory.txt
PLAY [this is our first playbook.] *************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [172.31.85.190]
TASK [creating file using variable] ************************************************************************************************************************************
skipping: [172.31.85.190]
TASK [creating file using variable] ************************************************************************************************************************************
 [WARNING]: Consider using the file module with state=touch rather than running touch.  If you need to use command because file is insufficient you can add warn=False
to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
changed: [172.31.85.190]
PLAY RECAP *************************************************************************************************************************************************************
172.31.85.190              : ok=2    changed=1    unreachable=0    failed=0
[ansible@ip-172-31-80-19 ~]$
Now run the below command on client 
[ansible@ip-172-31-85-190 ~]$ cd /tmp
[ansible@ip-172-31-85-190 tmp]$ ls
Raj4.txt
[ansible@ip-172-31-85-190 tmp]$
------------------------------------------------------------------------------------------------------------------
[ansible@ip-172-31-80-19 ~]$ vi conditionl.yml
[ansible@ip-172-31-80-19 ~]$ cat conditionl.yml
-
  name: this is our first playbook.
  hosts: all
  vars:
    age: 11
  tasks:
    -
      name: 'creating file using variable'
      command: touch /tmp/Gupta400.txt
      when: age == 18
    -
      name: 'creating file using variable'
      command: touch /tmp/Gupta40.txt
      when: age > 18
    -
      name: 'creating file using variable'
      command: touch /tmp/Gupta40000.txt
      when: age < 10
    -
      name: 'creating file using variable'
      command: touch /tmp/Gupta4000.txt
      when: age == 10 or age == 11
    -
      name: 'creating file using variable'
      command: touch /tmp/Raj400.txt
      when: age > 10 and age < 18
[ansible@ip-172-31-80-19 ~]$ ansible-playbook conditionl.yml -i inventory.txt
PLAY [this is our first playbook.] *************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [172.31.85.190]
TASK [creating file using variable] ************************************************************************************************************************************
skipping: [172.31.85.190]
TASK [creating file using variable] ************************************************************************************************************************************
skipping: [172.31.85.190]
TASK [creating file using variable] ************************************************************************************************************************************
skipping: [172.31.85.190]
TASK [creating file using variable] ************************************************************************************************************************************
 [WARNING]: Consider using the file module with state=touch rather than running touch.  If you need to use command because file is insufficient you can add warn=False
to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
changed: [172.31.85.190]
TASK [creating file using variable] ************************************************************************************************************************************
changed: [172.31.85.190]
Now Run the below command on client
[ansible@ip-172-31-85-190 tmp]$ ls
Gupta4000.txt  Raj400.txt
[ansible@ip-172-31-85-190 tmp]$
 
 
No comments:
Post a Comment