Export project list with branch names

Someone please help how to export project details with the branch names. We are using AWX.
Is it possible to do from Molecule server or from Ansible server ?

We tried some python scripts but so far it is only exporting project details, last run details etc.

What branch names are you looking for, it doesn’t store all of them, just the scm_branch or scm_refspec, that you’ve selected for the project, you can see it in api/v2/projects/ api endpoint. To export, easiest way is something like this

---
- name: Export projects
  hosts: localhost
  connection: local
  gather_facts: false
  environment:
     CONTROLLER_HOST: https://localhost
     CONTROLLER_USERNAME: admin
     CONTROLLER_PASSWORD: password
     CONTROLLER_VERIFY_SSL: False

  tasks:
    - name: Export projects
      awx.awx.export:
        projects: 'all'
      register: export_results

    - debug:
        var: export_results

    - name: Export projects to file
      copy:
        content: "{{ export_results | to_nice_yaml( width=50, explicit_start=True, explicit_end=True) }}"
        dest: projects.yaml
...
2 Likes

Thanks Sean.
We are basically looking for projects that are not using standard branch names like “awx-test”, “awx-syst” and “awx-prod”.

We want to list all the projects that are hosted in AWX with branch names so that we can reach out to project owners to use correct branch names as mentioned above.

The above should work then, you could then not put it in a file, but parse the data.

Another option is to use the awx.awx and the Ansible Galaxy collections, and the exported data from ALL, with that same module. Store them in Git, and then run a playbook on controller or a CI pipeline to make sure everything is set. Similar to what I did in this blog.

Several companies I know use this to prevent drift, make sure things like job templates are on the right playbook, workflows constructed correctly, inventories sources updated, and have PR approval over changes to the AWX configuration.

2 Likes

The code you have shared is working fine. Thanks a lot. Is there a way to filter out to get only the project name and branch name ?

I know we can get it like project.name etc but I don’t know how to use it here.

Sorry to bother. I am new to ansible and yml. I am learning. Your help is very much appreciated.

Hi @amvj welcome to Ansible and the forum

If you end up finding @sean_sullivan’s answer help with your question please remember to mark his reply as the solution.

1 Like

This should work in terms of removing all the unnecessary variables and getting what you want


- name: Setup controller
  hosts: localhost
  connection: local
  gather_facts: false

  environment:
    CONTROLLER_HOST: controller.nas
    CONTROLLER_USERNAME: admin
    CONTROLLER_PASSWORD: secret123
    CONTROLLER_VERIFY_SSL: false

  tasks:
    - name: Export projects
      awx.awx.export:
        projects: all
      register: export_results

    - name: Create new project list
      ansible.builtin.set_fact:
        projects: "{{ projects|default([]) +
                          [{'name': item.name,
                            'scm_branch': item.scm_branch}] }}"
      loop: "{{  export_results.assets.projects }}"

    - name: Export projects to file
      copy:
        content: "{{ projects | to_nice_yaml( width=50, explicit_start=True, explicit_end=True) }}"
        dest: projects.yaml
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.