How do i copy files, create files & change files in ansible?



  • Link: http://docs.ansible.com/ansible/file_module.html
    Link: http://docs.ansible.com/ansible/copy_module.html

    Ansible has two modules called copy & file, which are just part of a collection of File Modules that are available. The copy module can transfer files and directories, and the file module can change file attributes like permissions, owner, and group

    copy module

    Transfer a file, notice the double quotes, quotes are important, similar to running the old rsh type commands.

    $ ansible perth -m copy -a "src=/etc/hosts dest=/tmp/hosts mode=600"
    ip-xxxx.us-west-2.compute.internal | SUCCESS => {
        "changed": true, 
        "checksum": "a983aaa274e450705ec89584822af919e93741ef", 
        "dest": "/tmp/hosts", 
        "gid": 1001, 
        "group": "awsgrp", 
        "mode": "0600", 
        "owner": "awsuser", 
        "path": "/tmp/hosts", 
        "secontext": "unconfined_u:object_r:user_home_t:s0", 
        "size": 159, 
        "state": "file", 
        "uid": 1001
    }
    

    file module

    Change permission, owner and group on file, notice the double quotes

    $ ansible perth -b -m file -a "dest=/tmp/hosts mode=600 owner=adm group=adm"
    ip-xxxx.us-west-2.compute.internal | SUCCESS => {
        "changed": true, 
        "gid": 4, 
        "group": "adm", 
        "mode": "0600", 
        "owner": "adm", 
        "path": "/tmp/hosts", 
        "secontext": "unconfined_u:object_r:user_home_t:s0", 
        "size": 159, 
        "state": "file", 
        "uid": 3
    }
    

    Create a directory structure and change permissions, owner and group

    $ ansible perth -b  -m file -a "dest=/tmp/web/files mode=755 owner=adm group=adm state=directory"
    

    Delete a directory or file

    $ ansible perth -b -m file -a "dest=/tmp/web/files state=absent"
    

Log in to reply
 

© Lightnetics 2024