Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible:覆盖角色默认值_Ansible_Jinja2_Devops - Fatal编程技术网

Ansible:覆盖角色默认值

Ansible:覆盖角色默认值,ansible,jinja2,devops,Ansible,Jinja2,Devops,我有下面的ansible目录布局 inventories/ group_vars/ # common variables production and staging all production/ hosts group_vars/ all # common to all production groupN.yml host_vars/

我有下面的ansible目录布局

inventories/
   group_vars/     # common variables production and staging
      all
   production/
      hosts              
      group_vars/     
         all      # common to all production 
         groupN.yml
      host_vars/
         all     # common to all production 
         hostnameN.yml

   staging/
      hosts               
      group_vars/     
         all      # common to all staging 
         groupN.yml
      host_vars/
         all     # common to all staging 
         hostnameN.yml      

site.yml
production.yml
staging.yml

roles/
    docker/
         defaults
             # let's assume there defaults contain default_docker_version=a.b.c
         tasks
         ...
    webtier/
我的问题是: -如何使用主机特定的值覆盖roles/common/default中的值

也就是说,我希望具有特定于环境的角色的默认值

示例我希望生产主机具有docker版本的x.y.z,而暂存主机具有docker_版本的a.b.c

我尝试将变量添加到主机变量中,但ansible仍然无法找到它


-是否可以对结构布局进行改进?

您可以在特定环境的库存中使用docker版本的价值。然后,在剧本中调用角色时,需要传递该值以覆盖角色中的默认值

下面是通过重写值从playbook调用角色的代码段

 roles:
   - { role: roles/common, docker_version: "{{ docker_version }}" }
docker_version是一个变量,传递给角色以覆盖值及其来自playbook清单的值

请参阅检查ansible中变量的优先级,这非常有用


您可以在特定于环境的资源清册中使用docker版本的值。然后,在剧本中调用角色时,需要传递该值以覆盖角色中的默认值

下面是通过重写值从playbook调用角色的代码段

 roles:
   - { role: roles/common, docker_version: "{{ docker_version }}" }
docker_version是一个变量,传递给角色以覆盖值及其来自playbook清单的值

请参阅检查ansible中变量的优先级,这非常有用

问:如何使用主机特定的值覆盖roles/common/default中的值

答:对于暂存,将变量放入目录中

inventories/staging/host_vars
inventories/production/host_vars
对于生产,将变量放入目录中

inventories/staging/host_vars
inventories/production/host_vars
比如说

$ cat inventories/production/hosts
prod-001
prod-002
prod-003

$ cat inventories/production/hosts/host_vars/prod-001
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-002
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-003
docker_version: "x.y.z"
$ cat inventories/production/hosts
[group_01]
prod-001
prod-002
prod-003

$ cat inventories/production/group_vars/group_01
docker_version: "x.y.z"
$ cat inventories/production/group_vars/all
docker_version: "x.y.z"
ansible-playbook -i inventories/staging/hosts site.yml
使用特定于组的值会更有效。比如说

$ cat inventories/production/hosts
prod-001
prod-002
prod-003

$ cat inventories/production/hosts/host_vars/prod-001
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-002
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-003
docker_version: "x.y.z"
$ cat inventories/production/hosts
[group_01]
prod-001
prod-002
prod-003

$ cat inventories/production/group_vars/group_01
docker_version: "x.y.z"
$ cat inventories/production/group_vars/all
docker_version: "x.y.z"
ansible-playbook -i inventories/staging/hosts site.yml
,或全部。比如说

$ cat inventories/production/hosts
prod-001
prod-002
prod-003

$ cat inventories/production/hosts/host_vars/prod-001
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-002
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-003
docker_version: "x.y.z"
$ cat inventories/production/hosts
[group_01]
prod-001
prod-002
prod-003

$ cat inventories/production/group_vars/group_01
docker_version: "x.y.z"
$ cat inventories/production/group_vars/all
docker_version: "x.y.z"
ansible-playbook -i inventories/staging/hosts site.yml
问:在结构布局上有什么可以改进的地方吗

答:您的情况是生产和暂存库存在不同的目录中。这是必要的。比如说

$ cat inventories/production/hosts
prod-001
prod-002
prod-003

$ cat inventories/production/hosts/host_vars/prod-001
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-002
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-003
docker_version: "x.y.z"
$ cat inventories/production/hosts
[group_01]
prod-001
prod-002
prod-003

