首页 文章

Ansible变量分配给主机

提问于
浏览
0

首先发布在这里,我遇到了一个情况,我正在尝试分配存储在角色中的变量,在我的情况下: /var/lib/awx/projects/test/playbooks/roles/<< my 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 但同样的事情,它不需要我的变量 .

test fileid: 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传递变量时,它正在工作但我的变量必须由主机更改 .

有办法吗?或's not possible ? I' m使用 ansible 2.4.3.0 我想知道当我启动任务时,ansible通过我在 vars/main.yml 中的内容来覆盖我的变量,我只放了 id: station:

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

1 回答

  • 0

    确保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/            #
    

相关问题