Managing files in ansible

[mshaik@workstation ~]$ ansible localhost –list-hosts

hosts (1):
localhost
——————————————-
[mshaik@workstation ~]$ vim file.yaml

– name: creating a file
hosts: localhost
tasks:
– file:
path: /home/mshaik/sample
state: touch
owner: mshaik
group: mshaik
mode: 0755

——————————————-
[mshaik@workstation ~]$ ansible-playbook –syntax-check file.yaml

playbook: file.yaml

—————————————-
[mshaik@workstation ~]$ ansible-playbook -C file.yaml

PLAY [creating a file] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [file] ********************************************************************
changed: [localhost]

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

——————————————-
[mshaik@workstation ~]$ stat /home/mshaik/sample
stat: cannot stat ‘/home/mshaik/sample’: No such file or directory
——————————————-
[mshaik@workstation ~]$ ansible-playbook file.yaml

PLAY [creating a file] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [file] ********************************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
——————————————-
[mshaik@workstation ~]$ stat /home/mshaik/sample
File: ‘/home/mshaik/sample’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 51300626 Links: 1
Access: (0333/–wx-wx-wx) Uid: ( 1000/ mshaik) Gid: ( 1000/ mshaik)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2017-01-26 06:39:51.607557462 +0530
Modify: 2017-01-26 06:39:51.607557462 +0530
Change: 2017-01-26 06:39:51.608557462 +0530
Birth: –
——————————————-
[mshaik@workstation ~]$ ls -l /home/mshaik/sample
–wx-wx-wx. 1 mshaik mshaik 0 Jan 26 06:39 /home/mshaik/sample
——————————————-
[mshaik@workstation ~]$ ansible-playbook file.yaml

PLAY [creating a file] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [file] ********************************************************************
changed: [localhost]

TASK [stat] ********************************************************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
——————————————–
[mshaik@workstation ~]$ vim file.yaml

– name: creating a file
hosts: localhost
tasks:
– file:
path: /home/mshaik/sample
state: touch
owner: mshaik
group: mshaik
mode: 0755
– stat: path=/home/mshaik/sample
register: file_status
– debug: msg=”File exists”
when: file_status.stat.exists == true
——————————————–
[mshaik@workstation ~]$ ansible-playbook file.yaml

PLAY [creating a file] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [file] ********************************************************************
changed: [localhost]

TASK [stat] ********************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
“msg”: “File exists”
}

PLAY RECAP *********************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0

——————————————–
[mshaik@workstation ~]$ vim file.yaml

– name: creating a file
hosts: localhost
tasks:
– file:
path: /home/mshaik/sample
state: touch
owner: mshaik
group: mshaik
mode: 0755
– stat: path=/home/mshaik/sample
register: file_status
– debug: msg=”File exists”
when: file_status.stat.exists == true
– copy: content=”this is for test purpose\n” dest=”/home/mshaik/sample”
when: file_status.stat.exists == true

 
[mshaik@workstation ~]$ ansible-playbook -C file.yaml

PLAY [creating a file] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [file] ********************************************************************
changed: [localhost]

TASK [stat] ********************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
“msg”: “File exists”
}

TASK [copy] ********************************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=5 changed=2 unreachable=0 failed=0

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s