Ansible ordinary users execute commands

background:

There are a group of hosts deploying k8s services, but they are not given root permissions. Dozens of hosts cannot be manually deployed one by one, you must use ansible. It saves time and improves work efficiency.

Steps:

  1. First add the information of ordinary users in the ansible.cfg configuration file, please see the screenshot below

Insert picture description here

Open the comment on privilege escalation, as shown in the figure
Insert picture description here

[privilege_escalation]

become=True ***//Use "true" or "yes" to indicate that this privilege is enabled, such as: become=true indicates that the ******* became switch is turned on.

become_method=sudo *//******** indicates how to switch the ordinary account to root or other required accounts. You can use su or sudo here. *

become_user=root *//******** is set to the root account, which is equivalent to when we log in to the remote host as a normal account, and then use su-root to switch to the root account. *

become_ask_pass=True *// means asking for password*

2. Execute this command on the command line:

ansible nlp -m shell -a'systemctl reload docker' -K can use ordinary users to execute commands

Guess you like

Origin blog.csdn.net/qq_34939308/article/details/110454215