include_vars or vars_files in a role

52 views
Skip to first unread message

Tony Wong

unread,
Apr 24, 2023, 1:35:53 PM4/24/23
to Ansible Project
I got a pb that works like this. any idea how i can convert this to a role?



---

nametest

  hostsall

  gather_factsno

  vars_files:

    - vcenter_creds.yml

    - vars.yml

 

  tasks:

  - nameEnable SSH service

    vmware_host_service_manager:

      hostname"{{ vcenter_hostname }}"

      username"{{ vcenter_username }}"

      password"{{ vcenter_password }}"

      esxi_hostname"{{ hostname }}"

      validate_certsno

      statepresent

      service_name"TSM-SSH"

    delegate_tolocalhost

sdfsdfsd

Tony Wong

unread,
Apr 24, 2023, 2:04:30 PM4/24/23
to ansible...@googlegroups.com
similar to this. This pb i want to break it up into roles

any idea?

---

hostsall

  gather_factstrue

  connectionlocal

  vars_files:

    - vcenter_creds.yml

    - vars.yml

 

  tasks:

 

  - nameGather vmware host facts from vCenter

    community.vmware.vmware_host_facts:

      hostname"{{ vcenter_hostname }}"

      username"{{ vcenter_username }}"

      password"{{ vcenter_password }}"

      esxi_hostname"{{ hostname }}"

      validate_certsno

    registerhost_facts

    delegate_tolocalhost

 

  - nameEnable SSH service

    vmware_host_service_manager:

      hostname"{{ vcenter_hostname }}"

      username"{{ vcenter_username }}"

      password"{{ vcenter_password }}"

      esxi_hostname"{{ hostname }}"

      validate_certsno

      statepresent

      service_name"TSM-SSH"

    delegate_tolocalhost

 

  - name"change root pass"

    community.vmware.vmware_local_user_manager:

      hostname"{{ hostname }}"

      username"{{ esxi_root_user }}"

      password"{{ esxi_root_pass }}"

      local_user_name"{{ esxi_root_user }}"

      local_user_password"{{ new_esxi_root_pass }}"

      statepresent

      validate_certsFalse

    delegate_tolocalhost

 

  - nameEnable SSH service

    vmware_host_service_manager:

      hostname"{{ vcenter_hostname }}"

      username"{{ vcenter_username }}"

      password"{{ vcenter_password }}"

      esxi_hostname"{{ hostname }}"

      validate_certsno

      stateabsent


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/F6s6Iaaawxs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d637013b-7cba-4401-b7c0-edcb70b6a52cn%40googlegroups.com.

Prady A

unread,
Apr 25, 2023, 12:46:34 AM4/25/23
to ansible...@googlegroups.com
You can follow the below structure.
For more information .. happy coding..


Cheers 

You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALmkhkqb7ygKEc87sdhH0Mfnm_DPMTi2C5pEspm%2BNnUS2z1gcQ%40mail.gmail.com.

dulh...@mailbox.org

unread,
Apr 25, 2023, 1:05:42 AM4/25/23
to ansible...@googlegroups.com
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
 
nametest
  hostsall
  gather_factsno
 
  roles:
    - role1
 
(there are other ways to all the roles though but thius should do the job)
 
 
 
the ./tasks/main.yml
 
---
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Tony Wong

unread,
Apr 25, 2023, 8:59:53 AM4/25/23
to ansible...@googlegroups.com
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?



You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/F6s6Iaaawxs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1552518980.884442.1682399125194%40office.mailbox.org.

Tony Wong

unread,
Apr 25, 2023, 9:16:32 AM4/25/23
to ansible...@googlegroups.com
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




Dick Visser

unread,
Apr 25, 2023, 9:33:17 AM4/25/23
to ansible...@googlegroups.com
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....



Tony Wong

unread,
Apr 25, 2023, 9:42:15 AM4/25/23
to ansible...@googlegroups.com
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

Tony Wong

unread,
Apr 26, 2023, 9:06:20 AM4/26/23
to ansible...@googlegroups.com
any idea?

Haji Hoare

unread,
Apr 29, 2023, 11:28:42 PM4/29/23
to Ansible Project
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:
Reply all
Reply to author
Forward
0 new messages