I have several clouds, where each has a different user that we need to login to in order to become root (for installing services).
Now, the first time that we receive a new server, we login as root, define our service account, then all playbooks from this point are logging in with the service account to run tasks.
The issue is: the first part of "login as root". Basically every set of servers are using something different, for example:
- One of servers cloud are using directly
root@myserver
to login - Other servers cloud are using an
admin@myserver
to login then we need to saybecome: yes
Our inventory in the above case is something like:
[myserver_cloud1]
# server that requires an admin user and from there to become root
x.x.x.x ansible_user=admin ansible_become=yes
[myserver_cloud2]
# root is by default in the playbook i just say become: yes
z.z.z.z
The playbook then, has an issue: ansible_user
will always override any remote_user: {{service_account}}
used inside the playbooks.
So I thought of doing something different, inventory will always say ansible_user=my_service_account
but, then, how can I differentiate in the playbook "how" to login as root ( directly, or via admin user then sudo ) based on the cloud type?
So I'm not sure what's the right way to configure inventory/playbooks to be able to:
- Sometimes run the playbook as
root
- By default run the playbook as my
service account
Edit - Adding an example to reproduce
See the following playbook
---
- name: Testing clouds
hosts: "{{ ansible_limit | default(all) }}"
gather_facts: false
roles:
- defaults
- webserver
tasks:
- name: Testing something
shell: "whoami"
no matter what I try: become:yes
or whatever, one set fails, and the other set works:
x.x.x.x : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
z.z.z.z : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Any ideas? I want to avoid from saying -u
in the command line.