Difference between revisions of "Ansible Playbooks"
Jump to navigation
Jump to search
(Created page with "https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html") |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | # Order of vars | ||
+ | - inventory vars | ||
+ | - vars_files in playbook (outside of tasks) | ||
+ | - vars in tasks after os fact discovery | ||
+ | |||
+ | Set host connection to winrm on windows without ssh | ||
+ | |||
+ | And change auth creds via include vars by group name var | ||
+ | |||
+ | You could use template env vars with jinja template too | ||
+ | |||
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html | 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 %} | ||
+ | ``` | ||
+ | |||
+ | ``` | ||
+ | --- | ||
+ | - hosts: "{{hosts}}" | ||
+ | vars: | ||
+ | scripts: | ||
+ | - run.pl | ||
+ | tasks: | ||
+ | - name: win_stat | ||
+ | with_items: scripts | ||
+ | win_stat: path="D:\scripts\{{item}}" | ||
+ | register: "win_result" | ||
+ | when: "'windows' in group_names" | ||
+ | - name: other_stat | ||
+ | with_items: scripts | ||
+ | stat: path="/usr/local/bin/{{item}}" | ||
+ | register: "other_result" | ||
+ | when: "'windows' not in group_names" | ||
+ | - debug: var=win_result | ||
+ | - debug: var=other_result | ||
+ | - with_items: "{{ win_result.results }}" | ||
+ | debug: msg="{{item.item}}" | ||
+ | when: "not (item.skipped | default(false))" | ||
+ | - with_items: "{{ other_result.results}}" | ||
+ | debug: msg="{{item.item}}" | ||
+ | when: "not (item.skipped | default(false))" | ||
+ | ``` |
Latest revision as of 18:30, 20 November 2021
Order of vars
- inventory vars
- vars_files in playbook (outside of tasks)
- vars in tasks after os fact discovery
Set host connection to winrm on windows without ssh
And change auth creds via include vars by group name var
You could use template env vars with jinja template too
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 %}
--- - hosts: "{{hosts}}" vars: scripts: - run.pl tasks: - name: win_stat with_items: scripts win_stat: path="D:\scripts\{{item}}" register: "win_result" when: "'windows' in group_names" - name: other_stat with_items: scripts stat: path="/usr/local/bin/{{item}}" register: "other_result" when: "'windows' not in group_names" - debug: var=win_result - debug: var=other_result - with_items: "{{ win_result.results }}" debug: msg="{{item.item}}" when: "not (item.skipped | default(false))" - with_items: "{{ other_result.results}}" debug: msg="{{item.item}}" when: "not (item.skipped | default(false))"