Monday, August 14, 2017

Looping in ansible

If you have defined a YAML list in a variables file, or the ‘vars’ section, you can also do:
with_items: "{{ somelist }}"

Looping over file

---
 - hosts: ss
   tasks:
   - name: check the contents of a file and echo it
     debug: msg="{{item}}"
     with_file:
      - /home/bhr_moham607/test1.txt
      - /home/bhr_moham607/test2.txt

will loop and echo the message if only both the files are available. Also these files should be in master

Looping with Fileglob

with_fileglob matches all files in a single directory, non-recursively, that match a pattern. It calls Python’s glob library, and can be used like this:

---
- hosts: all
  tasks:
    # first ensure our target directory exists
    - name: Ensure target directory exists
      file:
        dest: "/etc/fooapp"
        state: directory

    # copy each file over that matches the given pattern
    - name: Copy each file over that matches the given pattern
       copy:
        src: "{{ item }}"
        dest: "/etc/fooapp/"
        owner: "root"
        mode: 0600
        with_fileglob:
         - "/playbooks/files/fooapp/*"

Tuesday, August 8, 2017

Difference between Import and Include

Import

All import* statements are pre-processed at the time playbooks are parsed. If you use any import* Task (import_playbook, import_tasks, etc.), it will be static. 

For static imports, the parent task options will be copied to all child tasks contained within the import.

Also in import it executes the code  and substitutes the result into the program. And no code is copied like include and hence no waste of memory or processor’s time.

With import loops cannot be used at all.

Include

# include, replace it with the contents of the file, and continue. All include* statements are processed as they encountered during the execution of the playbook. If you use any include* Task (include_tasks, include_role, etc.), it will be dynamic.

For dynamic includes, the task options will only apply to the dynamic task as it is evaluated, and will not be copied to child tasks.

Tags & Tasks which only exist inside a dynamic include will not show up in –list-tags output & –list-tasks output. You cannot use notify to trigger a handler name which comes from inside a dynamic include You cannot use --start-at-task to begin execution at a task inside a dynamic include.

When using variables for the target file or role name, variables from inventory sources (host/group vars, etc.) cannot be used.

Example for Import and Include

[root@ansim0 ymls]# cat Inc.yml
---
 - hosts: test
   tasks:
    - include: test.yml
      with_items: [ 1,2,3,4]

...

TASK [include] *****************************************************************
included: /root/ymls/test.yml for ansic2
included: /root/ymls/test.yml for ansic2
included: /root/ymls/test.yml for ansic2
included: /root/ymls/test.yml for ansic2

TASK [testing for include and import] ******************************************
ok: [ansic2] => {
    "msg": 1

}



Friday, August 4, 2017

Ansible Filters

Filters in Ansible are from Jinja2, and are used to do some filter operations with variable and with other items also.

Below are some of the filters with example.

1) Mandatory

When we use this setting and if the variable is not set then we will get the error like mentioned below.

---
 - hosts: ss
   tasks:
   - name: Check Mandotry filter setting
     debug: msg={{ variable | mandatory }}
...

Result:
FAILED! => {"failed": true, "msg": "Mandatory variable not defined."}

2) Default filter

{{ variable1 | default(56) }}

When this setting is used and if the variable is not set then the value of the variable will be replaced with the default value mentioned int the filter rather than the error being raised.

3) Default with Omit module

If we leave default filter value as NULL it will cause the chain of filters like “{{ variable | default(None) | Second_filter or omit }}” to fail. So it is better to have the value as omit if the value is NULL.

In the result we can see that only for file INDIA we have permission 444 for others where mode value is not set in the variable declaration section it took the default permission.

---
 - hosts: ss
   tasks:
   - name: Test Default filter with omit module
     file: dest={{item.path}} state=touch mode={{item.mode|default(omit)}}
     with_items:
     - path: /tmp/I
     - path: /tmp/LOVE
     - path: /tmp/INDIA
       mode: "0444"
...

-rw-------  1 moham607 users          0 Jul 18 06:21 I
-rw-------  1 moham607 users          0 Jul 18 06:21 LOVE

-r--r--r--  1 moham607 users          0 Jul 18 06:21 INDIA

4) Using IP filter 

Use IP filter to check is the variable value is a valid IP v4 address or not. But to use this filter we need python-netaddr package to be installed.

{{ myIP | ipaddr }}
{{ myIP | ipv4 }}
{{ myIP | ipv6 }}

---
 - hosts: ss
   tasks:
     - name: Take the IP of server using facts and check with the help of IP filter and print
       debug: msg="IP address of server is {{facter_ipaddress_eth2}}"
       when: (facter_ipaddress_eth2 | ipv4)
      (or)
      when: (ansible_eth3.ipv4.address | ipv4)
...


Above Task will print the message with IP address when the value of facter_ipaddress_eth2 is a valid ipv4 IP address.