9

I'm trying to add users to my servers using Ansible. Each user have a different encrypted password.

I have something like that:

vars:
  users:
    - myuser1
    - myuser2
  password: encryptedpasswordhere

tasks:
  - name: Creating users
    user: name={{ item }} password={{ password }} groups=sudo,adm shell=/bin/bash
    with_items: users

This work great, but all users have the same password.

I'm looking to make an associative array. In PHP I would do that:

$users = array('user'=>'myuser1', 'password'=>'encryptedpass1',
               'user'=>'myuser2', 'password'=>'encryptedpass2',
               );

There is a way to do that in an Ansible playbook?

15

http://docs.ansible.com/playbooks_loops.html#looping-over-hashes

---
users:
  alice:
    name: Alice Appleworth
    telephone: 123-456-7890
  bob:
    name: Bob Bananarama
    telephone: 987-654-3210

and

tasks:
  - name: Print phone records
    debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
    with_dict: users
  • @Francis As a fellow PHP dev I wasn't aware that most other languages call our "associative arrays" "hashes". Helps with Googling! – ceejayoz Oct 1 '14 at 19:53
  • 1
    @ceejayoz "hash" or "dict". – tedder42 Oct 7 '14 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.