-
-
Save phgrau/4048239 to your computer and use it in GitHub Desktop.
a quick and dirty tips-n-tricks for ansible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Variables | |
========== | |
predefined variables : | |
- inventory_hostname (fqdn) (normally the same as ansible.fqdn) | |
- inventory_hostname_short | |
To know the return codes returned by ansible modules, just use plain ansible -vvv to see them : | |
ansible -i ~/ansible/arrfab.net/hosts/hosts.cfg -vvv -m copy -a 'src=files/sysinfo dest=/etc/sysinfo' tungstene.arrfab.net | |
tungstene.arrfab.net | success >> { | |
"changed": true, | |
"daisychain_args": "src=files/sysinfo dest=/etc/sysinfo src=/var/tmp/ansible-1351514206.18-224994873189361/sysinfo", | |
"dest": "/etc/sysinfo", | |
"group": "root", | |
"md5sum": "e5e0af8c573543d6beb837ce2ef3e019", | |
"mode": "0644", | |
"owner": "root", | |
"path": "/etc/sysinfo", | |
"secontext": "root:object_r:etc_t", | |
"src": "/var/tmp/ansible-1351514206.18-224994873189361/sysinfo", | |
"state": "file" | |
} | |
so you know you can use ${status.changed} as a variable for logging/mails, etc... | |
example : | |
action: template ... | |
register: mytemplate | |
action: blablah | |
only_if: '${mytemplate.changed}' #other way of saying only_if: "'${mytemplate.changed}' == 'True'" | |
If your variables is a list containing one iteam on each line , you can use | |
register: users | |
and used ${users.stdout_lines} | |
only_if | |
========= | |
Test if a variable is defined | |
only_if: "is_set('${ansible_default_ipv4.gateway}')" | |
String comparison | |
only_if: "'$declared_user' == 'arrfab'" | |
Misc | |
only_if: "'$ansible_distribution_version'.startswith('5.')" | |
only_if: "'${blah}' == 'blah' or '${blah}' == 'otherblah'" | |
only_if: "'${blah}' in ('blah', 'otherblah')" | |
Templates | |
========== | |
You can just use variables in jinja2 templates like this : ${my_variable} (attention, variables can't contain . as in my.variable ! | |
Some default variables can be used, like {{ ansible_managed }}, $template_host, $template_uid and $template_path | |
a for loop : | |
{% for a in name_of_your_list %} | |
blabla | |
{ %endfor %}(name of your list being defined somewhere else in the vars section like name_of_your_list: [one,two,three] | |
{% if 'webserver' in group_names %} | |
# some part of a configuration file that only applies to webservers | |
{% endif %} | |
{% for host in groups['app_servers'] %} | |
# something that applies to all app servers. | |
{{ hostvars[host].ansible_eth0.ipv4.address }} | |
{% endfor %} | |
# resolv.in by {{ name | upper }} | |
# for {{ ansible_eth0.ipv4.address }} | |
{% if domain is defined -%} | |
domain {{ domain }} | |
{% endif -%} | |
{% for ns in resolvers -%} | |
nameserver {{ ns }} | |
{% endfor %} | |
{% if is_cluster_member == True %} | |
This machine is member of a cluster | |
{% else %} | |
{% endif %} | |
Using nesting variables: trying to show ansible_eth0.macaddress, but eth0 itself being a variable defined in the playbook or vars_prompt (user_interface) | |
user interface: {{ user_interface }} | |
{% set iface = 'ansible_' + user_interface %} | |
show: iface = {{ iface }} | |
show: inventory_hostname = {{ inventory_hostname }} | |
{% set ifacedata = hostvars[inventory_hostname][iface] %} | |
mac address of {{ iface }} is {{ ifacedata.macaddress }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment