Ansible - Modules βοΈ
Description π
Ansible contains several modules grouped in different categories.
Some categories are:
- system - actions to be performed at a system level
- modifying users, groups, iptables, firewall configs
- working with services
- commands - used to execute commands or scripts on a host
- simple commands using the
command
module - interactive execution using the
expect
module by responding to prompts - run scripts on the host using the
script
module
- simple commands using the
- files - helps work with files
- use the
archive
andun-archive
modules to archive and unarchive files⦠- use
find
,inlinefile
, andreplace
to modify file contents
- use the
- and many others..
Basics π
- command module
parameters
chdir
- change directory before executing the commandcreates
- a filename, when it already exists, this step will be skippedexecutable
- change the shell used to execute the command, should be an absolute pathfree_form
- the command module takes a free form command to runremoves
- remove the file after execution if it existswarn
- warn when the command changes things
- script module
- copy scripts to the remote systems
- execute scripts on remote systems
- name: <play-name> hosts: <host-pattern> tasks: - name: <task-name> script: <path-to/script.sh> <script-args>
- service module
- manage services - start, stop, restart
- name: <play-name> hosts: <host-pattern> tasks: - name: <task-name> service: name=<service-name> state=<service-state>
-
alternate method of creation using yml dictionary
- name: <play-name> hosts: <host-pattern> tasks: - name: <task-name> <module>: name: <service-name> state: <service-state>
- they are one and the same
Examples π§©
- command module
-
script module
play
- name: Sample Script Play - Create Target Log Directory hosts: localhost tasks: - name: create the target log directory script: create_script.sh
-
service module
play
- name: Samlple Service Play - Start Postgres Service hosts: localhost tasks: - name: Start postgres service service: name=postgresql state=started
- why is the state
started
, and not start?- if the service is already running, the state
started
will not cause any changes, this is called idempotent
- if the service is already running, the state
- started, stopped, restarted ensure that the service is in the desired state
- why is the state