Difference between revisions of "Ansible"

From Freephile Wiki
Jump to navigation Jump to search
(add example commands)
(Install, Modules, Best practices)
Line 1: Line 1:
 
[[wp:Ansible_(software)]]  is an open-source software platform for configuring and managing computers. It combines multi-node software deployment, ad hoc task execution, and configuration management.  Written in Python, it is packaged by [[RedHat]].  As of July 2016, we're using Ansible 2.2.0
 
[[wp:Ansible_(software)]]  is an open-source software platform for configuring and managing computers. It combines multi-node software deployment, ad hoc task execution, and configuration management.  Written in Python, it is packaged by [[RedHat]].  As of July 2016, we're using Ansible 2.2.0
  
 +
== Installation ==
 +
The preferred way to [http://docs.ansible.com/intro_installation.html install] is to just <code>git clone</code> the source.  Having the source makes it easy to upgrade, and it's self-contained, plus best of all you get all the examples and contribs. 
 +
If you see this error message:
 +
<pre>
 +
Traceback (most recent call last):
 +
  File "/usr/local/bin/ansible-playbook", line 44, in <module>
 +
    import ansible.constants as C
 +
ImportError: No module named ansible.constants
 +
</pre>
 +
Be sure to source the env-setup script
 +
 +
== Getting Started ==
 +
You must source the environment setup script to begin using Ansible (assuming you are running from a git checkout) <code>source ~/bin/ansible/hacking/env-setup</code>
 
Ansible provides three main commands:
 
Ansible provides three main commands:
 
# <code>ansible-playbook</code> - to execute an Ansible playbook on the specified systems
 
# <code>ansible-playbook</code> - to execute an Ansible playbook on the specified systems
Line 8: Line 21:
 
== Modules ==
 
== Modules ==
 
Ansible comes with [https://docs.ansible.com/ansible/modules_by_category.html over 200 modules] that you should get familiar with in order to use the system effectively.
 
Ansible comes with [https://docs.ansible.com/ansible/modules_by_category.html over 200 modules] that you should get familiar with in order to use the system effectively.
 +
 +
 +
There are a bunch of modules in Ansible, like the [https://docs.ansible.com/ansible/mysql_db_module.html MySQL module], the [https://docs.ansible.com/ansible/monit_module.html Monit module], or the [https://docs.ansible.com/ansible/file_module.html File module] and other interesting modules like jabber, mail, sendgrid, dpkg_selections, composer, yum, redhat_subscription, the [https://docs.ansible.com/ansible/authorized_key_module.html authorized_key module] for working with SSH keys, and a whole section of [https://docs.ansible.com/ansible/list_of_system_modules.html system modules].
 +
 +
 +
You can use the '''command module''' (secure but simple) or the '''[https://docs.ansible.com/ansible/shell_module.html shell module]'''.  The latter may be useful if you need to run bash explicitly (defaults to /bin/sh); or anytime you need $HOME and redirection.
 +
 +
 +
  
 
== Example Commands ==
 
== Example Commands ==
Line 33: Line 55:
 
Ansible can deploy to virtualization environments and public and private cloud environments including VMWare, OpenStack, AWS, Eucalyptus Cloud, KVM, and CloudStack
 
Ansible can deploy to virtualization environments and public and private cloud environments including VMWare, OpenStack, AWS, Eucalyptus Cloud, KVM, and CloudStack
  
== Installation ==
+
== Best Practices ==
The preferred way to [http://docs.ansible.com/intro_installation.html install] is to just <code>git clone</code> the sourceHaving the source makes it easy to upgrade, and it's self-contained, plus best of all you get all the examples and contribsHowever, when I ran my first <code>ansible-playbook digitalocean.yml</code>, I got an error message
+
Use tags to organize your Ansible work
<pre>
+
 
Traceback (most recent call last):
+
Use caching (default is off) to be able to refer to host 'facts' without having to hit each host in a playbook.
   File "/usr/local/bin/ansible-playbook", line 44, in <module>
+
 
    import ansible.constants as C
+
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.
ImportError: No module named ansible.constants
+
 
</pre>
+
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.
Clearly ansible is falling back to the OS installed version.  After I ran <code>source ~/bin/ansible/hacking/env-setup</code>, then I was able to run my ansible playbook
+
 
 +
The array notation is preferred over the dot notation for accessing variables.
 +
{{ ansible_eth0["ipv4"]["address"] }} over {{ ansible_eth0.ipv4.address }} because some keywords in Python would conflict
 +
 
 +
Reserved words:
 +
* hostvars
 +
* group_names
 +
* groups
 +
* environemnt
 +
 
 +
'''inventory_hostname''' is the name of the '''ansible_hostname''' is the discovered hostname
 +
 
 +
You can use a variables file to put sensitive data in a different file (one excluded from git).
 +
<source lang="yaml">
 +
- hosts: all
 +
  remote_user: root
 +
  vars:
 +
    favcolor: blue
 +
  vars_files:
 +
    - /vars/top_secret.yml
 +
</source>
 +
 
 +
You can use variables on the command line (and besides key=value pairs, you can use json or yml)
 +
<source lang="yaml">
 +
---
 +
 
 +
- hosts: '{{ hosts }}'
 +
   remote_user: '{{ user }}'
 +
 
 +
  tasks:
 +
    - ...
 +
</source>
 +
<code>ansible-playbook release.yml --extra-vars "hosts=vipers user=starbuck"</code>
 +
 
 +
== Scope ==
 +
Ansible has 3 main scopes:
 +
 
 +
'''Global''': this is set by config, environment variables and the command line
 +
'''Play''': each play and contained structures, vars entries, include_vars, role defaults and vars.
 +
'''Host''': variables directly associated to a host, like inventory, facts or registered task outputs
  
 
== Ansible with MediaWiki ==
 
== Ansible with MediaWiki ==

Revision as of 23:37, 15 September 2016

  1. Choosing which host(s) to operate on https://docs.ansible.com/ansible/intro_patterns.html