ansible-playbook權限提高多種方式

ansible-playbook 能夠方便快速的批量執行部署和運維任務,對於不一樣的場景和服務器,須要使用不一樣的權限提高方式。html

最佳實現:爲了提升playbook的兼容性,跟功能沒有直接關係的權限提高腳本,不要出如今palybook正文中,能夠在ansible-playbook運行的時候,經過-e傳入java

場景一:咱們有服務器的root密碼,並且容許root直接登錄。shell

ansible-playbook -i 'aliyun.lihuanhuan.net,' ./ansible_playbook_test/site.yml -e "ansible_ssh_user='root' ansible_ssh_pass='password'"
#切換到app_user,並執行java程序
- name: run app by java_user
  shell: java -jar hello.jar
  become: yes
  become_method: su
  become_user: app_user

場景二:咱們有服務器的root密碼,可是隻容許普通用戶user1使用su切換到root。服務器

ansible-playbook -i 'aliyun.lihuanhuan.net,' ./ansible_playbook_test/site.yml -e "ansible_ssh_user='user1' ansible_ssh_pass='user1_password' ansible_become='yes' ansible_become_method='su' ansible_become_user='root' ansible_become_pass='root_password' " -vvv
#切換到app_user,並執行java程序
- name: run app by java_user
  shell: java -jar hello.jar
  become: yes
  become_method: su
  become_user: app_user

場景三:咱們只有服務器的app_user密碼,並且只容許普通用戶user1使用su切換到app_user。app

ansible-playbook -i 'aliyun.lihuanhuan.net,' ./ansible_playbook_test/site.yml -e "ansible_ssh_user='user1' ansible_ssh_pass='user1_password' ansible_become='yes' ansible_become_method='su' ansible_become_user='app_user' ansible_become_pass='app_user_password' " -vvv
#切換到app_user,並執行java程序
- name: run app by java_user
  shell: java -jar hello.jar
  become: yes
  become_method: su
  become_user: app_user

場景四:咱們只有user1和password,可是容許使用特定的實用程序切換到root,例如:dzdo su -運維

ansible-playbook -i 'aliyun.lihuanhuan.net,' ./ansible_playbook_test/site.yml -e "ansible_ssh_user='user1' ansible_ssh_pass='user1_password' ansible_become_exe='dzdo su -' ansible_become='yes' ansible_become_method='su' ansible_become_user='root' ansible_become_pass='user1_password' " -vvv
#切換到app_user,並執行java程序
- name: run app by java_user
  shell: java -jar hello.jar
  become: yes
  become_method: su
  become_user: app_user

refer to https://docs.ansible.com/ansi...ssh

相關文章
相關標籤/搜索