0

I'm trying to automate hostname creation for 10x machines using ansible roles. What I want when executing playbook, as this wait for enter the user name manually.

I tried with vars_prompt module for satisfying the requirement. But here for a single *.yml file, I'm able to see the expected results

Without role - I can see the input is taken to variable host. but this works fine.

#host.yml . 
---
- hosts: ubuntu
  user: test
  sudo: yes
  vars_prompt:
    - name: host
      prompt: "Specify host name?"
      private: no

  tasks: 
    - debug:
        msg: ' log as {{ host }}'
    - name: Changing hostname 
      hostname:
        name: '{{ host }}'

With role, < this is not working > { vars_prompt not working }

#role.yml
---
- hosts: ubuntu
  user: test
  sudo: yes
  roles:
#    - hostname

#hostname/tasks/main.yml
- name: testing prompt 
  vars_prompt: 
    - name: host
      prompt: "Specify host name?"

- name: Ansible  prompt example
  debug:
    msg: "{{ host }}"

#Changing hostname
- name: Changing hostname
  hostname:
  name: "{{ host }}"

Here I'm getting error as

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to have been in '/root/roles/hostname/tasks/main.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: testing prompt
  ^ here

My expectation is to set some parameters need to set manually as input while the execution of playbook. Need to work this vars_prompt module in role.

user183980
  • 274
  • 3
  • 12
  • 2
    the error is expected as `roles` in ansible expected list of tasks and `vars_prompt` is not a task. Its is pre-task for ansible and have to be included in the ansible playbook itself. For your requirement I believe encryption is not required ? – error404 Jun 18 '19 at 11:10
  • yes encryption not needed. When I pass this in the main playbook, as the variable which is not taking in effect. I think for module "hostname" this is not reflecting. As a test, I just replaced by shell module, but it takes the input where I specified initially . Any idea how to pass input or extra args to hostname module ? – user183980 Jun 18 '19 at 11:31
  • Now i got that --extra-args will works for shell and not for "hostname" module. Any idea how can I pass hostname variable as input for these roles ? – user183980 Jun 18 '19 at 11:33
  • The first example (host.yml) is not working fine. `vars_prompt` executes only once and the same `host` is used for all hosts in the `[ubuntu]` group. – Vladimir Botka Jun 18 '19 at 13:02
  • thanks now I'm able to add vars_prompt in the main.yml file and its reflected fine, thanks – user183980 Jun 18 '19 at 15:59

1 Answers1

0

Run 1st play with serial: 1 and enter the hostnames. 2nd play (in the same playbook) will use the facts.

- hosts: ubuntu
  serial: 1
  user: test
  sudo: yes
  tasks:
    - pause:
        prompt: "Specify hostname for {{ inventory_hostname }}?"
        echo: yes
      register: result
    - set_fact:
        host: "{{ result.user_input }}"

- hosts: ubuntu
  user: test
  sudo: yes
  roles:
    - hostname
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • Thanks @vladimir for the suggestion, I'm able to suceed with vars_prompt module. Sure I will try with set_fact . – user183980 Jun 18 '19 at 16:01