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.
Download yml file [generateFiles1.yml]
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]
This can be achieved using with_sequence feature of ansible.
Following is an example.
Download yml file [generateFiles_pattern.yml]
This playbook would create 10 files with the names file1, file2, ..., file10.
Explorer the official documentation for more info.
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.
No comments:
Post a Comment