How in a single playbook become root as well as application user

Viewed 74

I have a requirement to setup an application and for that I need to install Nginx as root and then run another Java application as a different application user.

The condition for this is, I have a privileged user "priv_suer" which has sudo and I'm running the playbook as this user as I need to install and configure Nginx. But my application user is different from this user "app_user" which is unprivileged application only user.

The issue I'm facing is, this app_user needs password to become app_user. So in my case I need two passwords one is to become root and another one to become app_user.

I referred Understanding privilege escalation: become and only option I could find was "ansible_become_password".

Can anyone help me with this?

1 Answers

I think that privilege escalation can help. My solution is:

  • Declare different groups for servers running your Java applications and servers you want to install Nginx. In your case, the two groups can share the same servers.

    Here below I give an example of inventory.yml file:

all:
  children:
    app:
      hosts:
        127.0.0.1:
      vars:
        ansible_become_pass: app_user@123
        ansible_python_interpreter: /usr/bin/python3
        ansible_user: app_user

    nginx:
      hosts:
        127.0.0.1:
      vars:
        ansible_become_pass: root@123
        ansible_python_interpreter: /usr/bin/python3
        ansible_user: root
  • An example of playbook is as follow:
- hosts: app
  tasks:
    - name: Install Java app
      become: yes

- hosts: nginx
  tasks:
    - name: Install NGINX
      become: yes
  • Finally, run your ansible playbook with an inventory provided using -i option:
ansible-playbook -i etc/ansible/inventory.yml etc/ansible/playbook.yml
Related