Ansible 8 of 9: Loops

1 of 17. Open Cloud 9

2 of 17. Provision the infrastructure

Don't forget to update the inventory file 'hosts'. Validate that you can connect to all the hosts.

cd ansible-tasks terraform apply -auto-approve aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key==`Name`]|[0].Value,PublicIP:PublicIpAddress,PrivateIP:PrivateIpAddress,State:State.Name}' \ --output table cat hosts ansible all -m ping

3 of 17. Create a new Ansible playbook: create-files-folders.yml

Populate the new file with content.

touch create-files-folders.yml - hosts: all tasks: - name: Creating Folder file: path: /home/{{ ansible_user }}/folder01 state: directory - name: Creating File file: path: /home/{{ ansible_user }}/folder01/file01 state: touch - name: Check if folder exist stat: path: /home/{{ ansible_user }}/folder01 register: register_folder - name: Check if file exist stat: path: /home/{{ ansible_user }}/folder01/file01 register: register_file - name: Report if folder exists debug: msg: "The directory exists" when: register_folder.stat.exists and register_folder.stat.isdir - name: Report if file exists debug: msg: "The file exists" when: register_file.stat.exists

4 of 17. Run the playbook.

The script uses "stat" to determine if stuff exists.

ansible-playbook create-files-folders.yml

5 of 17. Use 'with_items' to create folders.

Modify create-files-folders.yml to use 'with_items' to create folders

- hosts: all tasks: - name: Creating Folder file: path: /home/{{ ansible_user }}/{{ item }} state: directory with_items: - folder01 - folder02 - folder03 - name: Check if folder exist stat: path: /home/{{ ansible_user }}/folder01 register: register_folder - name: Check if file exist stat: path: /home/{{ ansible_user }}/folder01/file01 register: register_file - name: Report if folder exists debug: msg: "The directory exists" when: register_folder.stat.exists and register_folder.stat.isdir - name: Report if file exists debug: msg: "The file exists" when: register_file.stat.exists

6 of 17. Run the playbook"

Validate the additional folders are created.

ansible-playbook create-files-folders.yml

7 of 17. Modify the playbook to remove the folders.

State should change to "absent"

- hosts: all tasks: - name: Creating Folder file: path: /home/{{ ansible_user }}/{{ item }} state: absent with_items: - folder01 - folder02 - folder03 - name: Check if folder exist stat: path: /home/{{ ansible_user }}/folder01 register: register_folder - name: Check if file exist stat: path: /home/{{ ansible_user }}/folder01/file01 register: register_file - name: Report if folder exists debug: msg: "The directory exists" when: register_folder.stat.exists and register_folder.stat.isdir - name: Report if file exists debug: msg: "The file exists" when: register_file.stat.exists

08 of 17. Run the playbook to remove the folders

Validate the folders are gone.

ansible-playbook create-files-folders.yml

09 of 17. Modify the playbook to use the loop directive to create folders

Note: Beyond the syntax difference, loop only works on flattened structures. E.g.: If one entry in the list contains sub-items, that entry would have to be "flattened" to use loop.

- hosts: all tasks: - name: Creating Folder file: path: /home/{{ ansible_user }}/{{ item }} state: directory loop: - folder01 - folder02 - folder03 - name: Check if folder exist stat: path: /home/{{ ansible_user }}/folder01 register: register_folder - name: Report if folder exists debug: msg: "The directory exists" when: register_folder.stat.exists and register_folder.stat.isdir

10 of 17. Run the playbook to recreate those folders

Validate the folders are created.

ansible-playbook create-files-folders.yml

11 of 17. One more time, modify the playbook to remove the folders.

State should change to "absent"

- hosts: all tasks: - name: Creating Folder file: path: /home/{{ ansible_user }}/{{ item }} state: absent loop: - folder01 - folder02 - folder03 - name: Check if folder exist stat: path: /home/{{ ansible_user }}/folder01 register: register_folder - name: Report if folder exists debug: msg: "The directory exists" when: register_folder.stat.exists and register_folder.stat.isdir

12 of 17. Run the playbook again to remove the folders

Validate the folders are gone.

ansible-playbook create-files-folders.yml

13 of 17. Modify the playbook to create a file inside each folder.

This will be done using a loop, also.

- hosts: all tasks: - name: Creating Folder file: path: /home/{{ ansible_user }}/{{ item }} state: directory loop: - folder01 - folder02 - folder03 - name: Creating files file: path: /home/{{ ansible_user }}/{{ item.dir }}/{{ item.file }} state: touch with_items: - { dir: "folder01", file: "file01"} - { dir: "folder02", file: "file02"} - { dir: "folder03", file: "file03"} - name: Check if folder exist stat: path: /home/{{ ansible_user }}/folder01 register: register_folder - name: Check if file exist stat: path: /home/{{ ansible_user }}/folder01/file01 register: register_file - name: Report if folder exists debug: msg: "The directory exists" when: register_folder.stat.exists and register_folder.stat.isdir - name: Report if file exists debug: msg: "The file exists" when: register_file.stat.exists

14 of 17. Run the playbook again to add folders and files

Validate the folders and files are present.

ansible-playbook create-files-folders.yml

15 of 17. Modify the playbook to remove all created.

The state should change to "absent"

- hosts: all tasks: - name: Creating Folder file: path: /home/{{ ansible_user }}/{{ item }} state: absent loop: - folder01 - folder02 - folder03 - name: Creating files file: path: /home/{{ ansible_user }}/{{ item.dir }}/{{ item.file }} state: absent with_items: - { dir: "folder01", file: "file01"} - { dir: "folder02", file: "file02"} - { dir: "folder03", file: "file03"} - name: Check if folder exist stat: path: /home/{{ ansible_user }}/folder01 register: register_folder - name: Check if file exist stat: path: /home/{{ ansible_user }}/folder01/file01 register: register_file - name: Report if folder exists debug: msg: "The directory exists" when: register_folder.stat.exists and register_folder.stat.isdir - name: Report if file exists debug: msg: "The file exists" when: register_file.stat.exists

16 of 17. Run the playbook to remove the items

ansible-playbook create-files-folders.yml

17 of 17. Cleanup

terraform destroy -auto-approve aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key==`Name`]|[0].Value,PublicIP:PublicIpAddress,PrivateIP:PrivateIpAddress,State:State.Name}' \ --output table

References

ansible.builtin.file module – Manage files and file properties - Ansible Documentation

Ansible playbooks - Ansible documentation

Loops - Ansible Documentation

ansible.builtin.stat module – Retrieve file or file system status - Ansible Documentation

ansible.builtin.debug module – Print statements during execution - Ansible Documentation

Command: init | Terraform | HashiCorp Developer

Command: plan | Terraform | HashiCorp Developer

Command: apply | Terraform | HashiCorp Developer

Command: destroy | Terraform | HashiCorp Developer

Comments

Popular posts from this blog

Containing the Chaos! | A Three-Part Series Demonstrating the Usefulness of Containerization to HumanGov

Ansible is the Answer! | A Three-Part Series Demonstrating the Usefulness of Ansible to HumanGov