Search code examples
ansiblerepeatroles

Is there a way to running a role until it succeeds in a play?


I have a role name system_check. After the system start up, I need to wait until this role succeeds.

Is there a way to run this role repeatedly until it succeeds?


Solution

  • Q: "Is there a way to running a role until it succeeds in a play?"

    A: It's not possible. The module include_role says

    Ignores some keywords, like until and retries.

    Instead, use ansible-runner. For example Use Runner as a standalone command line tool and test artifacts (rc, status, stdout).


    For example

    #!/bin/bash
    
    rcfile=private1/artifacts/ID01/rc
    statusfile=private1/artifacts/ID01/status
    
        ansible-runner -p test.yml -i ID01 run private1
        rc=$(cat $rcfile)
        echo "rc: $rc"
    
        until [ "0"  == "$rc" ]; do
            ansible-runner -p test.yml -i ID01 run private1
            rc=$(cat $rcfile)
            echo "rc: $rc"
        done
    
    
    $ tree private1
    private1
    ├── artifacts
    ├── daemon.log
    ├── env
    │   ├── envvars
    │   ├── extravars
    │   ├── passwords
    │   ├── settings
    │   └── ssh_key
    ├── inventory
    │   └── hosts
    └── project
        ├── roles
        │   └── testrole
        │       ├── defaults
        │       │   └── main.yml
        │       ├── handlers
        │       │   └── main.yml
        │       ├── meta
        │       │   └── main.yml
        │       ├── README.md
        │       ├── tasks
        │       │   └── main.yml
        │       ├── tests
        │       │   ├── inventory
        │       │   └── test.yml
        │       └── vars
        │           └── main.yml
        └── test.yml