<commit_before>Ansible Modules
===============

Ansible ships with a number of modules (called the 'module library') 
that can be executed directly on remote hosts or through :doc:`playbooks`.
Users can also write their own modules.   These modules can control system
resources, like services, packages, or files (anything really), or 
handle executing system commands.  

Let's review how we execute three different modules from the command line::

    ansible webservers -m service -a "name=httpd state=running"
    ansible webservers -m ping
    ansible webservers -m command -a "/sbin/reboot -t now"

Each module supports taking arguments.  Nearly all modules take ``key=value`` 
arguments, space delimited.  Some modules take
no arguments, and the command/shell modules simply take the string
of the command you want to run.

From playbooks, Ansible modules are executed in a very similar way::

    - name: reboot the servers
      action: command /sbin/reboot -t now 

All modules technically return JSON format data, though if you are using the
command line or playbooks, you don't really need to know much about
that.  If you're writing your own module, you care, and this means you do
not have to write modules in any particular language -- you get tho choose.

Most modules other than command are `idempotent`, meaning they will seek
to avoid changes to the system unless a change needs to be made.  When using Ansible
playbooks, these modules can trigger 'change events'.  Unless otherwise
noted, any given module does support change hooks.

Let's see what's available in the Ansible module library, out of the box:

.. _apt:

apt
```

Manages apt-packages (such as for Debian/Ubuntu).

*pkg*:

* A package name or package specifier with version, like name-1.0

*state*:

* Can be either 'installed', 'removed', or 'latest'.  The default is 'installed'.

Example action from Ansible :doc:`playbooks`::

    apt pkg=foo ensure=removed
    apt pkg=foo ensure=installed
    apt pkg=foo ensure=latest

NOTE: the apt module cannot currently request installation of a specific software version, as the yum
module can.  This should be available in a future release.

.. _command:

command
```````

The command module takes the command name followed by a list of
arguments, space delimited.  

If you want to run a command through the shell (say you are using
'<', '>', '|', etc), you actually want the 'shell' module instead.  
The 'command' module is much more secure as it's not affected by the user's environment.

The given command will be executed on all selected nodes.  It will not
be processed through the shell, so variables like "$HOME" and 
operations like "<", ">", "|", and "&" will not work.  As such, all
paths to commands must be fully qualified.

This module does not support change hooks and returns the return code
from the program as well as timing information about how long the
command was running for.

Example action from Ansible :doc:`playbooks`::

    command /sbin/shutdown -t now

If you only want to run a command if a certain file does not exist, you can do the
following::

    command /usr/bin/make_database.sh arg1 arg2 creates=/path/to/database

The `creates=` option will not be passed to the executable.


.. _copy:

copy
````

The copy module moves a file on the local box to remote locations.  In addition to the options
listed below, the arguments available to the `file` module can also be passed to the copy
module.

*src*:

* Local path to a file to copy to the remote server.  This can be an
  absolute or relative path.


*dest*:

* Remote absolute path where the file should end up.

This module also returns md5sum information about the resultant file.

Example action from Ansible :doc:`playbooks`::

    copy src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644


.. _facter:

facter
``````

Runs the discovery program 'facter' on the remote system, returning
JSON data that can be useful for inventory purposes.  

Requires that 'facter' and 'ruby-json' be installed on the remote end.

This module is informative only - it takes no parameters & does not
support change hooks, nor does it make any changes on the system.
Playbooks do not actually use this module, they use the :ref:`setup`
module behind the scenes.


