Ansible playbook-wide variable

后端 未结 2 1984
無奈伤痛
無奈伤痛 2021-02-05 06:01

I have a playbook with multiple hosts section. I would like to define a variable in this playbook.yml file that applies only within the file, for example:

vars:
         


        
2条回答
  •  甜味超标
    2021-02-05 06:46

    The set_fact module will accomplish this if group_vars don't suit your needs.

    https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_fact_module.html

    This module allows setting new variables. Variables are set on a host-by-host >basis just like facts discovered by the setup module. These variables will >survive between plays during an Ansible run, but will not be saved across >executions even if you use a fact cache.

    - hosts: db:web
      tasks:
      - set_fact: my_global_var='hello'
    
    - hosts: db
      tasks:
      -shell: echo {{my_global_var}} 
    
    - hosts: web
      tasks:
      -shell: echo {{my_global_var}} 
    

提交回复
热议问题