free_form in the module documentation indicate that the module takes free form command to run. There is no parameter actually named free_form.
script
Runs a local script on a remote node after transferring it.
- name: Play1hosts: localhosttasks:
- name: 'Run a script on remote server'script: /some/local/script.sh -arg1 -arg2
service
Manage services - Start, Stop, Restart
- name: Start services in orderhosts: localhosttasks:
- name: Start the database serviceservice: name=postgresql state=started
- name: Start the httpd serviceservice:
name: httpdstate: started
lineinfile
Search for a line in a file and replace it or add it if it doesn't exist.
- name: Add DNS server to resolv.confhosts: localhosttasks:
- name: Add DNS server to resolv.conflineinfile:
path: /etc/resolv.confline: 'nameserver 10.1.250.10'
Variables
Stores information that varies with each host
In inventory file, ansilbe_host, ansible_connection, etc., are variables.
Defined in the playbook using vars directive. key/value pairs of variables will be child of vars.
Defined in separate file with host name. Eg: web.yaml where web is the ansible host alias.
Accessed using {{ variable_name }}Jinja2 templating syntax.
Note
{{ variable_name }} neeed to be written inside quotes if it is not concatenated with other strings.
Conditionals
Execute different tasks based on value of a data, a variable or the result of previous task.
when statement is used in task level, if the result is True, the task is executed.
All variables can be used directly in conditionals without double curly braces.
==andor operators are used in conditional statements.
when can be combined with loop statement to apply conditionals in loop. Condition is applied separately on each item.
Previous task results can be registered in variable using register keyword. String contents of the registered variable using variable.stdout
Loops
Execute a task multiple times, Ansible offers loop, with_<lookup> and until keywords.
Any with_* statements which requires lookup within a loop should not be converted to loop keyword.
loop keyword is equivalent to with_list, and is the best choice for simple loops.
- name: Print list of fruitshosts: localhostvars:
fruits:
- Apple
- Banana
- Grapes
- Orangetasks:
- command: echo "{{ item }}"with_items: "{{ fruits }}"
Roles
A way to package reusable ansible Playbooks and share with others.
Roles let you automatically load Ansible artifacts (vars, files, tasks, handlers, etc.,).
Used for orgnaizing the ansible code into tasks, vars, defaults, handlers, templates, etc., directories.
Ansible Glaxy is a place where you can find many useful Ansible roles.
ansible-galaxy init <role> command used to create the required directory structure for the role.
Roles will be referred from roles directory in play's current directory or /etc/ansible/roles directory.
In /etc/ansible/ansible.cfg roles path is defined as roles_path = /etc/ansible/roles
To search a role, use ansible-galaxy search <role keyword> or can use the Ansible Galaxy website.
To use a role, use ansible-galaxy install <role>. The role will be extracted to /etc/ansible/roles directory.
To list roles, use ansible-galaxy list. Roles path can be found by ansible-config dump | grep ROLE
Use ansible-galaxy install <role> -p ./roles for installing the role not in the default roles directory.
Advanced Topics
Preparing Windows Server
Windows targets requires winrm on the machine for Ansible connection. Requires configuration on the target machine.
pywinrm module need to be installed on the Ansible control machine.
Ansible-Galaxy
Repository of re-usable ansible roles from the community.
Patterns
In ansible commands and playbooks, patterns can be used to select specific group of hosts in your inventory.
Patterns are highly flexible. wildcard, RegEx, exclude, etc., available.
Dynamic Inventory
Ansible inventory with fluctuating host IP entries, etc., can make use of Dynamic inventory.
This is achieved by inventory plugins. Eg. aws_ec2 plugin.
Developing Custom Modules
If required custom logic can be written in Python and can be imported as Ansible custom module.