Ansible 变量分配给主机 [英] Ansible variables assign to host

查看:28
本文介绍了Ansible 变量分配给主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次在这里发帖,我遇到了一个情况,我正在尝试分配存储在角色中的变量,在我的情况下:/var/lib/awx/projects/test/playbooks/roles/<<我的角色 >>/vars/main.yml但我希望它们由主机分配,所以我尝试在我的主机文件中分配它们,如下所示:

first post here, I got a situation, i'm trying to assign variables which are stored in roles, in my case : /var/lib/awx/projects/test/playbooks/roles/<< my roles >>/vars/main.yml but I want them to be assign by host, so I tried to assign them in my hosts files like this :

<代码>[测试]10.102.32.42 id=1 站=红 ...但这不起作用,我的变量没有定义.

[test] 10.102.32.42 id=1 station=red ... but that don't work, my variables are not defined.

我尝试使用 host_vars/ ansible-playbook test_role.yml -i host -f 5 -e guest_vars_file=host_vars/test 但同样的事情,不需要我的变量.

I tried with host_vars/ ansible-playbook test_role.yml -i host -f 5 -e guest_vars_file=host_vars/test but same thing, it doesn't take my variables.

我的测试文件:<代码>编号:1车站:红色

我尝试了 group_vars/ ansible-playbook test_role.yml -i host -f 5 -e guest_vars_file=group_vars/test 我不知道它是否好方法.

I tried with group_vars/ ansible-playbook test_role.yml -i host -f 5 -e guest_vars_file=group_vars/test I don't know if it's the good way to do it.

我尝试了一个简单的 ansible-playbook test_role.yml -i host 和文件在好位置但没有

I tried a simple ansible-playbook test_role.yml -i host and files in good place but no

我尝试通过在主机中分配变量来使用 AWX,但没有奏效.

I tried with AWX by assign variables in my hosts, didn't worked.

当我用 -e 传递变量时,它可以工作,但我的变量必须由主机更改.

When I'm passing variables with -e, it's working but my variables have to change by hosts.

有什么办法吗?或者不可能?我正在使用 ansible 2.4.3.0我想知道当我启动任务时,ansible 是否通过我的 vars/main.yml 中的内容覆盖了我的变量我只放的地方<代码>ID:车站:

Any way to do it ? or it's not possible ? I'm using ansible 2.4.3.0 I was wondering if when I launch the task, ansible overrited my variables by what it is in my vars/main.yml where I only put id: station:

所以解决方案是在 host_vars 和 !不要将变量放在 roles/my_role/vars/main.yml 中,因为它会覆盖 host_vars 中的库存变量.谢谢

So the solution is to put the right name in host_vars AND ! not to put variables in roles/my_role/vars/main.yml because it will override your vars that are stock in host_vars . Thanks

推荐答案

确保 host_vars 与 playbook 相关,在这种情况下 mytest.yml:

Make sure host_vars is relative to playbook, in this case mytest.yml:

user> find roles/mytest
roles/mytest
roles/mytest/tasks
roles/mytest/tasks/main.yml

user> cat roles/mytest/tasks/main.yml 
---

- name: test myvar
  shell: echo "{{ myvar }}" > /tmp/myvar

user> cat hosts2
[test]
10.102.32.42

user> cat host_vars/10.102.32.42 
myvar: "this is 10.102.32.42"

user> cat mytest.yml 
---
- hosts: test
  roles:
    - mytest


user> ansible-playbook --inventory-file=hosts2 mytest.yml 

PLAY [test] *******************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************
ok: [10.0.0.4]

TASK [mytest : test myvar] ****************************************************************************************************
changed: [10.102.32.42]

PLAY RECAP ********************************************************************************************************************
10.102.32.42                   : ok=2    changed=1    unreachable=0    failed=0   

user> ssh 10.102.32.42 "cat /tmp/myvar"
this is 10.102.32.42

请注意,group_varshost_vars 文件夹与某个角色文件夹无关.考虑到这些文件夹中设置的任何变量可能对多个角色是通用的,或者它们可能与特定主机相关,不是特定角色.请参阅Ansible 最佳实践;:

Note that group_vars, host_vars folders are NOT relative to some role folder. Consider that any variables set in these folders may be common against several roles, or they may be related to a specific host, not a specific role. Refer to Ansible Best Practices; :

production                # inventory file for production servers
staging                   # inventory file for staging environment

group_vars/
   group1                 # here we assign variables to particular groups
   group2                 # ""
host_vars/
   hostname1              # if systems need specific variables, put them here
   hostname2              # ""

library/                  # if any custom modules, put them here (optional)
module_utils/             # if any custom module_utils to support modules, put them here (optional)
filter_plugins/           # if any custom filter plugins, put them here (optional)

site.yml                  # master playbook
webservers.yml            # playbook for webserver tier
dbservers.yml             # playbook for dbserver tier

roles/
    common/               # this hierarchy represents a "role"
        tasks/            #
            main.yml      #  <-- tasks file can include smaller files if warranted
        handlers/         #
            main.yml      #  <-- handlers file
        templates/        #  <-- files for use with the template resource
            ntp.conf.j2   #  <------- templates end in .j2
        files/            #

这篇关于Ansible 变量分配给主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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