Changes

Jump to navigation Jump to search
8,578 bytes added ,  22:08, 3 August 2020
no edit summary
[[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
Ansible "== Installation ==The preferred way to [http://docs.ansible.com/playbooksintro_installation.html Playbooksinstall]" use an 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 descriptive language based on YAMLcontribs.
Ansible can deploy to virtualization environments and public and private cloud environments including VMWare, OpenStack, AWS, Eucalyptus Cloud, KVM, and CloudStack<source lang="bash">cdmkdir ~/bincd bingit clone git://github.com/ansible/ansible.git --recursivecd ./ansiblesource ./hacking/env-setup</source>
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 If you get all the examples and contribs. However, when I ran my first <code>ansible-playbook digitalocean.yml</code>, I got an see this error message :
<pre>
Traceback (most recent call last):
ImportError: No module named ansible.constants
</pre>
Clearly ansible is falling back Be sure to source the env-setup script == Getting Started ==You must source the OS installed version. After I ran 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:# <code>ansible-playbook</code> - to execute an Ansible playbook on the specified systems# <code>ansible</code> - to execute an individual shell command or Ansible module on the specified systems# <code>ansible-vault</code> - (optional) to encrypt or decrypt YAML files that Ansible uses. === Global Config ===<code>export ANSIBLE_HOST_KEY_CHECKING=False</code>or set it in your ~/.ansible.cfg so that as you add new hosts it won't prompt you. Also [https://docs.ansible.com/ansible/intro_getting_started.html#your-first-commands use 'ssh' instead of paramiko] when doing this. === Initialize a Project ==='''Ansible Galaxy''' If you want to do a new project, you can use the <code>ansible-galaxy foo init</code> command which will create the directory and file structure for 'foo' in the current working directory. Also, if you want to install other Ansible Galaxy projects, you can either do it "manually" <code> ansible-galaxy install -r </code> Or, setup a 'requirements.yml' file in your playbook that then gets run by your stack. <ref>https://stackoverflow.com/questions/25230376/how-to-automatically-install-ansible-galaxy-roles</ref> <code>ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options]</code> == 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. 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, [https://github.com/ansible/ansible-modules-core/blob/devel/cloud/digital_ocean/digital_ocean.py digital ocean], 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. To sanitize any variables passed to the shell module, you should use <nowiki>"{{ var | quote }}" instead of just "{{ var }}"</nowiki> to make sure they don't include evil things like semicolons.   == Example Commands ==Note: control verbosity with <code>-vvvv</code># <code>ansible --help</code> display help# <code>ansible --version</code> show version info# <code>ansible -c local -i ~/ansible_hosts -m ping all</code> ping all the hosts in the inventory file# <code>ansible '''-m setup''' wiki.example.com</code> Use the '''[https://docs.ansible.com/ansible/latest/setup_module.html setup]''' module to gather ansible 'facts' (aka [[ansible_variables]]) about that host.# <code>ansible '''localhost''' -m setup -a 'gather_subset=!all'</code> or look at the localhost# <code>ansible all -m setup -a '''"filter=ansible_distribution*"'''</code> use a filter action to see specific variables# <code>ansible localhost -m setup --tree /tmp/facts</code> store all facts in a file 'tree', based on hostname#<code>ansible -m debug -a "var=hostvars['wiki.example.com']" localhost</code> gives you the '[[ansible hostvars]]'# <code>ansible-playbook play1.yml play2.yml</code> Run multiple playbooks# <code>ansible-playbook -i production webservers.yml --tags ntp '''--list-tasks'''</code> confirm what task names would be run if I ran this command and said "just ntp tasks"# <code>ansible-playbook '''--list-tags''' launch.yml</code> see what tags exist in my playbook (the tasks list shows more detail + the tags)# <code>ansible-playbook -i production webservers.yml --limit boston '''--list-hosts'''</code> confirm what hostnames might be communicated with if I was said "limit to boston" <ref>Choosing which host(s) to operate on https://docs.ansible.com/ansible/intro_patterns.html</ref># <code>~/bin/ansible/contrib/inventory/digital_ocean.py --list --pretty --api-token TOKEN_HERE</code> use the DO api to list your droplets (dynamic inventory)# <code>ansible-playbook -vvv launch.yml -l wiki.example.com --user=root -e do_name=wiki.example.com '''--start-at-task'''='remove empty wiki schema from database if it already exists'</code> start at a particular point in the task list# <code>php -r 'var_dump(json_decode(file_get_contents("/tmp/facts/localhost"), true));'</code> look at the json with php (or more interesting tools) With Ansible's Jinja2 filters, you can specify the output of a variable to be 'pretty' <nowiki>{{ some_variable | to_nice_json }}</nowiki> == Variables ==; You have 3 plays in one playbook. Will play 3 be able to reference facts registered in play 1?:: facts, yes, play vars, no: vars associated to the host, persist, vars defined in the play, do not, set_facts, registered vars and gathered facts associate to the host so those do persist for the run my  == Playbooks ==Ansible "[http://docs.ansible.com/playbooks.html Playbooks]" use an easy and descriptive language based on YAML. == Targets ==Ansible can deploy to virtualization environments and public and private cloud environments including VMWare, OpenStack, AWS, Eucalyptus Cloud, KVM, and CloudStack == Best Practices ==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. 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.<nowiki>{{ ansible_eth0["ipv4"]["address"] }} over {{ ansible_eth0.ipv4.address }}</nowiki> because some keywords in Python would conflict Reserved words:* hostvars* group_names* groups* environemnt '''inventory_hostname''' is the name of the hostname as configured in Ansible's inventory host file. '''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 -playbookrelease.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. == 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 Vagrant ==https://docs.ansible.com/ansible/guide_vagrant.html and [[Private:QualityBox/Vagrant]]
== Ansible with MediaWiki ==
== Ansible Docs ==
Some of the docs pages I've visited
 
* https://docs.ansible.com/ansible/playbooks_intro.html
* https://docs.ansible.com/ansible/intro_inventory.html
* http://docs.ansible.com/playbooks_best_practices.html
* http://docs.ansible.com/playbooks_loops.html
* https://docs.ansible.com/ansible/playbooks_conditionals.html
* https://docs.ansible.com/ansible/playbooks_startnstep.html
* https://docs.ansible.com/ansible/playbooks_roles.html#task-include-files-and-encouraging-reuse
* http://docs.ansible.com/YAMLSyntax.html
* https://docs.ansible.com/ansible/become.html
* https://docs.ansible.com/ansible/debug_module.html
* https://docs.ansible.com/ansible/playbooks_debugger.html (<code>strategy:debug</code>)
* https://docs.ansible.com/ansible/playbooks_conditionals.html#sts=The When Statement%C2%B6
== Ansible References ==
* http://tjelvarolsson.com/blog/taking-the-effort-out-of-server-configuration-using-ansible/
* http://tjelvarolsson.com/blog/how-to-create-automated-and-reproducible-work-flows-for-installing-scientific-software/ < with Vagrant
* http://jpmens.net/2012/06/06/configuration-management-with-ansible/
* [http://jinja.pocoo.org/docs/dev/ Jinja] - the template engine for Ansible
* [https://cheat.readthedocs.io/en/latest/ansible/index.html Dan's Cheat Sheets]
{{References}}
[[Category:Virtualization]]
[[Category:Configuration Management]]
[[Category:DevOps]]

Navigation menu