Refresh your planetexpress
hosts as well as your .ansible.cfg and inventory files.
student@bchd:~$
bash ~/px/scripts/full-setup.sh
PART 1- Take the playbook below and use the nineties
variable with your file
task. Create all the directories using a for loop. Only one task is allowed!
SUPER BONUS- Can you make all the directories lowercase?
- name: making dirs
hosts: bender
connection: ssh
gather_facts: no
vars:
nineties:
- Teenage
- Mutant
- Ninja
- Turtles
tasks:
- name: Making directories from a list
file:
path: # add the item keyword!
state: directory
# add the loop keyword!
Hint 1: Adding the `loop` keyword
- name: Making directories from a list
file:
path: # add the item keyword!
state: directory
loop: "{{ nineties }}"
Hint 2: Returning `Teenage`,`Mutant`,`Ninja`, and `Turtles`
When using a loop, each item returned is represented by the variable {{ item }}
!
- name: Making directories from a list
file:
path: "{{ item }}"
state: directory
loop: "{{ nineties }}"
Hint 3: Making directory names lowercase
This is a topic we'll explore in greater detail later-- but Jinja2 has what are known as filters that can change objects. In this case, the |lower filter will do the job!
- name: Making directories from a list
file:
path: "{{ item | lower }}"
state: directory
loop: "{{ nineties }}"
PART 2- Take the playbook below and edit the msg
parameter so that it takes the name
and groups
value from each dictionary to complete the sentence.
- name: looping dictionaries
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Loop across complex data structures
debug:
msg: # complete this line. Make it read "A _____ is my favorite ____!"
loop:
- { name: 'banana', groups: 'fruit' }
- { name: 'rousing game of Monopoly', groups: 'pastime' }
- { name: 'cookie', groups: 'snack' }
- { name: 'carrot', groups: 'vegetable' }
Hint 1: Using Dot Notation
Our loop is going over a sequence (aka array/list) of mappings (aka object/dictionary). From each mapping in that list, we want to return the value of name
and the value of groups
.
Here's a breakdown of how item
would be sliced using dot notation.
item.name = banana
item.groups = fruit
item.name = rousing game of Monopoly
item.groups = pastime
item.name = cookie
item.groups = snack
item.name = carrot
item.groups = vegetable
Hint 2: Using Dot Notation with our Loop
- name: Loop across complex data structures
debug:
msg: "A {{ item.name }} is my favorite {{ item.groups }}!"
loop:
- { name: 'banana', groups: 'fruit' }
- { name: 'rousing game of Monopoly', groups: 'pastime' }
- { name: 'cookie', groups: 'snack' }
- { name: 'carrot', groups: 'vegetable' }