ansible-playbook 从命令行传递额外变量

huangapple go评论78阅读模式
英文:

ansible-playbook extra vars passing from command line

问题

我的Playbook如下:

---

- name: 安装和配置AD身份验证
  hosts: test
  become: yes
  become_user: root

  vars:
    hostname: "{{ host_name }}"
   
  vars_prompt:
    - name: "bind_password"
      prompt: "xxx.local\\user的密码"
      private: yes

  tasks:

    - name: Ansible提示示例。
      debug:
        msg: "{{ bind_password }}"

    - name: Ansible提示示例。
      debug:
        msg: "{{ host_name }}"

我正在使用以下命令传递变量:

ansible-playbook hostname_set.yml --extra-vars "host_name= 'xxx.xxx.local'"

但我没有得到我用于设置主机名的确切变量值。

xxx.xxx\user的密码:
PLAY [安装和配置AD身份验证]
TASK [收集事实]
  ok: [x.x.x.x]
TASK [Ansible提示示例。]
  ok: [x.x.x.x] => {
        "msg": "wel"
}
TASK [Ansible提示示例。]
  ok: [x.x.x.x] => {
        "msg": ""
}
TASK [设置主机名]
  changed: [x.x.x.x]
PLAY RECAP
x.x.x.x : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
英文:

My playbook looks like:

---

- name: Install and configure AD authentication
  hosts: test
   become: yes
   become_user: root

  vars:
    hostname: "{{ host_name }}"
   

  vars_prompt:
    - name: "bind_password"
      prompt: "Password for xxx.local\\user"
      private: yes

  tasks:

    - name: Ansible prompt example.
      debug:
        msg: "{{ bind_password }}"

    - name: Ansible prompt example.
      debug:
        msg: "{{ host_name }}"

and i am using below command to pass the variable
ansible-playbook hostname_set.yml --extra-vars "host_name= 'xxx.xxx.local'"

but i am not getting exact variable value what i am using for setting up hostname.

Password for xxx.xxx\user:
PLAY [Install and configure AD authentication] 
TASK [Gathering Facts] 
 ok: [x.x.x.x]
TASK [Ansible prompt example.] 
     ok: [x.x.x.x] => {
        "msg": "wel"
}
TASK [Ansible prompt example.] 
ok: [x.x.x.x] => {
    "msg": ""
}
 TASK [Setup the hostname] 
 changed: [x.x.x.x]
 PLAY RECAP 
 x.x.x.x              : ok=4    changed=1    unreachable=0    failed=0    
 skipped=0    rescued=0    ignored=0

答案1

得分: 1

你的命令行中有一个额外的空格,这会影响ansible解释额外变量的方式。只需移除它:

--extra-vars "host_name='xxx.xxx.local'"

请注意,你甚至不需要那么多的引号。以下内容也应该如预期地工作:

--extra-vars host_name=xxx.xxx.local

英文:

You have an extra space in your command line that ruins how ansible is interpreting the extra vars. Just remove it:

--extra-vars "host_name='xxx.xxx.local'"

Note that you don't even need all those quotes. The following should also work as expected:

--extra-vars host_name=xxx.xxx.local

huangapple
  • 本文由 发表于 2020年1月3日 23:29:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定