0

I am trying to learn Ansible and I've been dabbling with some roles that I've found on Github, specifically this one that automates user creation together with sudo rules and SSH key deployment:

https://github.com/GROG/ansible-role-management-user

I've cloned all four roles that are dependent and I am trying to use it to make two or more management users on a test server. I am having issues understanding, how to list more users to add:

  • If I use the defaults/main.yml file of management role to list more users under the management_user_settings, the users get created, but their SSH keys are not deployed - they are deployed if there is only one user there.
management_user_settings:
  - name: usr1
    state: present
    comment: user1 mykiy
    shell: '/bin/bash'
    authorized_keys:
      - key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
        exclusive: no
    sudo:
      hosts: ALL
      as: ALL
      commands: ALL
      nopasswd: yes

  - name: usr2
    comment: user2 otherkiy
    shell: '/bin/bash'
    authorized_keys:
      - key: "{{ lookup('file', '/home/user/SSH/user.name/id_rsa.pub') }}"
        exclusive: no
    sudo:
      hosts: ALL
      as: ORACLE
      commands: ALL
      nopasswd: yes
  • I've tried to list variables in a playbook for this role, but I have the same issue, users get created, but their SSH key is not deployed.

  • I've also tried to make a list with variables and use with_items: command, but I can't get it to work.

---
- hosts: all
  user: root
  vars:
    user_list:
          name: "{{ item.name }}"
          comment: "{{ item.comment }}"
          shell: '/bin/bash'
          uid: "{{ item.uid }}"
          groups: "{{ item.groups }}"
          password: "{{ item.password }}"
          state: "{{ item.state }}"
          authorized_keys:
            - key: "{{ item.key }}"
          sudo: "{{ item.sudo }}"

    management_user_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

    management_user_settings:
      - "{{ user_list }}"

    management_user_list:
      - "{{ management_user_settings }}"

    management_user_list_host: []
    management_user_list_group: []

    sudo_root:
            hosts: ALL
            as: ALL
            commands: ALL
            nopasswd: yes

    sudo_oracle:
            hosts: ALL
            as: ORACLE
            commands: ALL
            nopasswd: yes

  roles:
    - role: user-role-management
      with_items:
        - { name: abc1, comment: "a user", uid: 1300, password: somepw, state: present, key: "{{ lookup('file', '/home/user/SSH/user.name/id_rsa.pub') }}", sudo: "{{ sudo.root }}" }
        - { name: abc2, comment: "b user", state: present, key: "{{ management_user_key }}", sudo: "{{ sudo.oracle }}" }

Is there a simple answer how do you add more management users with this role?

2

1 Answer 1

0

Q: "Is there a simple answer how do you add more management users with this role?"

A: Yes. A list can be appended. For example

- hosts: localhost
  vars:
    my_list: [a, b, c]
    list1: [x, y]
  tasks:
    - set_fact:
        my_list: "{{ my_list + list1 }}"
    - debug:
        var: my_list

gives

    "my_list": [
        "a", 
        "b", 
        "c", 
        "x", 
        "y"
    ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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