skip to Main Content

I have a simple playbook that I run on new managed nodes for Ansible
the playbook has 3 roles : create ansible admin user on destination host , copy ssh key , sets sudo no passwd for ansible user
I have Rhel based nodes and also debian based nodes
for Rhel I use root , for debian root not used by default and I keep it that way so I have a different admin user called sysadmin
I am trying to find a way that the playbook will identify the OS and choose either root or sysadmin user to run the Play , and also use a proper password from a file in ansible vault
thanks
this is the playbook

  • name: init managed node
    user: root
    hosts: init_clients
    become: yes
    gather_facts: yes
    ignore_errors: no
    vars:
    user: ansible-admin
    passwd: password-hash

    roles:

    • create_admin_user
    • set_authorized_key
    • set_no_pass

2

Answers


  1. Chosen as BEST ANSWER

    My issue is that Ansible still needs to run the first Playbook as some user if that user is the same for all systems (root for example).

    That's ok if the user is not the same.

    I have to run the playbook once for Rhel and once for Debian and then change the "user" statement to a differener user.


  2. 100% real code. But if I didn’t have it at my fingertips, I would skip answering this question, as there’s no indication of any effort on your part.

    - name: Set the system user name for Ubuntu
      set_fact:
        linux_system_user: ubuntu
      when: ansible_os_family == 'Debian'
    
    - name: Set the system user name for CentOS
      set_fact:
        linux_system_user: centos
      when: ansible_distribution == 'CentOS'
    
    - name: Set the system user name for RedHat
      set_fact:
        linux_system_user: root
      when: ansible_distribution == 'RedHat'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search