英文:
Use Ansible Special Variables in Playbook Vars Section
问题
我正在尝试创建一个条件变量 - 如果 ansible_user
是 ansible
,那么我将设置 ansible_ssh_private_key_file
,这样就不需要在命令行上指定它。我之前在我的剧本中使用过 ansible_user
,但似乎在 vars
部分中不起作用:
---
- hosts: all
gather_facts: no
vars:
private_key_file: "~/.ssh/ansible/ansible_ed25519"
ansible_ssh_private_key_file: "{{ private_key_file if ansible_user == 'ansible' else '' }}"
tasks:
- debug:
msg: "{{ ansible_ssh_private_key_file }}"
我可以在 vars
部分中引用 inventory_hostname
,没有问题,但它似乎不喜欢 ansible_user
,因为在运行 ansible-playbook playbook.yml -i hosts.yml -u ansible -k
时会导致以下错误:
FAILED! => {"msg": "The field 'private_key_file' has an invalid value, which includes an undefined variable. The error was: 'ansible_user' is undefined. 'ansible_user' is undefined. 'ansible_user' is undefined. 'ansible_user' is undefined"}
我能够在稍后的任务中查询 ansible_user
- 有人知道如何从命令行获取 -u <user>
的值以在 vars
中使用的替代方法吗?
我已经验证了 ansible_user
在剧本中是可见的,例如:
---
- hosts: all
gather_facts: no
tasks:
- name: "ansible_user"
debug:
msg: "{{ ansible_user }}"
如果我然后运行这个剧本,我会得到以下输出:
ansible-playbook playbook.yml -i hosts.yml -u ansible -k
PLAY [all] ******************************************************************************************************
TASK [ansible_user] ******************************************************************************************
ok: [icct0-s1-aob-01] => {
"msg": "ansible"
}
这证明我确实有一个有效的 `ansible_user` 值,但我只是无法从 `vars` 中引用它。
英文:
I am trying to create a conditional variable - if the ansible_user
is ansible
then I will set the ansible_ssh_private_key_file
so it doesn't need to be specified on the command line. I have previously used ansible_user
within my playbooks, but it appears it doesn't work within the vars
section:
---
- hosts: all
gather_facts: no
vars:
private_key_file: "~/.ssh/ansible/ansible_ed25519"
ansible_ssh_private_key_file: "{{ private_key_file if ansible_user == 'ansible' else '' }}"
tasks:
- debug:
msg: "{{ ansible_ssh_private_key_file }}"
I can reference inventory_hostname
in the vars
section without a problem, but it doesn't like ansible_user
as it results in the following error when running ansible-playbook playbook.yml -i hosts.yml -u ansible -k
:
FAILED! => {"msg": "The field 'private_key_file' has an invalid value, which includes an undefined variable. The error was: 'ansible_user' is undefined. 'ansible_user' is undefined. 'ansible_user' is undefined. 'ansible_user' is undefined"}
I am able to query ansible_user
in a task further down - anyone know of an alternative to get the -u <user>
value from the command line to use in vars
?
I have verified that ansible_user
is visible within the playbook, e.g:
---
- hosts: all
gather_facts: no
tasks:
- name: "ansible_user"
debug:
msg: "{{ ansible_user }}"
If I then run this play I get the following:
ansible-playbook playbook.yml -i hosts.yml -u ansible -k
PLAY [all] **********************************************************************************************************************************************************************
TASK [ansible_user] *************************************************************************************************************************************************************
ok: [icct0-s1-aob-01] => {
"msg": "ansible"
}
This proves I do have a valid value for ansible_user
, but I just can't reference it from within vars
.
答案1
得分: 3
当在命令行中使用 -u
设置 Ansible 用户时,ansible_user
变量将不会在您的 play 的标题中作为事实可用。您可以通过以下方式解决此问题:
-
在清单中设置
ansible_user
,而不是在命令行上设置它,或者 -
通过以下方式将其有效地提升为“真正的事实”:
- hosts: all gather_facts: false tasks: - set_fact: ansible_user: "{{ ansible_user }}" - hosts: all vars: private_key_file: "~/.ssh/ansible/ansible_ed25519" ansible_ssh_private_key_file: "{{ private_key_file if ansible_user == 'ansible' else '' }}" tasks: - debug: msg: "{{ ansible_ssh_private_key_file }}"
英文:
When setting the ansible user on the command line with -u
, the ansible_user
variable won't be available as a fact in the header of your play. You can work around this by:
-
Setting
ansible_user
in your inventory instead of on the command line, or -
Effectively promoting it to a "real fact" like this:
- hosts: all gather_facts: false tasks: - set_fact: ansible_user: "{{ ansible_user }}" - hosts: all vars: private_key_file: "~/.ssh/ansible/ansible_ed25519" ansible_ssh_private_key_file: "{{ private_key_file if ansible_user == 'ansible' else '' }}" tasks: - debug: msg: "{{ ansible_ssh_private_key_file }}"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论