Friday, April 2, 2021

Managing Files using Ansible

Description: Here I have explained, How to create a blank file? How to copy files to a remote PC? How to add one and multiple lines in the file?

How to Create a blank file?
Below is the YAML file to create a blank file in a remote machine

##Create blank file with Test_Ansible.txt --- - hosts: web tasks: - name: Create blank file file: path: "/root/Test_Ansible.txt" state: touch



How to Copy a file to a remote machine from a local machine?
Below is the YAML file to copy the file to remote machine from local machine|
--- - hosts: web tasks: - name: Copy from Local to Remote Target Machine with 'copy' copy: src: Sources.list dest: /root/Sources.list



How to Copy a file to a remote machine from a remote location?
Below is the YAML file to copy the file to remote machine path from remote machine location
--- - hosts: web tasks: - name: Copy file from one directory to other on the Remote Machine copy: src: /etc/passwd dest: /tmp/passwd remote_src: yes


How to Add one line file using Ansible?
Below is the YAML file to add one line using Ansible
--- - hosts: web tasks: - name: line insert lineinfile: path: /root/Test_Ansible.txt line: 'Add New Line 1' insertbefore: BOF










How to Add Multiple Lines in an existing file using Ansible?
Below is the YAML file to add multiple lines using Ansible 
--- - hosts: web tasks: - name: Add lines in fie lineinfile: path: /root/Test_Ansible.txt state: present line: "{{ item }}" with_items: - 'This is Line 2' - 'This is Line 3'

How to Remove Line from the existing file using Ansible?
Below is the YAML file to remove a line from Ansible
--- - hosts: web tasks: - name: Remove Line lineinfile: path: /root/Test_Ansible.txt state: absent line: 'This is Line 2'

No comments:

Post a Comment