【问题标题】:Ansible variables assign to hostAnsible 变量分配给主机
【发布时间】:2018-05-04 08:35:45
【问题描述】:

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

[test] 10.102.32.42 id=1 station=red ... 但这不起作用,我的变量没有定义。

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

我的测试文件 id: 1 station: red

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

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

我尝试通过在主机中分配变量来使用 AWX,但没有成功。

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

有什么办法吗?还是不可能?我正在使用 ansible 2.4.3.0 我想知道当我启动任务时,ansible 是否用 vars/main.yml 中的内容覆盖了我的变量 我只放的地方 id: station:

编辑: 所以解决方案是将正确的名称放入 host_vars AND !不要将变量放在 roles/my_role/vars/main.yml 中,因为它会覆盖您在 host_vars 中的库存变量。谢谢

【问题讨论】:

    标签: ansible


    【解决方案1】:

    确保 host_vars 与 playbook 相关,在本例中为 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 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/            #
    

    【讨论】:

    • 参考优先级(serverfault.com/questions/806060/…),我相信我的例子是“主机事实”
    • 我还发现 Ansible 最佳实践在剧本定位方面不足,只是说“......顶级剧本被角色分开......”。这是我想补充的:playbook (yml) 文件不驻留在角色文件夹中,plays 相对于 group_vars、hosts_vars 文件夹定位
    • 非常感谢!这就像魅力!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 2019-05-10
    • 2021-03-02
    • 1970-01-01
    • 2017-05-24
    • 2017-03-22
    相关资源
    最近更新 更多