How to write dynamic variable in Ansible playbook

前端 未结 3 363
一整个雨季
一整个雨季 2021-01-30 10:51

Based on extra vars parameter I Need to write variable value in ansible playbook

ansible-playbook playbook.yml -e \"param1=value1 param         


        
3条回答
  •  既然无缘
    2021-01-30 11:30

    I am sure there is a smarter way for doing what you want but this should work:

    - name         : Test var
      hosts        : all
      gather_facts : no
      vars:
        myvariable : false
      tasks:
        - name: param1
          set_fact:
            myvariable: "{{param1}}"
          when: param1 is defined
    
        - name: param2
          set_fact:
            myvariable: "{{ param2 if not myvariable else myvariable + ',' + param2 }}"
          when: param2 is defined
    
        - name: param3
          set_fact:
            myvariable: "{{ param3 if not myvariable else myvariable + ',' + param3 }}"
          when: param3 is defined
    
        - name: default
          set_fact:
            myvariable: "default"
          when: not myvariable
    
        - debug:
           var=myvariable
    

    Hope that helps. I am not sure if you can construct variables dynamically and do this in an iterator. But you could also write a small python code or any other language and plug it into ansible

提交回复
热议问题