# Changing different passwords for servers in Ansible

**URL:** <https://forum.ansible.com/t/changing-different-passwords-for-servers-in-ansible/4190>\
**Category:** Get Help\
**Created:** [March 3, 2024, 4:42pm UTC](https://forum.ansible.com/t/changing-different-passwords-for-servers-in-ansible/4190 "2024-03-03T16:42:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aria](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aria/32/2467_2.png) [@aria](https://forum.ansible.com/u/aria)\
**Post date:** [March 3, 2024, 4:42pm UTC](https://forum.ansible.com/t/changing-different-passwords-for-servers-in-ansible/4190/1 "2024-03-03T16:42:15Z")

</div>

I have 10 Linux servers. I am going to change the password of the root user for all servers. I have written a playbook with “expect” and “user” modules, but I need to run this playbook separately for each server.  
[php]  
\*_ **My playbook for change eatch server password** _

- name: change password server 1  
expect:  
command: passwd root  
responses:  
(?i)password: ‘adfatgqweeee’  
no\_log: true  
tags: [change\_pass1]

- name: change password server 2  
expect:  
command: passwd root  
responses:  
(?i)password: ‘adfatgqweeee’  
no\_log: true  
tags: [change\_pass2]

\*\*\*\*\*\* Playbook command\*\*\*\*\*\*

# ansible-playbook -i inventory/host\_test myproject.yml --tags change\_pass1

# ansible-playbook -i inventory/host\_test myproject.yml --tags change\_pass2

[/php]

I am looking for a solution to set a different password for each server through a playbook. for example :  
[php]  
var1: server1  
var2: server2  
var3: server3  
.  
.  
.  
var10: server10

var1: srver\_pass1  
var2: srver\_pass2  
var3: srver\_pass3  
.  
.  
.  
var4: srver\_pass4  
[/php]

I know that passwords and servers should be defined as variables, but I don’t know how to write the playbook. I would be grateful if you could advise me on this matter.

---

<div class="post-metadata">

**Author:** ![Dustin](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dustin/32/2573_2.png) [@Dustin](https://forum.ansible.com/u/Dustin)\
**Post date:** [March 4, 2024, 5:40pm UTC](https://forum.ansible.com/t/changing-different-passwords-for-servers-in-ansible/4190/2 "2024-03-04T17:40:39Z")

</div>

# Very Introductory Example

It sounds like you are at the beginning of your ansible learning and testing. Here’s a quick and simple example to achieve what you want.

**–This is a very brief example, do not use this for production–**

Change to your working directory.  
Create a file named inventory. Add a line for each server, using this as your example:

```ini
# File: ~/workspace/inventory
[servers]
server_1 root_pass=Password1
server_2 root_pass=Password2
server_3 root_pass=Password3

```

Create a playbook for changing the password on each system.

```yaml
---
# File: ~/workspace/play_change_passwords.yml
- name: Change Root Password
  hosts: servers
  become: yes
  become_method: sudo
  become_user: root
  become_flags: '-i'
  
  tasks:
    - name: Change Root Password
      ansible.builtin.user:
        name: root
        password: "{{ root_pass | password_hash('sha512') }}"

```

Run the playbook:  
`ansible-playbook -i ~/workspace/inventory ~/workspace/play_change_passwords.yml`

# Key Takeaways and Things to Research

## Inventory and Passwords

Do not use this inventory file in production. The playbook idea is alright, but handling inventories and passwords should not be done like I have above.

## Ansible Setup

Research how to setup a basic ansible-core environment.  
[This Ansible Documentation](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html%5D) is a great starting point.

## Ansible Vaults

Research how to use `ansible-vault` command and `ANSIBLE_VAULT_PASSWORD_FILE` environment variable to begin securely storing passwords and other sensitive information. For example, this is where you would store `root_password: YourRootPassword`

## Host Vars

Research how to setup a `host_vars` directory and use it to store variables that are unique to each host in your inventory.
