ansible-test and roles variables.

Hi, I’m trying to use Ansible Test to build an integration test and have a few questions.

  • I read a few topics about the integration-config.yml file. What can I put in this file? Is there any documentation about it?

  • I have some roles to test with variables declared in them. How can I use these variables in my tests? I don’t want to duplicate the variables declared inside the roles in the integration/targets folders. Is there any documentation about variable usage for Ansible Test?

Thanks a lot .

The inventory_config.yml file is loaded from tests/integration/integration_config.yml in collections. It can contain arbitrary variables. The only documentation I found seems outdated (those example templates no longer exist).

You can use role variables in your test targets by passing public: True to include_role, or by using import_role (which sets public to True by default). If you use include_role, those role variables will be available following that task. If you use import_role, the role variables will be available anywhere in the test target. Here’s some documentation on the differences between includes and imports.

Thank you for your reply.

I tried different things with include_role, which raised new problems. I’ll try to summarise them here:

Here is the structure that was tested:

my_repo/
├── src/
│   └── ansible/
│       ├── inventory/
│       │   └── production.ini    
│       ├── playbooks/
│       │   └── site.yml           
│       └── roles/
│           └── role1/
│               ├── defaults/
│               │   └── main.yml  
│               ├── tasks/
│               │   └── main.yml   
│               ├── vars/
│               │   └── main.yml   
│               └── ...            
└── tests/
    └── integration/
        ├── inventory             
        └── targets/
            └── test_role1/
                ├── tasks/
                │   └── main.yml   
                └── vars/
                    └── main.yml   

What I tried to do within the test_role1 was to include or import the role1.
I tried executing ansible-test with something like this:

ansible-test integration --controller "origin:python=3.13@/home/user/code/.venv/bin/" --target "ssh:user@vm,python=3.9

The command works well without any ‘include’ or ‘import’ tasks, meaning that ansible-test works well with this configuration. However, I cannot ‘link’ Role1 to Test_Role1. The same applies to include or import vars.

Having read your reply, I think my tests folder should be within the ‘src/ansible’ folder, next to the ‘inventory’, ‘roles’ and ‘playbook’ folders, to make the ‘role1’ variables available in my tests.

my_repo/
└── src/
    └── ansible/
        ├── inventory/
        │   └── production.ini    
        ├── playbooks/
        │   └── site.yml           
        ├── roles/
        │   └── role1/
        │       ├── defaults/
        │       │   └── main.yml  
        │       ├── tasks/
        │       │   └── main.yml   
        │       ├── vars/
        │       │   └── main.yml   
        │       └── ...            
        └── tests/
            └── integration/
                ├── inventory    
                └── targets/
                    └── test_role1/
                        ├── tasks/
                        │   └── main.yml   
                        └── vars/
                            └── main.yml  

The ansible-test command no longer works.:

Configured locale: en_US.UTF-8
RLIMIT_NOFILE: (1024, 1048576)
FATAL: Cannot run integration tests without "roles/test/" or "tests/integration/targets/".

I tried to remove the ‘ansible-test’ command from almost every location within my repository, and as far as I can tell, I found this topic: ansible-test does not work with collections installed by galaxy inside a git repository · Issue #68499 · ansible/ansible · GitHub, which contains the same error.

In my configuration, ‘my_repo’ is a Git repository.

What do you think of this? What folder structure would you recommend for testing with ansible-test?

Sorry for this long message!

The roles directory needs to be in the root of the collection. The layout for collections is documented here Collection structure — Ansible Community Documentation.

ansible-test needs to be run from the root of the collection, and the path must include ansible_collections. There’s some documentation for that here Testing collections — Ansible Community Documentation.

Here's a minimal layout containing a role and integration test
/home/shertel/collections/ansible_collections/ns/col
├── roles
│   └── role1
│       └── vars
│           └── main.yml
└── tests
    └── integration
        ├── integration_config.yml
        └── targets
            └── test_role1
                └── tasks
                    └── main.yml

9 directories, 3 files
roles/role1/vars/main.yml
test_role_variable: defined_for_role1
tests/integration/integration_config.yml
test_variable: defined_for_tests
tests/integration/targets/test_role1/tasks/main.yml
- assert:
    that:
      - test_variable is defined

- include_role:
    name: role1  # Or ns.col.role1
    public: True

- assert:
    that:
      - test_role_variable is defined
Running the test
$ pwd
/home/shertel/collections/ansible_collections/ns/col
$ ansible-test integration test_role1
WARNING: Unable to determine context for the following test targets, they will be run on the target host: test_role1
Running test_role1 integration test role

PLAY [testhost] ****************************************************************

TASK [Gathering Facts] *********************************************************
ok: [testhost]

TASK [test_role1 : assert] *****************************************************
ok: [testhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

TASK [include_role : role1] ****************************************************
included: role1 for testhost

TASK [test_role1 : assert] *****************************************************
ok: [testhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

PLAY RECAP *********************************************************************
testhost                   : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

WARNING: Reviewing previous 1 warning(s):
WARNING: Unable to determine context for the following test targets, they will be run on the target host: test_role1

Thanks a lot for your reply, and for sharing resources. I’ll give a try.

Regards.

1 Like