This guide covers the setup of an Ansible-based VMware automation environment including AWX, GitLab CE for source control, and vCenter integration.
The data flow is
- Playbooks stored in /etc/ansible/playbooks
- Git push to GitLab (http://gitlab.ash.local)
- AWX pulls from GitLab automatically on sync
- AWX runs playbooks against vCenter via community.vmware
- Secrets protected by Ansible Vault
Check if the AWX environment is running

Bring up the AWX environment

Verify pod health

Create a new admin user in Gitlab

Create an Access Token

Create blank project Project

Give it a name such as a vmware-automation and choose visibility: Private. Also create empty repo in GitLab with no README! file as we arent cloning it.

Next we will initialize Git in our source directory so back on our ansible server, go to /etc/ansible/ and here we will push all these yaml files from Ansible Node to Gitlab .
Create .gitignore
Some of these files dont need to published out to Gitlab such as secrets
awk@ansible:/etc/ansible$ cat > /etc/ansible/.gitignore << 'EOF'
# Secrets -
vault.yml
*.key
*.crt
# External repos
pyvmomi-community-samples/
EOFInitialize git & Configure git identity
awk@ansible:/etc/ansible$ git init
Initialized empty Git repository in /etc/ansible/.git/
awk@ansible:/etc/ansible$
awk@ansible:/etc/ansible$ git branch -M main
awk@ansible:/etc/ansible$
awk@ansible:/etc/ansible$ git config user.email "awk@ash.local"
awk@ansible:/etc/ansible$
awk@ansible:/etc/ansible$ git config user.name "awk"
awk@ansible:/etc/ansible$
awk@ansible:/etc/ansible$Stage files
awk@ansible:/etc/ansible$
awk@ansible:/etc/ansible$
awk@ansible:/etc/ansible$ git add .
awk@ansible:/etc/ansible$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: README.md
new file: ansible.cfg
new file: group_vars/centos.yml
new file: group_vars/home-vcsa.yml
new file: group_vars/nginx.yml
new file: group_vars/resolvfile.yml
new file: group_vars/ubuntu.yml
new file: group_vars/webserver.yml
new file: group_vars/zorin.yml
new file: hosts
new file: playbooks/.gitignore
new file: playbooks/centos-basic-packages.yaml
new file: playbooks/configure_postfix.yml
new file: playbooks/get_all_vms.yml
new file: playbooks/install_gitlab.yml
new file: playbooks/test_vcenter.yml
new file: playbooks/ubuntu-basic-packages.yaml
new file: roles/baseline/README.md
new file: roles/baseline/defaults/main.yml
new file: roles/baseline/handlers/main.yml
new file: roles/baseline/meta/main.yml
new file: roles/baseline/tasks/main.yml
new file: roles/baseline/templates/motd.j2
new file: roles/baseline/tests/inventory
new file: roles/baseline/tests/test.yml
new file: roles/baseline/vars/CentOS.yml
new file: roles/baseline/vars/Debian.yml
new file: roles/baseline/vars/Rocky.yml
new file: roles/baseline/vars/Ubuntu.yml
new file: roles/baseline/vars/main.yml
new file: webfiles/default-site.html
Commit
awk@ansible:/etc/ansible$ git commit -m "Initial commit - Ansible VMware automation"
[main (root-commit) 7874e5d] Initial commit - Ansible VMware automation
32 files changed, 656 insertions(+)
create mode 100644 .gitignore
create mode 100644 README.md
create mode 100644 ansible.cfg
create mode 100644 group_vars/centos.yml
create mode 100644 group_vars/home-vcsa.yml
create mode 100644 group_vars/nginx.yml
create mode 100644 group_vars/resolvfile.yml
create mode 100644 group_vars/ubuntu.yml
create mode 100644 group_vars/webserver.yml
create mode 100644 group_vars/zorin.yml
create mode 100644 hosts
create mode 100644 playbooks/.gitignore
create mode 100644 playbooks/centos-basic-packages.yaml
create mode 100644 playbooks/configure_postfix.yml
create mode 100644 playbooks/get_all_vms.yml
create mode 100644 playbooks/install_gitlab.yml
create mode 100644 playbooks/test_vcenter.yml
create mode 100644 playbooks/ubuntu-basic-packages.yaml
create mode 100644 roles/baseline/README.md
create mode 100644 roles/baseline/defaults/main.yml
create mode 100644 roles/baseline/handlers/main.yml
create mode 100644 roles/baseline/meta/main.yml
create mode 100644 roles/baseline/tasks/main.yml
create mode 100644 roles/baseline/templates/motd.j2
create mode 100644 roles/baseline/tests/inventory
create mode 100644 roles/baseline/tests/test.yml
create mode 100644 roles/baseline/vars/CentOS.yml
create mode 100644 roles/baseline/vars/Debian.yml
create mode 100644 roles/baseline/vars/Rocky.yml
create mode 100644 roles/baseline/vars/Ubuntu.yml
create mode 100644 roles/baseline/vars/main.yml
create mode 100644 webfiles/default-site.html
awk@ansible:/etc/ansible$ git log --oneline
7874e5d (HEAD -> main) Initial commit - Ansible VMware automation
awk@ansible:/etc/ansible$ git remote add origin http://gitlab.ash.local/awk/vmware-automation.git
awk@ansible:/etc/ansible$ git push -u origin main
Username for 'http://gitlab.ash.local': awk
Password for 'http://awk@gitlab.ash.local':
Enumerating objects: 45, done.
Counting objects: 100% (45/45), done.
Delta compression using up to 4 threads
Compressing objects: 100% (32/32), done.
Writing objects: 100% (45/45), 9.94 KiB | 1.66 MiB/s, done.
Total 45 (delta 3), reused 0 (delta 0), pack-reused 0
To http://gitlab.ash.local/awk/vmware-automation.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
awk@ansible:/etc/ansible$
Add remote and push
awk@ansible:/etc/ansible$ git remote add origin http://gitlab.ash.local/awk/vmware-automation.git
awk@ansible:/etc/ansible$ git push -u origin main
Username for 'http://gitlab.ash.local': awk
Password for 'http://awk@gitlab.ash.local':
Enumerating objects: 45, done.
Counting objects: 100% (45/45), done.
Delta compression using up to 4 threads
Compressing objects: 100% (32/32), done.
Writing objects: 100% (45/45), 9.94 KiB | 1.66 MiB/s, done.
Total 45 (delta 3), reused 0 (delta 0), pack-reused 0
To http://gitlab.ash.local/awk/vmware-automation.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
awk@ansible:/etc/ansible$
Our project files are now stored in gitlab

Sync AWX project
AWX → Projects → VMware Automation → Sync

On the AWX, Add Home vCenter credential and GitLab credential
| Resources → Credentials → Add → VMware vCenter Host: Username: administrator@vsphere.local |


vCenter credentials are now ready

| Add GitLab credential Resources → Credentials → Add → Source Control Username: awk, Password: <gitlab token> Create token in GitLab: Profile → Access Tokens → read_api + read_repository |

Both our credentails are now ready

Likewise, On the AWX, add the second vCenter credential
| Resources → Credentials → Add → Work VMware vCenter Host: Username: administrator@vsphere.local |

Create Project in AWX
Resources → Projects → Add Name: VMware Automation Source Control Type: Git URL: http://172.16.11.122/awk/vmware-automation.git Branch: main Credential: GitLab

Sync project

Create Inventory
There are two ways to add vCenter to the Ansible inventory
Inventory → Add → Home Lab vCenter

Our vCenter is now added

Go to the host section and add the vCenter

Then add Variables to the host

The next method is using a Static hosts file
| Add static hosts source Sources → Add → Sourced from Project Inventory File: hosts |

Next is to add vCenter dynamic source to Auto-discovers all VMs from vCenter
Sources → Add → VMware vCenter Credential: Home vCenter

Sync both sources

Now ansible is aware of our VM’s

Create Job Template in AWX
| Resources → Templates → Add → Job Template Name: Test vCenter Connection J |

| Launch AWX → Templates → Test vCenter Connection → Launch |

In the next blog, we will focus on some real automation using Ansible so that’s it for now.


