Ansible: Difference between revisions
No edit summary |
update best practices |
||
| Line 96: | Line 96: | ||
== Best Practices == | == Best Practices == | ||
Building Ansible Automation Platform execution environments (EE) | |||
* https://www.redhat.com/architect/ansible-execution-environment-tips | |||
Using Python 3 | |||
* https://docs.ansible.com/ansible/latest/reference_appendices/python_3_support.html | |||
* https://docs.ansible.com/ansible/latest/dev_guide/developing_python_3.html | |||
Use | * Use tags to organize your Ansible work | ||
* Use caching (default is off) to be able to refer to host 'facts' without having to hit each host in a playbook. | |||
The array notation is preferred over the dot notation for accessing variables. | * Use register of [https://docs.ansible.com/ansible/playbooks_variables.html variables] to create more 'facts'. Results vary from module to module. Use -v to see possible values. | ||
* There is an order of precedence with [https://docs.ansible.com/ansible/playbooks_variables.html playbook variables], with role defaults the lowest priority and extra vars the winner. | |||
* The array notation is preferred over the dot notation for accessing variables.<br /> | |||
<nowiki>{{ ansible_eth0["ipv4"]["address"] }} over {{ ansible_eth0.ipv4.address }}</nowiki> because some keywords in Python would conflict | <nowiki>{{ ansible_eth0["ipv4"]["address"] }} over {{ ansible_eth0.ipv4.address }}</nowiki> because some keywords in Python would conflict | ||
Reserved words: | * Reserved words: | ||
* hostvars | ** hostvars | ||
* group_names | ** group_names | ||
* groups | ** groups | ||
* environemnt | ** environemnt | ||
'''inventory_hostname''' is the name of the hostname as configured in Ansible's inventory host file. '''ansible_hostname''' is the discovered hostname | * '''<code>inventory_hostname</code>''' is the name of the hostname as configured in Ansible's inventory host file. '''<code>ansible_hostname</code>''' is the discovered hostname | ||
You can use a variables file to put sensitive data in a different file (one excluded from git). | * You can use a variables file to put sensitive data in a different file (one excluded from git). | ||
<source lang="yaml"> | <source lang="yaml"> | ||
- hosts: all | - hosts: all | ||
| Line 125: | Line 132: | ||
</source> | </source> | ||
You can use variables on the command line (and besides key=value pairs, you can use json or yml) | * You can use variables on the command line (and besides key=value pairs, you can use json or yml) | ||
<source lang="yaml"> | <source lang="yaml"> | ||
--- | --- | ||
| Line 137: | Line 144: | ||
<code>ansible-playbook release.yml --extra-vars "hosts=vipers user=starbuck"</code> | <code>ansible-playbook release.yml --extra-vars "hosts=vipers user=starbuck"</code> | ||
Check [https://www.ansible.com/blog/ansible-performance-tuning Performance Tuning] like enabling <code>pipelining</code> which is off by default. | * Check [https://www.ansible.com/blog/ansible-performance-tuning Performance Tuning] like enabling <code>pipelining</code> which is off by default. | ||
Truthy values should always be expressed as '''one of <code>[false, true]</code>'''. Although the Ansible docs show that [https://docs.ansible.com/ansible/latest/YAMLSyntax.html#yaml-basics you can use several forms of expression for boolean values], and [http://yaml.org/type/bool.html the YAML spec specifies a fuller range of possibilities] described below, the Ansible documentation now also [https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html clarifies that only lowercase 'true' or 'false'] is compatible with [[yamllint]] options. | * Truthy values should always be expressed as '''one of <code>[false, true]</code>'''. Although the Ansible docs show that [https://docs.ansible.com/ansible/latest/YAMLSyntax.html#yaml-basics you can use several forms of expression for boolean values], and [http://yaml.org/type/bool.html the YAML spec specifies a fuller range of possibilities] described below, the Ansible documentation now also [https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html clarifies that only lowercase 'true' or 'false'] is compatible with [[yamllint]] options. | ||
;YAML boolean values, not Ansible: | ;YAML boolean values, not Ansible: | ||
:<syntaxhighlight lang=ebnf> | :<syntaxhighlight lang=ebnf> | ||