1

I use multiple hosts and I want to prompt user to provide sudo username and password if ansible_become_user and ansible_become_password is not set in the inventory or playbook

I use following:

inventory file:

os:
  children:
    centos:
      hosts:
        clean_centos_1:
          vars:
          ansible_become_user: root
          ansible_become_password: root
    rocky:
      hosts:
        clean_rocky_1:
    ubuntu:
      hosts:
        clean_ubuntu_1:
    debian:
      hosts:
        clean_debian_1:
    alpine:
      hosts:
        clean_alpine_1:
          vars:
          ansible_become_user: root
          ansible_become_password: root
  vars:
    ansible_user: test 
    ansible_password: test

playbook

- name: "PLAY1"
  hosts: all
  gather_facts: no

  tasks:
## Identify ansible_become_user if present
#------------------------------------------------------
  - name: "Test if ansible_become_user is empty" 
    debug:
      var: ansible_become_user
    register: result
    when: ansible_become_user | length > 0
    ignore_errors: yes

  - name: "Set status_ansible_become_user"
    set_fact:
      status_ansible_become_user: "{{ status_ansible_become_user|default({}) | combine( { ansible_host: 'absent' if result.failed is true else 'present' }) }}"


## Get user input
#------------------------------------------------------
  - name: "User-input: <ansible_become_user>"
    pause:
      prompt: "\nEnter root username or sudo username for host: {{ansible_host}}"
    when: item == 'absent'
    loop: "{{status_ansible_become_user.values()}}"

  - set_fact:
      ansible_become_user: "{{ result.user_input }}"
    when: result.failed is false


  - debug:
      var: item == 'absent'
    loop: "{{status_ansible_become_user.values()}}"

In above example because clean_centos_1 is first in the row and it is set with ansible_become_user and ansible_become_passwor, other hosts are skipped as following and there is no prompt

TASK [User-input: <ansible_become_user>] **********************************************************************************************************************************************************************************************************************************
skipping: [clean_centos_1] => (item=present)