fetch
`````

This module works like 'copy', but in reverse.  It is used for fetching files
from remote machines and storing them locally in a file tree, organized by hostname.

*src*:

* The file on the remote system to fetch.  This needs to be a file, not a directory. Recursive fetching may be supported later.

*dest*:

* A directory to save the file into.  For example, if the 'dest' directory is '/foo', a src file named '/tmp/bar' on host 'host.example.com', would be saved into '/foo/host.example.com/bar'.

The fetch module is a useful way to gather log files from remote systems.  If you require
fetching multiple files from remote systems, you may wish to execute a tar command and
then fetch the tarball.


file
````

Sets attributes of files, symlinks, and directories, or removes files/symlinks/directories. 
All parameters available to the file module are also available when running the `copy` or 
`template` modules.

*dest*:

* absolute path to a file on the filesystem.

*state*:

* either 'file', 'link', 'directory', or 'absent'.  The default is 'file'.  If 'directory', the directory and all immediate subdirectories will be created if they do not exist.  If 'file', the file will NOT be created if it does not exist, specify `copy` or `template` for the module name instead if you need to put content at the specified location.  If 'link', the symbolic link will be created or changed.  If 'absent', directories will be recursively deleted, and files or symlinks will be unlinked.

*mode*:

* the mode the file or directory should be, such as 644, as would be given to `chmod`.  English modes like "g+x" are not yet supported.

*owner*:

* name of user that should own the file or directory, as would be given to `chown`.

*group*:

* name of group that should own the file or directory, as would be given to `chgrp`

*src*:

* path of the file to link to (applies only to 'link' state)

*dest*:

* location where the symlink will be created for 'link' state, also an alias for 'path'.


Example action from Ansible :doc:`playbooks`::

    file path=/etc/foo.conf owner=foo group=foo mode=0644
    file path=/some/path owner=foo group=foo state=directory
    file path=/path/to/delete state=absent
    file src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link

.. _git:

git
```

Deploys software (or files) from git checkouts.

*repo*:

* git or http protocol address of the repo to checkout.

*dest*:

* Where to check it out, an absolute directory path.

*version*:

* What version to check out -- either the git SHA, the literal string
  ``HEAD``, or a tag name.

Example action from Ansible :doc:`playbooks`::

    git repo=git://foosball.example.org/path/to/repo.git dest=/srv/checkout version=release-0.22

.. _group:

group
`````
<commit_msg>Stub reference to SELinux.<commit_after>Ansible Modules
===============

Ansible ships with a number of modules (called the 'module library') 
that can be executed directly on remote hosts or through :doc:`playbooks`.
Users can also write their own modules.   These modules can control system
resources, like services, packages, or files (anything really), or 
handle executing system commands.  

Let's review how we execute three different modules from the command line::

    ansible webservers -m service -a "name=httpd state=running"
    ansible webservers -m ping
    ansible webservers -m command -a "/sbin/reboot -t now"

Each module supports taking arguments.  Nearly all modules take ``key=value`` 
arguments, space delimited.  Some modules take
no arguments, and the command/shell modules simply take the string
of the command you want to run.

From playbooks, Ansible modules are executed in a very similar way::

    - name: reboot the servers
      action: command /sbin/reboot -t now 

All modules technically return JSON format data, though if you are using the
command line or playbooks, you don't really need to know much about
that.  If you're writing your own module, you care, and this means you do
not have to write modules in any particular language -- you get tho choose.

Most modules other than command are `idempotent`, meaning they will seek
to avoid changes to the system unless a change needs to be made.  When using Ansible
playbooks, these modules can trigger 'change events'.  Unless otherwise
noted, any given module does support change hooks.

Let's see what's available in the Ansible module library, out of the box:

.. _apt:

apt
```

Manages apt-packages (such as for Debian/Ubuntu).

*pkg*:

* A package name or package specifier with version, like name-1.0

*state*:

* Can be either 'installed', 'removed', or 'latest'.  The default is 'installed'.

Example action from Ansible :doc:`playbooks`::

    apt pkg=foo ensure=removed
    apt pkg=foo ensure=installed
    apt pkg=foo ensure=latest

NOTE: the apt module cannot currently request installation of a specific software version, as the yum
module can.  This should be available in a future release.

.. _command:

