Tuesday, April 16, 2019

Ansible Playbook: Generate multiple files at specific time interval

In the previous post, we have seen how to generate multiple files using with_sequence and loop features.

Here, we will introduce the delay functionality in the loop feature. This will give a pause after every iteration. So, each file will be created at specific time interval. Let's say, we will create 20 files at 10sec time interval.
Download the yml file [generateFiles_pattern_withDelay.yml]

--- 
- hosts: localhost
  gather_facts: no
  become: no
  tasks:
    - name: create 20 empty file in each 10 sec
      file:
        path: "/tmp/playing_opera/loopdemo/file{{ item }}"
        state: touch
      with_sequence: start=1 end=20
      loop_control:
        pause: 10
...



Next: ..?? Not yet decided :)





Ansible Playbook: Generating multiple files with loop and with_sequence

Let's write a playbook which would create multiple empty files. All the filenames must follow a pattern. Lets say file1, file2, file3, ...

For this we will use with_sequence and loop features. with_sequence would allow us to generate a list of numbers and loop feature would allow us to iterate of the lists.

Generate empty file from a list

Job is to create a list of filenames and use that list to create multiple empty file.
Download yml file [generateFiles1.yml]
--- 
- hosts: localhost
  gather_facts: no
  become: no
  tasks:
    - name: create the location/directory
      file:
        path: /tmp/playing_opera/loopdemo
        state: directory

    - name: generate empty file from a list of file names
      file:
        path: "/tmp/playing_opera/loopdemo/{{ item }}"
        state: touch
      with_items:
        - fruitsList
        - censusReport
        - countryList
…

The first task would first make sure that the directory is present. If not this would create one.
The second task is to create three files with name "fruitsList", "censusReport", and "countryList".

The above playbook can also written in following way.
Download yml file [generateFiles2.yml]

--- 
- hosts: localhost
  gather_facts: no
  become: no
  tasks:
    - name: create the location/directory
      file:
        path: /tmp/playing_opera/loopdemo
        state: directory

    - name: generate empty file from a list of file names
      file:
        path: "/tmp/playing_opera/loopdemo/{{ item }}"
        state: touch
      loop: [ fruitsList, censusReport, countryList ]
…


Generate empty files with pattern

It is sometimes necessary to create a list of files following some patterns. For example file1, file2, file3, ...
This can be achieved using with_sequence feature of ansible.
Following is an example.

Download yml file [generateFiles_pattern.yml]

--- 
- hosts: localhost
  gather_facts: no
  become: no
  tasks:
    - name: generate 10 empty files
      file:
        path: "/tmp/playing_opera/loopdemo/file{{ item }}"
        state: touch
      with_sequence: start=1 end=10
…

This playbook would create 10 files with the names file1, file2, ..., file10.
Explorer the official documentation for more info.









Manually adding ansible_host inventory file, if NOT present.

In computing, Ansible is an open-source software provisioning, configuration management, and application deployment tool. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. 

If you are installing ansible using pip or any other manual process, first check if "ansible" directory is present inside "/etc" 

if not, here is the steps you can follow to add ansible_host inventory file. This inventory file mainly keeps the inventory of detail information regarding all the host machines. 

Steps: 

1>  Open Terminal in the linux system and enter following commands
2>  cd /etc
3>  mkdir -p ansible/host/
4>  cd ansible/host/
5>  vim ansible_host
6> Add host details in following format
 [host group name]
<host IP> [ansible_port=<port number>] [ansible_user=<username>] [ansible_ssh_pass=<password>]
e.g.
[localvms]
172.17.12.65 ansible_user=guestuser1 ansible_ssh_pass=123
172.17.12.80 ansible_port=808 ansible_user=
guestuser1 ansible_ssh_pass=123

Click here for more information from official source.


Ansible Playbook: ping remote server(s)

ping command is basically used to test the reachability of a server. Ansible allows you to ping multiple servers or a group of servers at once. It is assumed that, Ansible tool is already installed in your linux system.

Steps:
1>   Create empty yml file with the name pingserver.yml

vim pingserver.yml

2>   Add following contents:

---
- hosts: myservers
  tasks:
   - name: check the ping
     ping:
...

3>   Save and execute the pingserver.yml file using followng command
ansible-playbook pingserver.yml


make sure following content is present in ansible_host inventory file
[myservers]
192.168.15.34 ansible_user=chinmaya ansible_ssh_pass=mypassword
www.chinmayadehury.in ansible_user=chinmaya ansible_ssh_pass=mypassword
192.168.15.37 ansible_user=chinmaya ansible_ssh_pass=mypassword  

[Alternative]
In this method no playbook file is needed to create.
Pass the following command to the terminal

ansible myservers -m ping


If ansible_host file is not present, click here for the steps to add ansible_host file.
-Thanks