How to create GCP policy that will install ops-agent on all Ubuntu 22.04 VMs?

67 Views Asked by At

I'm trying to create a policy that will install ops-agent on all Ubuntu 22.04 automatically, so I've created this OS policy assignment and it seems it's been rolled out successfully but I still see my VMs as Non-compliant, what is that I'm doing wrong?
Command: gcloud compute os-config os-policy-assignments describe ops-agent-ubuntu-22-stag-os-policy --project=project-stag
Result:

baseline: true
instanceFilter:
  inventories:
  - osShortName: ubuntu
    osVersion: '22.04'
name: projects/10777777778136/locations/europe-west1-b/osPolicyAssignments/ops-agent-ubuntu-22-stag-os-policy
osPolicies:
- id: ops-agent-ubuntu22-stag-os-policy1111111
  mode: ENFORCEMENT
  resourceGroups:
  - inventoryFilters:
    - osShortName: ubuntu-22.04
    resources:
    - exec:
        enforce:
          interpreter: SHELL
          script: |
            sudo bash add-google-cloud-ops-agent-repo.sh --uninstall 
            sudo bash add-google-cloud-ops-agent-repo.sh --also-install
        validate:
          interpreter: SHELL
          script: |
            sudo bash add-google-cloud-ops-agent-repo.sh --uninstall 
            sudo bash add-google-cloud-ops-agent-repo.sh --also-install
      id: uninstall-existing-agent
    - exec:
        enforce:
          interpreter: SHELL
          script: sudo systemctl start google-cloud-ops-agent.target && exit 100
        validate:
          interpreter: SHELL
          script: if (ps aux | grep 'opt[/].*google-cloud-ops-agent.*bin/'); then
            exit 100; else exit 101; fi
      id: ensure-agent-running
revisionCreateTime: '2024-02-26T07:23:44Z'
revisionId: fd1aa77c-4444-45ea-9e69-929678cf6b09
rollout:
  disruptionBudget:
    percent: 10
  minWaitDuration: 0s
rolloutState: SUCCEEDED
uid: 746d7ba2-07bd-44444-a466-8f298f2b7cfe


and when trying to check the reports it doesn't show any reason for the failure
Command:

gcloud compute os-config os-policy-assignment-reports describe ops-agent-ubuntu-22-stag-os-policy --instance=test-opsagent --location=europe-west1-b --project=project-stag

Result:

gcloud compute os-config os-policy-assignment-reports describe ops-agent-ubuntu-22-stag-os-policy --instance=test-opsagent --location=europe-west1-b --project=project-stag
instance: test-opsagent
lastRunId: ''
name: projects/10777777778136/locations/europe-west1-b/instances/1924903941761499182/osPolicyAssignments/ops-agent-ubuntu-22-stag-os-policy/report
osPolicyAssignment: projects/10777777778136/locations/europe-west1-b/osPolicyAssignments/ops-agent-ubuntu-22-stag-os-policy@fd1aa77c-ad48-45ea-9e69-929678cf6b09
osPolicyCompliances:
- complianceState: NON_COMPLIANT
  complianceStateReason: ''
  osPolicyId: ops-agent-ubuntu22-stag-os-policy1111111
updateTime: '2024-02-26T07:33:50.896031Z'
0

There are 0 best solutions below