vars_prompt不起作用-Ansible

时间:2019-06-18 11:01:19

标签: ansible roles prompt

我正在尝试使用ansible角色自动为10台计算机创建主机名。执行剧本时我想要什么,因为这需要手动输入用户名。

我尝试使用vars_prompt模块满足要求。但是在这里,对于单个* .yml文件,我可以看到预期的结果

没有角色-我可以看到输入被带到变量host。但这很好。

#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 }}'

具有角色,<这不起作用> {vars_prompt不起作用}

#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 }}"

这是我的错误

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

我的期望是设置一些参数,这些参数需要在执行Playbook时手动设置为输入。需要在角色中使用此vars_prompt模块。

1 个答案:

答案 0 :(得分:0)

使用 serial: 1 运行第一个游戏,然后输入主机名。第二部剧本(在同一本剧本中)将使用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
相关问题