Search This Blog

Monday, 1 March 2021

Ansible playbook for creating a directory

 Step 1) Create a sample file on ansible master server.

[osboxes@master ansible-playbooks]$ touch /tmp/sample.txt

Step 2) Create a playbook

---
- name: create a directory and copy a file to remote hosts
  hosts: all
  become: yes
  become_user: root
  vars:
    remote_dir: /tmp/example
    sample_file: /tmp/sample.txt

  tasks:
    - name: Create a directory on remote host
      file:
        path: "{{ remote_dir }}"
        type: directory
        recurse: true

    - name: copy a file from ansible master to remote directory
      copy:
        src: "{{ sample_file }}"
        dest: "{{ remote_dir }}"

Step 3) Check the syntax

[osboxes@master ansible-playbooks]$ ansible-playbook create_dir.yml -i inventory.txt --syntax-check
playbook: create_dir.yml

Step 4) Run the playbook

[osboxes@master ansible-playbooks]$ ansible-playbook  create_dir.yml -i inventory.txt -K
BECOME password:

PLAY [create a directory and copy a file to remote hosts] ***************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [linuxhost]

TASK [Create a directory on remote host] ********************************************************************************************************************
changed: [linuxhost]

TASK [copy a file from ansible master to remote directory] **************************************************************************************************
changed: [linuxhost]

PLAY RECAP **************************************************************************************************************************************************
linuxhost                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


Step 5) Verify if the directory is created and file has been copied on the remote host

[osboxes@linuxhost tmp]$ ls -l /tmp/example/
total 0
-rw-r--r--. 1 root root 0 Mar  1 05:15 sample.txt

1 comment:

Anonymous said...

Hi Pavan,

Really Appreciate your efforts for creating such nice blog on weblogic.

Can you please share topics related to Oracle forms and reports step by step upgradation from 11g R2 to 12c.

Regards,
Nitin