无法使用从暂停注册的变量分配的变量 [英] Could not use variable assigned from variable registred from pause

查看:77
本文介绍了无法使用从暂停注册的变量分配的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ansible中使用条件,并且已经使用pause模块提示输入变量,但是我无法在剩余任务中使用该变量.我想根据最终用户插入的值来更改DNS服务器IP,但这没有用.

I'm using conditions in Ansible, and I have used the pause module to prompt for a variable, but I m not being able to use that variable for my remaining task. I wanted to change the DNS Server IP according to the value inserted from the end-user, but it didn't work.

代码:

 - name: os system
   pause:
    prompt: |
     Which os do you want to use?
      1- Windows Server
      2- CentOS_7
      3- CentOs_8
      4- Ubuntu
      5- Others
   register: os_system
 - name: Rename_PC
   pause:
    prompt: Enter New PC Name
   register: rename
   when: os_system.user_input == "1"
 - name: Domain Decision
   pause:
    prompt: Do you want your PC in Domain
   register: decision
   when: os_system.user_input == "1"
 - name: Domain Name
   pause:
     prompt: Enter Domain Name
   register: dc_name
   when: os_system.user_input == "1" and decision.user_input == "yes"
 - name: DNS IP
   pause:
     prompt: Enter DNS IP
   register: d_ip
   when: os_system.user_input == "1"  and decision.user_input "yes"
 - name: Domain DNS
   vmware_vm_shell:
    hostname: "{{ vc_name }}"
    username: "{{ myuser }}"
    password: "{{ mypass }}"
    validate_certs: no
    vm_username: Administrator
    vm_password: '{{ guest_password}}'
    vm_id: "{{ guest_name  }}"
    vm_shell: 'c:\windows\system32\windowspowershell\v1.0\powershell.exe'
    vm_shell_args:  '-command "(Set-DnsClientServerAddress -InterfaceAlias Ethernet0 -ServerAddress {{ d_ip }})"'
    wait_for_process: yes
   when: os_system.user_input == "1" and decision.user_input | bool
   delegate_to: localhost

如您所见,我在输入DNS IP 中以d_ip的形式输入了DNS ip,但是在下面尝试使用{{ d_ip }}时,它没有更改DNS IP,显示提示时,我已经迷惑了上面.

As you can see I have entered the DNS ip as d_ip in Enter DNS IP, but down below when I tried to use {{ d_ip }} it didn't change the DNS IP, that I have enetered above when the prompt was being displayed.

推荐答案

您丢失了pause

You are missing the fact that the return from pause is a dictionary and that the user input is stored in the user_input field.

因此,您的解决方法是仅使用d_ip.user_input而不是简单地使用d_ip:

So your fix is just to use d_ip.user_input instead of simply d_ip:

vm_shell_args: '-command "(Set-DnsClientServerAddress -InterfaceAlias Ethernet0 -ServerAddress {{ d_ip.user_input }})"'


请记住,这与您在上面的when条件下确实使用过的行为相同,实际上您在其中使用了user_input字段:


Mind that it is just the same behaviour as you already used on the when condition above, where you are indeed using the user_input field:

when: os_system.user_input == "1"

这篇关于无法使用从暂停注册的变量分配的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