include_vars or vars_files in a role

I got a pb that works like this. any idea how i can convert this to a role?

similar to this. This pb i want to break it up into roles

any idea?

You can follow the below structure.
For more information … happy coding…

https://www.golinuxcloud.com/ansible-roles-directory-structure-tutorial/

Cheers

the role structure can be as complex as mentioned in the previous post, but it does not have to include everything. In your case I’d say it boild down to this.

you create a structure like this:

base_folder

__ playbook.yml

__ /roles

__ role1

__/tasks

__main.yml

__/defaults

__/main
__vcenter_creds.yml
__vars.yml

the playbook.yml

  • name: test
    hosts: all
    gather_facts: no

roles:

  • role1

(there are other ways to all the roles though but thius should do the job)

the ./tasks/main.yml

for

__/defaults

__/main
__vcenter_creds.yml
__vars.yml

does these need to be under defaults under each role?

what if other roles need to access these same vars files?

this is what i have currently

change_esxi_root_pass

├── hosts

├── main.yml

└── roles

├── change_esxi_root

├── disable_ssh

└── enable_ssh

├── tasks

│ └── main.yml

└── vars

├── vars.yml

└── vcenter_creds.yml

You could use ansible-galaxy to instantiate a role skeleton:

dick.visser@GA0267 tmp$ ansible-galaxy init enable_ssh

  • Role enable_ssh was created successfully

dick.visser@GA0267 tmp$ tree enable_ssh/
enable_ssh/
├── README.md
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml

9 directories, 8 files

btw, different roles to enable and disable SSH to me sounds like too much overhead…

but how would i split up enable/disable ssh? can both be in same role? I need to enable ssh before i can change root pass, then disable ssh after its done

any idea?

Hi,

I think it would be a good idea to create a single role for managing ESXi settings, including changing the root password.
To make the role reusable, I recommend not specifying the hostname, ID, and password within the role itself.
In your playbook, you can utilize a vars_files directive to include a file like vcenter_creds.yml that contains definitions for variables such as vcenter_hostname.
These defined variables can then be accessed and used within your role as well.
In Ansible, variables defined within the role’s vars/main.yml file have a higher precedence than those defined in the playbook.
Therefore, to ensure that the playbook-defined variables are used, avoid defining the same variables in the role’s vars/main.yml file.

2023年4月26日水曜日 22:06:20 UTC+9 Tony Wong: