April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Managing files in ansible

Managing files in ansible

[root@controller ~]$ ansible localhost –list-hosts

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

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

——————————————-
[root@controller ~]$ ansible-playbook –syntax-check file.yaml

playbook: file.yaml

—————————————-
[root@controller ~]$ 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

——————————————-
[root@controller ~]$ stat /home/root/sample
stat: cannot stat ‘/home/root/sample’: No such file or directory
——————————————-
[root@controller ~]$ 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
——————————————-
[root@controller ~]$ stat /home/root/sample
File: ‘/home/root/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/ root) Gid: ( 1000/ root)
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: –
——————————————-
[root@controller ~]$ ls -l /home/root/sample
–wx-wx-wx. 1 root root 0 Jan 26 06:39 /home/root/sample
——————————————-
[root@controller ~]$ 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
——————————————–
[root@controller ~]$ vim file.yaml

– name: creating a file
hosts: localhost
tasks:
– file:
path: /home/root/sample
state: touch
owner: root
group: root
mode: 0755
– stat: path=/home/root/sample
register: file_status
– debug: msg=”File exists”
when: file_status.stat.exists == true
——————————————–
[root@controller ~]$ 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

——————————————–
[root@controller ~]$ vim file.yaml

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

[root@controller ~]$ 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

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>