command
```````

The command module takes the command name followed by a list of
arguments, space delimited.  

If you want to run a command through the shell (say you are using
'<', '>', '|', etc), you actually want the 'shell' module instead.  
The 'command' module is much more secure as it's not affected by the user's environment.

The given command will be executed on all selected nodes.  It will not
be processed through the shell, so variables like "$HOME" and 
operations like "<", ">", "|", and "&" will not work.  As such, all
paths to commands must be fully qualified.

This module does not support change hooks and returns the return code
from the program as well as timing information about how long the
command was running for.

Example action from Ansible :doc:`playbooks`::

    command /sbin/shutdown -t now

If you only want to run a command if a certain file does not exist, you can do the
following::

    command /usr/bin/make_database.sh arg1 arg2 creates=/path/to/database

The `creates=` option will not be passed to the executable.


.. _copy:

copy
````

The copy module moves a file on the local box to remote locations.  In addition to the options
listed below, the arguments available to the `file` module can also be passed to the copy
module.

*src*:

* Local path to a file to copy to the remote server.  This can be an
  absolute or relative path.


*dest*:

* Remote absolute path where the file should end up.

This module also returns md5sum information about the resultant file.

Example action from Ansible :doc:`playbooks`::

    copy src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644


.. _facter:

facter
``````

Runs the discovery program 'facter' on the remote system, returning
JSON data that can be useful for inventory purposes.  

Requires that 'facter' and 'ruby-json' be installed on the remote end.

This module is informative only - it takes no parameters & does not
support change hooks, nor does it make any changes on the system.
Playbooks do not actually use this module, they use the :ref:`setup`
module behind the scenes.


fetch
`````

This module works like 'copy', but in reverse.  It is used for fetching files
from remote machines and storing them locally in a file tree, organized by hostname.

*src*:

* The file on the remote system to fetch.  This needs to be a file, not a directory. Recursive fetching may be supported later.

*dest*:

* A directory to save the file into.  For example, if the 'dest' directory is '/foo', a src file named '/tmp/bar' on host 'host.example.com', would be saved into '/foo/host.example.com/bar'.

The fetch module is a useful way to gather log files from remote systems.  If you require
fetching multiple files from remote systems, you may wish to execute a tar command and
then fetch the tarball.


file
````

Sets attributes of files, symlinks, and directories, or removes files/symlinks/directories. 
All parameters available to the file module are also available when running the `copy` or 
`template` modules.

*dest*:

* absolute path to a file on the filesystem.

*state*:

* either 'file', 'link', 'directory', or 'absent'.  The default is 'file'.  If 'directory', the directory and all immediate subdirectories will be created if they do not exist.  If 'file', the file will NOT be created if it does not exist, specify `copy` or `template` for the module name instead if you need to put content at the specified location.  If 'link', the symbolic link will be created or changed.  If 'absent', directories will be recursively deleted, and files or symlinks will be unlinked.

*mode*:

* the mode the file or directory should be, such as 644, as would be given to `chmod`.  English modes like "g+x" are not yet supported.

*owner*:

* name of user that should own the file or directory, as would be given to `chown`.

*group*:

* name of group that should own the file or directory, as would be given to `chgrp`

*src*:

* path of the file to link to (applies only to 'link' state)

*dest*:

* location where the symlink will be created for 'link' state, also an alias for 'path'.


Example action from Ansible :doc:`playbooks`::

    file path=/etc/foo.conf owner=foo group=foo mode=0644
    file path=/some/path owner=foo group=foo state=directory
    file path=/path/to/delete state=absent
    file src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link

The file module also supports numerous SELinux attributes (documentation on this pending).

.. _git:

git
```

Deploys software (or files) from git checkouts.

*repo*:

* git or http protocol address of the repo to checkout.

*dest*:

* Where to check it out, an absolute directory path.

*version*:

* What version to check out -- either the git SHA, the literal string
  ``HEAD``, or a tag name.

Example action from Ansible :doc:`playbooks`::

    git repo=git://foosball.example.org/path/to/repo.git dest=/srv/checkout version=release-0.22

.. _group:

group
`````

{'fraction_non_alphanumeric': 0.07829679949576301, 'fraction_numerical': 0.002661250787870299, 'mean_word_length': 3.9360525406152784, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3898091', 'n_tokens_mistral': 4051, 'n_tokens_neox': 3886, 'n_words': 2109}