I have my fresh Ubuntu server on Linode and I have a root password for that server.
Now my Ansible playbook is like this:
- hosts: linode
sudo: true
remote_user: user1
roles:
- role: common
- role: apache2
I want to execute the playbook as user1
, but thing is user1
does not exist yet and I don't want to run roles as root
.
I need to manually create user1
before running a playbook.
Is there any way to automate that?
To run a specific command as the root user in Ansible, you can implement the become directive and set the value to 'true. ' Doing this tells Ansible to implement sudo with no arguments when running the command.
You need to be root to execute - ansible.
Ansible Sudo or become is a method to run a particular task in a playbook with Special Privileges like root user or some other user. become and become_user both have to be used in a playbook in certain cases where you want your remote user to be non-root.it is more like doing sudo -u someuser before running a task.
If you want to run multiple tasks in a playbook concurrently, use async with poll set to 0. When you set poll: 0 , Ansible starts the task and immediately moves on to the next task without waiting for a result. Each async task runs until it either completes, fails or times out (runs longer than its async value).
Keep in mind that you are allowed multiple "plays" in a single playbook file. So this could be the contents of a single playbook file:
- name: First play, to create the user
hosts: linode
sudo: true
remote_user: root
tasks:
- name: create my user
user: name=user1 password=etc...
- Second play, to do the rest of the work
hosts: linode
sudo: false
remote_user: user1
roles:
- role: common
- role: apache2
Note that I have used a task to create the user, but that could be a role so you can re-use it between different projects.
You can create a very simple bootstrap playbook that you run as root and it'll create your user1
which will then run all the other playbooks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With