0% found this document useful (0 votes)
56 views6 pages

4-Deploy Files To Managed Hosts

The document provides a series of Ansible playbooks for managing files and directories on remote Linux hosts. It includes instructions for copying files, changing file permissions, checking file status, creating and removing files, and adding text to files. Each section contains example code snippets and descriptions of the tasks performed by the playbooks.

Uploaded by

drakolaksa122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views6 pages

4-Deploy Files To Managed Hosts

The document provides a series of Ansible playbooks for managing files and directories on remote Linux hosts. It includes instructions for copying files, changing file permissions, checking file status, creating and removing files, and adding text to files. Each section contains example code snippets and descriptions of the tasks performed by the playbooks.

Uploaded by

drakolaksa122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Linux RedHat Certified Engineer

(RHCE - EX294)
Deploy Files to Managed Hosts
Copy Files to Remote Clients
# echo somestuff > /home/iafzal/some.cfg
# vim copy.yml

---
- name: Copy file from local to remote clients Description of the playbook
hosts: all Run it on all hosts
tasks: Run the following task(s)
- name: Copying file Description of the task
become: true Transfer as a current user
copy: Run copy module
src: /home/iafzal/some.cfg
dest: /tmp
Source of the file
owner: iafzal
group: iafzal
Destination of the file
mode: 0644
Change ownership and file permissions

By: Imran Afzal


www.utclisolutions.com
Change File Permissions
# Login to LinuxClient1
# touch /home/iafzal/linux2

# Login to ControlNode
# vim filepermission.yml

---
- name: Change file permissions
hosts: all

tasks:
- name: Files Permissions
file:
path: /home/iafzal/linux2 File location
mode: a+w Permissions

Run the playbook


# anisble-playbook filepermission.yml

• Ansible modules and options


https://docs.ansible.com/ansible/2.5/modules/ By: Imran Afzal
www.utclisolutions.com
Check File or Directory Status
# Login to ControlNode
# vim checkFS.yml

---
- name: File status module
hosts: localhost

tasks:
- name: Check file status and attributes
stat: stat module
path: /etc/hosts File path/location
register: fs Register or output to fs container
- name: Show result
debug: debug module to print
msg: File attributes {{ fs }} Print what is inside of fs container

Run the playbook


# anisble-playbook checkFS.yml

By: Imran Afzal


www.utclisolutions.com
Create Dir/File and Remove File
# Login to ControlNode
# vim crfile.yml
Run the playbook
--- # anisble-playbook crfile.yml
- name: Create and Remove file
hosts: all
tasks:
- name: Create a directory
file: file module
path: /tmp/seinfeld Location
owner: iafzal
group: iafzal
mode: 770
state: directory Create a directory
- name: Create a file in that directory
file:
path: /tmp/seinfeld/jerry Create a file
state: touch
- name: Stat the new file jerry
stat:
path: /tmp/seinfeld/jerry
register: jf Put the result in jf container
- name: Show file status
debug:
msg: File status and attributes {{ jf }} Print container or variable contents
- name: Remove file
file:
path: /tmp/seinfeld/jerry
state: absent Delete the file By: Imran Afzal
www.utclisolutions.com
Create a File and Add Text
# Login to ControlNode
# vim addtext.yml

---
- name: Create a file and add text
hosts: localhost

tasks:
- name: Create a new file
file: file module
path: /tmp/george File path/location
state: touch Create a file
- name: Add text to the file
blockinfile: blockinfile module
path: /tmp/george
block: George is one of the main Add text to the file
character of Seinfeld show and he is Jerry’s
best friend

Run the playbook


# anisble-playbook addtext.yml
By: Imran Afzal
www.utclisolutions.com

You might also like