cancel
Showing results for 
Search instead for 
Did you mean: 
uconn-alf02013
Mission Specialist
Mission Specialist
  • 276 Views

Role structure question

Jump to solution

Hi, i am doing RH294 chapter 7 and the guided exercise of section 7.4

In section 7.3 they clearly show a role "motd" with a "files" folder inside it.

In the guided exercise they manually create the "files" folder at the "project" level and not in the "roles" level of the directory structure.

I missed that little detail, and created my "files" folder in a place the ansible could not find when running the playbook.

Does anyone know what i was supposed to be thinking so the task has this line

src:  files/html/

and not

src: /home/student/role-create/roles/myvhost/files/html/

in order to get the play to run without crashing ?
I spent may hours looking at code and watching the video solution before i noticed instructor create the files/html/  folder OUTSIDE of the role/myvhost  folder !
Thank you.

-Angelo

Labels (2)
1 Solution

Accepted Solutions
Travis
Moderator
Moderator
  • 115 Views

@uconn-alf02013 -

I'm not 100% sure I fully understand your question, but I will try to explain the answer based on what I think you are asking.

It would appear from the question

Does anyone know what i was supposed to be thinking so the task has this line

src:  files/html/

and not

src: /home/student/role-create/roles/myvhost/files/html/

that you are concerned about the path and how it finds the files. So, one thing to consider is that in terms of a playbook and if you are running directly both should work in the real world. However, for ROLES and what you are going for in the real world is the ability to share and reuse. The files/html is a realative path for the Ansible role you are creating and when you create a role, the tasks/main.yml, knows where things are the handlers/main.yml and other items in the role know where things can be found. Likewise, playbooks using those roles know where things can be found all based on relative paths. Having the src specified with an absolute path, when that role is used somewhere else, the file and path would not be in your role.

Might be extra confusing is the fact that we use the "roles" directive instead of tasks and it does its thing and you aren't necessarily seeing that the tasks and variables and files and components that get imported in the role are available.

Things can get even more complex when you look at roles and collections. I've attached a couple PDFs of when I have taught the RH294 before that give an Ansible overview and introduction as well as give AAP overviews with the newer collections. Where things really get wild is where you can use an import_playbooks from a collection.

Feel free to check out these resources that I usually provide to my students.

Useful Github Repositories


Useful Github Repositories for VS Code


VS Code Marketplace Extensions

 

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training

View solution in original post

5 Replies
Chetan_Tiwary_
Community Manager
Community Manager
  • 243 Views

@uconn-alf02013  Please check the below screenshots where I used the roles/myvhost/files/html folder for the index.html:

 

Chetan_Tiwary__2-1737744873918.png

 

 

Chetan_Tiwary__1-1737744745698.png

 

0 Kudos
uconn-alf02013
Mission Specialist
Mission Specialist
  • 120 Views

Hi, let me propose my question a different way.
why must i create the folder "files/html" with a path of

/home/student/role-create/files/html

and i can not do it in the normal place the "files" folder appears
when you run a command like

ansible-galaxy role init myvhost
from the path /home/student/role-create/roles/


Is this some special setup for the lab environment and in real world this works fine ?
roles/
└── motd
├── defaults
│ └── main.yml
├── files
├── handlers
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
└── templates
└── motd.j2

 

Thanks for trying to explain it.

-Angelo

Travis
Moderator
Moderator
  • 116 Views

@uconn-alf02013 -

I'm not 100% sure I fully understand your question, but I will try to explain the answer based on what I think you are asking.

It would appear from the question

Does anyone know what i was supposed to be thinking so the task has this line

src:  files/html/

and not

src: /home/student/role-create/roles/myvhost/files/html/

that you are concerned about the path and how it finds the files. So, one thing to consider is that in terms of a playbook and if you are running directly both should work in the real world. However, for ROLES and what you are going for in the real world is the ability to share and reuse. The files/html is a realative path for the Ansible role you are creating and when you create a role, the tasks/main.yml, knows where things are the handlers/main.yml and other items in the role know where things can be found. Likewise, playbooks using those roles know where things can be found all based on relative paths. Having the src specified with an absolute path, when that role is used somewhere else, the file and path would not be in your role.

Might be extra confusing is the fact that we use the "roles" directive instead of tasks and it does its thing and you aren't necessarily seeing that the tasks and variables and files and components that get imported in the role are available.

Things can get even more complex when you look at roles and collections. I've attached a couple PDFs of when I have taught the RH294 before that give an Ansible overview and introduction as well as give AAP overviews with the newer collections. Where things really get wild is where you can use an import_playbooks from a collection.

Feel free to check out these resources that I usually provide to my students.

Useful Github Repositories


Useful Github Repositories for VS Code


VS Code Marketplace Extensions

 

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training
uconn-alf02013
Mission Specialist
Mission Specialist
  • 109 Views

Hi, i accept your answer.

But if Ansible knows to check the path directory structure of a role,
I would have thought the location ../roles/myvhost/files/html would have been searched ?
and then
               src: files/html/
would have been fine.
But i used the absolute path
               src: /home/student/role-create/roles/myvhost/files/html/
just to get it to work cuz i was confused how things actually work.

Apparently in the lab exercise something must be configured somewhere that
             src: files/html/
works and it exists at
            /home/student/role-create/files/html/
I just need to run the lab again and see if i can figure out how/where that is done.
Thanks for all the help !

Travis
Moderator
Moderator
  • 107 Views

You're welcome. As far as I can remember (it has been about 18 months since I last delivered this course) there is nothing special done in the lab environment to make things work. Also, keep in mind, the path structure for the role is absolute within that role only (not the filesystem). So when using the motd role, you are already in that motd directory, and then in it, you have vars, tasks, files, etc. If there is another role like httpd there is another set of directories that match what is in all the other roles.

So there is a difference in how things are "on the filesystem" vs. "included in the role".

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training
Join the discussion
You must log in to join this conversation.