You hint in the title about immutable infrastructure, so it sounds like you already know the solution: don't change existing servers, but bring up new ones with your changes, switch over to them, and switch back to the old ones if necessary.
Theoretically any of the standard config management tools can roll back by simply checking out a previous commit and ...
I solved this by adding
to the no_log-task. This makes ansible continue to the next task, even when the task fails. Then for the next task define a debug task, which always fails and outputs the registered variable, but only runs when the previous task failed:
- name: Error output
Generally, Ansible is very flexible and often there are more "correct" solutions.
Start for example with the question "Where do the configuration data come from?", put the default data to the roles and decide which variables should be configured in "group_vars/host_vars", in the roles, and which in the playbooks. Review Variable precedence: Where should I ...
Use the --limit option to select a subset of hosts:
ansible-playbook -i /path test.yml --tags automation --limit host2
Its description from ansible-playbook documentation:
further limit selected hosts to an additional pattern
How to formulate the code to get rid of [DEPRECATION WARNING] during runtime?
Simply by following the tip in the error message.
when: CURRENT_INSTALLED_VERSION.stdout is version(EXPECTED_REMOTE_SOFTWARE_VERSION,'>=')
Is there are better way to apply the same logic?
I don't see anything wrong if it meets your current needs.
Only remark: The first ...
The best practice is to separate production and staging(testing) inventory. See Staging vs Production.
This way you pick with -i what you are targeting.
To make it more robust you might want to clone a separate structure for production and restrict access to it. Pull from staging before deploying to production.
In detail, this would mean to 1) put the ...
The magic you need is magic variables, in particular the groups variable, which is
a dictionary/map with all the groups in inventory and each group has the list of hosts that belong to it.
Then you can use template filters to format the list: use map with regex_replace to add the quotes to all list elements and then join them into the final result.
I met the same situation. I have login1 for my machine, passwordless sudo and login2, under which I should perform some actions. I did not solve it by ansible means. But I made such a workaround:
- name: "Install nvm"
shell: sudo -u buildkite-agent bash -c "<my commands to be performed on behalf of buildkite-agent>"
Such a case: