代码之家  ›  专栏  ›  技术社区  ›  kasuke

Ansible变量分配给主机

  •  0
  • kasuke  · 技术社区  · 7 年前

    在这里的第一篇文章中,我遇到了一种情况,我试图分配存储在角色中的变量,在我的例子中: /var/lib/awx/projects/test/playbooks/roles/<<我的角色(&T)&燃气轮机/变量/主。yml公司 但我希望它们由主机分配,因此我尝试在我的主机文件中分配它们,如下所示:

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

    我试过了 host\u变量/ ansible-playbook test_role.yml -i host -f 5 -e guest_vars_file=host_vars/test 但同样的,它不接受我的变量。

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

    我试过了 group\u变量/ 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是否会根据任务中的内容过度使用变量 变量/主。yml公司 我只把 id: station:

    编辑: 因此,解决方法是输入正确的名称 host\u变量 和不将变量放入 角色/my\u角色/vars/main。yml公司 因为它会覆盖你库存的VAR host\u变量 谢谢

    1 回复  |  直到 6 年前
        1
  •  1
  •   damobrisbane    6 年前

    在这种情况下,请确保host\u vars相对于playbook 我的测试。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\u变量 ,则, host\u变量 文件夹与某些角色文件夹无关。考虑到这些文件夹中设置的任何变量可能是针对多个角色的通用变量,或者它们可能与特定主机相关, 非特定角色 提到 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/            #