If you really are interested in exactly ansible way.
An ansible script that runs on a server server-ansible-01 from under user user01 and does work on another server-web-01 can really work from under user ansible of server server-web-01. (At what server-ansible-01 and server-web-01 can be the same server - and this is exactly your case)
This is a full-time feature ansible. It is set at the playbook level, not the role - although if there is a desire, then for the individual steps of the role connected in the playbook, other credentials can be specified.
Just enter your remote_user in your remote_user
:
- name: asdf hosts: '{{ target }}' remote_user: root roles: - myrole01
Also, if you connect to a remote server-ansible-01 as ansible, then you can perform some steps as an apache or root user. We read about become
- Become (Privilege Escalation)
This works in modern versions of an ansibl (it seems, from 1.9, I definitely have been in 2.1 for a long time), there used to be other commands, sudo_user
and sudo
, you can search in the documentation, I have already refactored my playbooks.
Questions that relate to the ability to connect to another server - they always boil down to the correct setting of ssh-keys (I recommend to read in this my question: Ansible: forward ssh-agent and sudo ) and will partially affect the issue of passwordless sudo:
- name: nopasswd sudo for ansible user lineinfile: "dest=/etc/sudoers state=present regexp='^{{ ansible_user.login }}' line='{{ ansible_user.login }} ALL=(ALL) NOPASSWD: ALL'"
I do not very much welcome the fact that you want to execute ansible scripts from under the root account (all the scripts work quietly without me, moreover - with SELinux enabled), this crutch if you don’t want to clean up is yours own decision.
In general, nothing prevents the launch of a playbook on behalf of the user wwwrun version and the execution of scripts on a “remote” web server (with the same IP) but as root of this web server.
All the above concerns the performance of a playbook from another user. But.
You put a completely different question into your question: "how do I run the sh-script from the admin site of the site, but not from the user of the web server".
This question is in no way related to ansibl, the answer to it was given to you above - if this part of the question is more important to you, then I recommend accepting the answer. .
Moreover, if you want to concretize the question and set the task “to run not just any script whatsoever, but an ansible playbook”, then you may have difficulties precisely because the user wwwrun has reduced rights for security reasons - you will need him in / usr / apache create your own ssh key and much more to fix. I went through, I know. It’s not that these problems are unsolvable, it’s just that lately I’ve somehow paid more attention to not creating security holes when creating solutions, and you have enough holes here.