$ cat inventories/production/group_vars/group_01
docker_version: "x.y.z"
$ cat inventories/production/group_vars/all
docker_version: "x.y.z"
ansible-playbook -i inventories/staging/hosts site.yml
在这种情况下,将使用目录staging/hosts/host\u vars和staging/hosts/group\u vars中的清单变量。可以对它进行测试

ansible-inventory -i inventories/staging/hosts --list
根据您的用例,还有其他改进,而不是改进

注释

库存/组变量/全部无效 库存/生产/主机变量/所有都是相同的 问:如何使用主机特定的值覆盖roles/common/default中的值

答:对于暂存,将变量放入目录中

inventories/staging/host_vars
inventories/production/host_vars
对于生产,将变量放入目录中

inventories/staging/host_vars
inventories/production/host_vars
比如说

$ cat inventories/production/hosts
prod-001
prod-002
prod-003

$ cat inventories/production/hosts/host_vars/prod-001
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-002
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-003
docker_version: "x.y.z"
$ cat inventories/production/hosts
[group_01]
prod-001
prod-002
prod-003

$ cat inventories/production/group_vars/group_01
docker_version: "x.y.z"
$ cat inventories/production/group_vars/all
docker_version: "x.y.z"
ansible-playbook -i inventories/staging/hosts site.yml
使用特定于组的值会更有效。比如说

$ cat inventories/production/hosts
prod-001
prod-002
prod-003

$ cat inventories/production/hosts/host_vars/prod-001
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-002
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-003
docker_version: "x.y.z"
$ cat inventories/production/hosts
[group_01]
prod-001
prod-002
prod-003

$ cat inventories/production/group_vars/group_01
docker_version: "x.y.z"
$ cat inventories/production/group_vars/all
docker_version: "x.y.z"
ansible-playbook -i inventories/staging/hosts site.yml
,或全部。比如说

$ cat inventories/production/hosts
prod-001
prod-002
prod-003

$ cat inventories/production/hosts/host_vars/prod-001
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-002
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-003
docker_version: "x.y.z"
$ cat inventories/production/hosts
[group_01]
prod-001
prod-002
prod-003

$ cat inventories/production/group_vars/group_01
docker_version: "x.y.z"
$ cat inventories/production/group_vars/all
docker_version: "x.y.z"
ansible-playbook -i inventories/staging/hosts site.yml
问:在结构布局上有什么可以改进的地方吗

答:您的情况是生产和暂存库存在不同的目录中。这是必要的。比如说

$ cat inventories/production/hosts
prod-001
prod-002
prod-003

$ cat inventories/production/hosts/host_vars/prod-001
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-002
docker_version: "x.y.z"

$ cat inventories/production/hosts/host_vars/prod-003
docker_version: "x.y.z"
$ cat inventories/production/hosts
[group_01]
prod-001
prod-002
prod-003

$ cat inventories/production/group_vars/group_01
docker_version: "x.y.z"
$ cat inventories/production/group_vars/all
docker_version: "x.y.z"
ansible-playbook -i inventories/staging/hosts site.yml
在这种情况下,将使用目录staging/hosts/host\u vars和staging/hosts/group\u vars中的清单变量。可以对它进行测试

ansible-inventory -i inventories/staging/hosts --list
根据您的用例,还有其他改进,而不是改进

注释

库存/组变量/全部无效 库存/生产/主机变量/所有都是相同的
它似乎不起作用。我尝试了`roles:`role:docker docker_version:{{{docker_version}}`我尝试了但ansible抛出-在模板化{{docker_version}时发生了未处理的异常。它似乎不起作用。我尝试了`roles:`role:docker docker_version:{{{docker_version}}`我尝试了但ansible抛出-模板化{{docker_version}时发生未处理的异常感谢Vladimir。为什么库存/集团变量/全部无效?如果我想要生产和暂存之间的公共属性-我将如何实现它?哪些主机将使用库存/组变量/所有?当您需要公共属性时,为什么要分开库存?生产主机和临时主机共享一个特定属性。我希望能以某种方式实现它,你愿意分享什么样的偏好?默认的额外变量?例如,在远程触发的playbook开始时使用,您可以构建任何您可以想象的体系结构,如果您想。作为默认共享Hanks Vladimir。为什么库存/集团变量/全部无效?如果我想要生产和暂存之间的公共属性-我将如何实现它?哪些主机将使用库存/组变量/所有?当您需要公共属性时,为什么要分开库存?生产主机和临时主机共享一个特定属性。我希望能以某种方式实现它,你愿意分享什么样的偏好?默认的额外变量?例如,在远程触发的playbook开始时使用,您可以构建任何您可以想象的体系结构,如果您想要。作为默认值共享