Difference between revisions of "Ansible Playbooks"
Jump to navigation
Jump to search
Line 10: | Line 10: | ||
ansible-playbook -i 127.0.0.1 | ansible-playbook -i 127.0.0.1 | ||
ansible-playbook site.yml --limit datacenter2 -f 10 | ansible-playbook site.yml --limit datacenter2 -f 10 | ||
+ | ``` | ||
+ | |||
+ | |||
+ | Using if or lookup from env in jinja vars | ||
+ | ``` | ||
+ | --- | ||
+ | - name: -Test- | ||
+ | hosts: local | ||
+ | vars: | ||
+ | my_group_var: False | ||
+ | # my_group_var: True | ||
+ | |||
+ | tasks: | ||
+ | |||
+ | - name: Prepare vars file from template. | ||
+ | template: src=/tmp/vars.yaml.j2 | ||
+ | dest=/tmp/vars.yaml | ||
+ | |||
+ | - name: Include vars | ||
+ | include_vars: "/tmp/vars.yaml" | ||
+ | ``` | ||
+ | |||
+ | The content of example jinja template /tmp/vars.yaml.j2 is: | ||
+ | |||
+ | ``` | ||
+ | {% if my_group_var %} | ||
+ | test: | ||
+ | var1: value | ||
+ | var2: value | ||
+ | {% else %} | ||
+ | test: | ||
+ | var1: other_value | ||
+ | var2: other_value | ||
+ | {% endif %} | ||
``` | ``` |
Revision as of 04:10, 18 November 2021
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html
https://docs.ansible.com/ansible/latest/cli/ansible-playbook.html
https://docs.ansible.com/ansible/latest/user_guide/intro_patterns.html
ansible-playbook -i 127.0.0.1 ansible-playbook site.yml --limit datacenter2 -f 10
Using if or lookup from env in jinja vars
--- - name: -Test- hosts: local vars: my_group_var: False # my_group_var: True tasks: - name: Prepare vars file from template. template: src=/tmp/vars.yaml.j2 dest=/tmp/vars.yaml - name: Include vars include_vars: "/tmp/vars.yaml"
The content of example jinja template /tmp/vars.yaml.j2 is:
{% if my_group_var %} test: var1: value var2: value {% else %} test: var1: other_value var2: other_value {% endif %}