Is it possible to define playbook-global variables in Ansible?

前端 未结 4 2006
独厮守ぢ
独厮守ぢ 2021-02-01 12:44

I have a large Ansible playbook where Docker images are built when running it. I am using an increasing number as the tag to version them. Currently, I have to specify this in e

4条回答
  •  难免孤独
    2021-02-01 13:30

    I know this might not answer OP's query exactly, but for people googling to set global variables(lists/arrays) so that they don't have to set them in each task this might be pretty handy.

    Its surprisingly simple, Say you have a list of domains that you need to pass, I named the variable goofy just to signify that it could be anything.

    - hosts: [yourhostlistgoeshere]
      remote_user: root
      vars:
        domainslisto:
           - site1.com
           - website2.xyz
    
    ....
    
    
    
      tasks:
        - name: copy vhosts
          template: src=virtualhost.conf dest=/etc/apache2/sites-available/{{ item }}.conf
          with_items: "{{ domainslisto }}"
    
        - name: a2ensite {{ item }}
          command: a2ensite {{ item }}
          with_items: "{{ domainslisto }}"
    

提交回复
热议问题