Published
- 2 min read
Changing Root Password Using an Ansible Playbook
Learning to Automate with Ansible
Ansible:
- Ansible is used as a configuration management system and an open-source automation tool to deploy, manage, and scale infrastructure and applications
This is how you can automate the change in the root password of your systems using ansible.
1**. Create an Ansible ‘Hosts’ file**
Open your terminal in your local computer and create a new file called “change_password.yml” using the touch command in Linux or MacOS
Now you can open it in your preferred text editor. In this demonstration I will use Visual Studio Code. You can run the editor through your terminal using the ‘code’ command.
2. Create an Ansible ‘Hosts’ file
The hosts file in Ansible is where you define your server names and assign them variables in the inventory. In your text editor, create a new file and place all your hosts in it in the following format:
[Web_servers]
web1 ansible_host = 192.168.1.101 ansible_user = ubuntu
web2 ansible_host = 192.168.1.102 ansible_user = centos
3. Add the following modules to your playbook
Using Ansible builtin modules, you can change the password with the following tasks.
Essentially, what this code does is define the name for the task you are running, in our case, we name the task “Change root password”. We define the user credentials to authenticate as the root user. and change the password using the “password” command, where we first check what the root password variable is where we defined it as our environment variable, and we use a “when” clause to ensure the the password stored is not empty and is defined.
4. Running the playbook
Back in the terminal, you can run the playbook to change the root password using the “ansible-playbook” command;
ansible - playbook - i / location / where / hostsAreStored - l web_servers change_password.yml
Note, if you are using CI/CD, you can integrate this playbook with your YAML file that is running CI/CD.
Conclusion
This was the tutorial on how to change your system’s root password. I hope you enjoyed and thanks for reading and subscribe!