I want to change the behaviour of an ansible playbook when it is executed from within an interactive console as opposed to non-interactive console (like CI environment)
How can I do this from inside the playbook?
I want to change the behaviour of an ansible playbook when it is executed from within an interactive console as opposed to non-interactive console (like CI environment)
How can I do this from inside the playbook?
You need a way to determine whether your command runs from a CI or not. If you have access to what command to run then I would suggest to simply add some extra variables to the command, for example
ansible-playbook -i inv.yml --extra-vars "ci=True" playbook.yml
and then you can refer to the ci
variable inside your playbook to determine what to do.
If this is cumbersome then the best thing to do is rely on the environmental variables that most CI servers set. You should consult the documentation of your CI server whether it sets any kind of environmental variables, and if yes what they do. Most (but not all) do set the CI
variable to true
. Worst case scenario you should specify this variable manually in the configuration of your CI server.
You should be able to access the environment variables of the management host machine using:
{{ lookup('env','CI') }}
Of course for these options to work you have to make sure that the environment variable is properly set on non-interactive shells (which the CI should do for you), and is not set on interactive ones.