在Playbook的Vars部分使用Ansible特殊变量。

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

Use Ansible Special Variables in Playbook Vars Section

问题

我正在尝试创建一个条件变量 - 如果 ansible_useransible,那么我将设置 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: &quot;~/.ssh/ansible/ansible_ed25519&quot;
    ansible_ssh_private_key_file: &quot;{{ private_key_file if ansible_user == &#39;ansible&#39; else &#39;&#39; }}&quot;

  tasks:
    - debug:
        msg: &quot;{{ ansible_ssh_private_key_file }}&quot;

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! =&gt; {&quot;msg&quot;: &quot;The field &#39;private_key_file&#39; has an invalid value, which includes an undefined variable. The error was: &#39;ansible_user&#39; is undefined. &#39;ansible_user&#39; is undefined. &#39;ansible_user&#39; is undefined. &#39;ansible_user&#39; is undefined&quot;}

I am able to query ansible_user in a task further down - anyone know of an alternative to get the -u &lt;user&gt; 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: &quot;ansible_user&quot;
      debug:
        msg: &quot;{{ ansible_user }}&quot;

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] =&gt; {
    &quot;msg&quot;: &quot;ansible&quot;
}

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 的标题中作为事实可用。您可以通过以下方式解决此问题:

  1. 在清单中设置 ansible_user,而不是在命令行上设置它,或者

  2. 通过以下方式将其有效地提升为“真正的事实”:

    - 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:

  1. Setting ansible_user in your inventory instead of on the command line, or

  2. Effectively promoting it to a "real fact" like this:

    - hosts: all
      gather_facts: false
      tasks:
        - set_fact:
            ansible_user: &quot;{{ ansible_user }}&quot;
    
    - hosts: all
    
      vars:
        private_key_file: &quot;~/.ssh/ansible/ansible_ed25519&quot;
        ansible_ssh_private_key_file: &quot;{{ private_key_file if ansible_user == &#39;ansible&#39; else &#39;&#39; }}&quot;
    
      tasks:
        - debug:
            msg: &quot;{{ ansible_ssh_private_key_file }}&quot;
    

huangapple
  • 本文由 发表于 2023年6月12日 21:54:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457372.html
匿名

发表评论

匿名网友

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

确定