Saturday, July 28, 2018

Ansible modules


shell, command, and raw modules
-------------------------------

`command`
This module is the default module used in ad-hoc commands and has very basic functionality.
$ ansible vm01 -a "hostname"

The above command is same as including "-m command".

This doesn't support the following shell operators.
|
>
<
&
etc..

If you need extended functionalities like piping and redirection, use "shell" module instead.
`shell`
Similar to "command" module but runs the command trough a shell (/bin/sh) which enables it to support
extended shell syntax that cannot be done on "command" module.
$ ansible vm02 -m shell -a "ls -l | awk '{print $1}' | grep [./*] | > /tmp/file.out"

Do note that on the following command, the variable will be evaluated on your control machine before
sending to target.
$ ansible vm02 -m shell -a "echo $LOGNAME"

To evaluate it on the target, use single instead of double quotes.
$ ansible vm02 -m shell -a 'echo $LOGNAME'
`raw`
- quick and dirty way to run SSH commands
- doesn't go through the module subsystem
- doesn't require Python installed on remote system (COOL!)
- can be used in 2 the following cases:
    a. to install "python-simplejson" on the remote machine
    b. used in speaking to devices that doesn't have python interpreter installed such as routers

File Transfer Modules
---------------------

`copy`
This allows you to transfer files to target machines.
$ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"

File and Directory Management
-----------------------------

`file`
Can be used to change permissions and ownerships of files.
$ ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
$ ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"

Can also be used to creare directories similar to `mkdir -p`.
$ ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"

You can also delete directories recursively.
$ ansible webservers -m file -a "dest=/path/to/c state=absent"

Package Management
------------------

`yum`
Ensure a package is installed, but don’t update it:
$ ansible webservers -m yum -a "name=acme state=present"

Ensure a package is installed to a specific version:
$ ansible webservers -m yum -a "name=acme-1.5 state=present"

Ensure a package is at the latest version:
$ ansible webservers -m yum -a "name=acme state=latest"

Ensure a package is not installed:
$ ansible webservers -m yum -a "name=acme state=absent"

User Management
---------------

`user`
Allows creating, deleting, and updating of user accounts.
$ ansible all -m user -a "name=foo password="
$ ansible all -m user -a "name=foo state=absent"

Source Control
--------------

`git`
Deploy your webapp straight from git:
$ ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"

Service Management
------------------

`service`
Ensure a service is started on all webservers:
$ ansible webservers -m service -a "name=httpd state=started"

Alternatively, restart a service on all webservers:
$ ansible webservers -m service -a "name=httpd state=restarted"

Ensure a service is stopped:
$ ansible webservers -m service -a "name=httpd state=stopped"

Time Limited Background Operations
----------------------------------

`async_status`
You can use this module to query status of background jobs/tasks launched by Ansible.

Let's say you launch a job with a max timeout of 60 minutes and without polling:
$ ansible all -B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff"

To query the task, you can make use of `async_status` together with the job id.
$ ansible web1.example.com -m async_status -a "jid=488359678239.2844"

The job ID is usually returned when you launched the task. If none, you can look at the latest
file under "~/.ansible_async/"

Gathering Facts
---------------

`setup`
Ansible facts are information gethered from target systems. This will be discussed in detail in
playbooks section.

To see all information on a target node:
$ ansible all -m setup

You can filter the output by:
$ ansible all -m setup -a "filter=ansible_all_ipv4_addresses"

No comments:

Post a Comment