TASK [set_fact] ***********************************************************************************************************************************************************************************************************************************************************
fatal: [clean_centos_1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'user_input'\n\nThe error appears to be in '/git/ansible/role/rar.pkg.python/playbook/test.yml': line 28, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - set_fact:\n    ^ here\n"}
skipping: [clean_rocky_1]
skipping: [clean_debian_1]
skipping: [clean_ubuntu_1]
fatal: [clean_alpine_1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'user_input'\n\nThe error appears to be in '/git/ansible/role/rar.pkg.python/playbook/test.yml': line 28, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - set_fact:\n    ^ here\n"}


Question:
How to properly verify if ansible_become_user and ansible_become_password were set and if not how to prompt use to provide ansible_become_user and ansible_become_password which might be different for each host?

1 Answer 1

1

For example, in the playbook below

- hosts: all
  gather_facts: false
  vars:
    ab: "{{ hostvars|dict2items|json_query(_query) }}"
    _query: "[].{host: key,
                 ansible_become_user: value.ansible_become_user,
                 ansible_become_password: value.ansible_become_password}"
    ab_hosts: "{{ ab|map(attribute='host')|list }}"
    ab_dict: "{{ dict(ab_hosts|zip(ab)) }}"
  tasks:
    - block:
        - include_tasks: enter_ansible_become.yml
          loop: "{{ ab }}"
        - set_fact:
            ab_dict: "{{ ab_dict|combine(ab_update, recursive=True) }}"
      run_once: true
    - add_host:
        groups: test
        hostname: "{{ item.key }}"
        ansible_become_user: "{{ item.value.ansible_become_user }}"
        ansible_become_password: "{{ item.value.ansible_become_password }}"
      loop: "{{ ab_dict|dict2items }}"

- hosts: test
  gather_facts: false
  tasks:
    - debug:
        msg: |-
          ansible_become_user: {{ ansible_become_user }}
          ansible_become_password: {{ ansible_become_password }}

Create a list and a dictionary of the hosts and the variables

  ab:
  - ansible_become_password: root
    ansible_become_user: root
    host: clean_centos_1
  - ansible_become_password: null
    ansible_become_user: null
    host: clean_rocky_1
  - ansible_become_password: null
    ansible_become_user: null
    host: clean_ubuntu_1
  - ansible_become_password: null
    ansible_become_user: null
    host: clean_debian_1
  - ansible_become_password: root
    ansible_become_user: root
    host: clean_alpine_1
  ab_dict:
    clean_alpine_1:
      ansible_become_password: root
      ansible_become_user: root
      host: clean_alpine_1
    clean_centos_1:
      ansible_become_password: root
      ansible_become_user: root
      host: clean_centos_1
    clean_debian_1:
      ansible_become_password: null
      ansible_become_user: null
      host: clean_debian_1
    clean_rocky_1:
      ansible_become_password: null
      ansible_become_user: null
      host: clean_rocky_1
    clean_ubuntu_1:
      ansible_become_password: null
      ansible_become_user: null
      host: clean_ubuntu_1

Run once, iterate the list in the block, and include the tasks from the file enter_ansible_become.yml

shell> cat enter_ansible_become.yml
- block:
    - pause:
        prompt: "[{{ item.host }}] Enter ansible_become_user"
      register: result
    - set_fact:
        ab_update: "{{ ab_update|d({})|combine(update, recursive=True) }}"
      vars:
        update: "{{ {item.host: {'ansible_become_user': result.user_input}} }}"
  when: not item.ansible_become_user

- block:
    - pause:
        prompt: "[{{ item.host }}] Enter ansible_become_password"
      register: result
    - set_fact:
        ab_update: "{{ ab_update|d({})|combine(update, recursive=True) }}"
      vars:
        update: "{{ {item.host: {'ansible_become_password': result.user_input}} }}"
  when: not item.ansible_become_password

Optionally, set the parameter echo: false to hide the passwords. The default value is true. See echo.


For example, enter the values of the missing variables

TASK [pause] *********************************************************************************
[pause]
[clean_rocky_1] Enter ansible_become_user:
admin1^Mok: [clean_centos_1]

TASK [set_fact] ******************************************************************************
ok: [clean_centos_1]

TASK [pause] *********************************************************************************
[pause]
[clean_rocky_1] Enter ansible_become_password:
123^Mok: [clean_centos_1]

TASK [set_fact] ******************************************************************************
ok: [clean_centos_1]

TASK [pause] *********************************************************************************
[pause]
[clean_ubuntu_1] Enter ansible_become_user:
admin2^Mok: [clean_centos_1]

TASK [set_fact] ******************************************************************************
ok: [clean_centos_1]

TASK [pause] *********************************************************************************
[pause]
[clean_ubuntu_1] Enter ansible_become_password:
456^Mok: [clean_centos_1]

TASK [set_fact] ******************************************************************************
ok: [clean_centos_1]

TASK [pause] *********************************************************************************
[pause]
[clean_debian_1] Enter ansible_become_user:
admin3^Mok: [clean_centos_1]

TASK [set_fact] ******************************************************************************
ok: [clean_centos_1]

TASK [pause] *********************************************************************************
[pause]
[clean_debian_1] Enter ansible_become_password:
789^Mok: [clean_centos_1]

TASK [set_fact] ******************************************************************************
ok: [clean_centos_1]

This will create the dictionary ab_update

  ab_update:
    clean_debian_1:
      ansible_become_password: '789'
      ansible_become_user: admin3
    clean_rocky_1:
      ansible_become_password: '123'
      ansible_become_user: admin1
    clean_ubuntu_1:
      ansible_become_password: '456'
      ansible_become_user: admin2

Still in the block, combine the dictionaries

  ab_dict:
    clean_alpine_1:
      ansible_become_password: root
      ansible_become_user: root
      host: clean_alpine_1
    clean_centos_1:
      ansible_become_password: root
      ansible_become_user: root
      host: clean_centos_1
    clean_debian_1:
      ansible_become_password: '789'
      ansible_become_user: admin3
      host: clean_debian_1
    clean_rocky_1:
      ansible_become_password: '123'
      ansible_become_user: admin1
      host: clean_rocky_1
    clean_ubuntu_1:
      ansible_become_password: '456'
      ansible_become_user: admin2
      host: clean_ubuntu_1

In the next task use module add_host and create dynamic group test. Use this group in the next play. The variables should be declared properly

PLAY [test] **********************************************************************************

TASK [debug] *********************************************************************************
ok: [clean_centos_1] => 
  msg: |-
    ansible_become_user: root
    ansible_become_password: root
ok: [clean_rocky_1] => 
  msg: |-
    ansible_become_user: admin1
    ansible_become_password: 123
ok: [clean_ubuntu_1] => 
  msg: |-
    ansible_become_user: admin2
    ansible_become_password: 456
ok: [clean_debian_1] => 
  msg: |-
    ansible_become_user: admin3
    ansible_become_password: 789
ok: [clean_alpine_1] => 
  msg: |-
    ansible_become_user: root
    ansible_become_password: root
1
  • Vladimir this is great example, Thank you very much for your help again! Apr 27, 2022 at 8:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .