Ansible: Extracting IP address from a CIDR( 192.168.21.x/24) - ansible

I have a variable name something like this defined in json format.
"zxc_address" : "192.168.21.x/24"
and i need to extract the IP address part(192.168.21.x) using ansible(yaml code)
what is the simple solution for that? can it be done using ansible filters. if yes then how?
Thanks,
VM

If you are using Ansible >=1.9 it is possible to use a filter for this:
{{ '192.0.2.1/24' | ipaddr('address') }}
Have a look at the documentation.

This should work:
{{ zxc_address.split("/")[0] }}

Related

How to use multiple statements in Ansible azure_rm conditional_groups

I have an ansible role which pulls all VM names(Adding details below):
plugin: azure_rm
auth_source: cli
include_vm_resource_groups:
plain_host_names: yes
conditional_groups:
prod_hosts: "'prod1' in name"
uat_hosts: "'uat' in name"
The above works perfectly fine. However, I have a requirement where the prod hosts can either have prod1 or prod2 in VM name. Ex: prod1-appvm, prod2-appvm. If I add "'prod1' or 'prod2' in name", will it work?
Ex:
conditional_groups:
prod_hosts: "'prod1' or 'prod2' in name"
uat_hosts: "'uat' in name"
I tried searching online, and I found this, where it says a mapping of group names to Jinja2 expressions. But I couldn't get any confirmed answer if 'or' will work.
Thanks and Regards,
Safi Junaid.
Add conditional statement using When Condition . When condition is used to control whether a task or role runs or is skipped.
It is the simplest conditional statement applies to a single task in Ansible. Create the task, then add a when statement that applies a test. You can write something as follows :
- hosts: all
tasks:
when: name in ['prod1','prod2']
Check this document for examples on grouping VMs based on VM type.
Open this Conditionals document for more information.

Transform ansible yml files "with vars templating" to yml files "without templating"

I have this yml files inside an ansible project with templating and vars :
custom_values:
postgresql:
postgresqlDatabase: "{{ secrets.db_name }}"
postgresqlPassword: "{{ secrets.postgres_password }}"
I search a solution to generate the same yml file without the templating like :
custom_values:
postgresql:
postgresqlDatabase: "mydatabase"
postgresqlPassword: "mypassword"
Do you know an existing software to do that automatically ?
You already have a set of ansible templates, that are rendered by a render engine like Jinja2.
The easiest way to convert them would be to actually use the render engine to render the templates, supplying the correct values to it.
You'll end up with a bunch of templates that have the {{ something }} blocks, replaced with the values you want.
Since this looks to be simple Jinja2 templating, please refer to: https://jinja.palletsprojects.com/en/2.10.x/
You'll end up with something like this:
>>> from jinja2 import Template
>>> template = Template('Hello {{ name }}!')
>>> template.render(name='John Doe')
Please also refer to this stackoverflow post: How to load jinja template directly from filesystem
That explains how to load templates from files
This python code is OK for me :
#import necessary functions from Jinja2 module
from jinja2 import Environment, FileSystemLoader
#Import YAML module
import yaml
#Load data from YAML into Python dictionary
config_data = yaml.load(open('./my_vars.txt'))
#Load Jinja2 template
env = Environment(loader = FileSystemLoader('./templates'), trim_blocks=True, lstrip_blocks=True)
template = env.get_template('my_template.yml')
#Render the template with data and print the output
print(template.render(config_data))
thanks :)

Extract server IP from the group in the inventory file

I have the following inventory file:
[group_01]
g01_h01 ansible_ssh_host='10.1.0.1'
g01_h01 ansible_ssh_host='10.1.0.2'
[group_02]
g02_h01 ansible_ssh_host='10.2.0.1'
g02_h01 ansible_ssh_host='10.2.0.2'
[group_03:children]
group_01
group_02
[group_03:vars]
fst_group2={{groups['group_02'][0]}}
snd_group1={{groups['group_01'][1]}}
I would like that in my playbook variables had the following values:
fst_group2=10.2.0.1
snd_group1=10.1.0.2
Instead I get:
fst_group2=g02_h01
snd_group1=g01_h02
Any ideas, a workaround?
Very strange task indeed... Anyway,
groups variable – is a list of hosts, which are g01_h01, g01_h02, etc.
To achieve what you expect, you may use this:
[group_03:vars]
fst_group2={{hostvars[groups['group_02'][0]]['ansible_ssh_host']}}
snd_group1={{hostvars[groups['group_01'][1]]['ansible_ssh_host']}}
And keep in mind that ansible_ssh_host is deprecated in favor of ansible_host.

How to concatenate several strings and variables in Yaml?

I need to do something like this:
domain: &domain example.com
ip: &ip 1.2.3.4
Address4: v=spf1 include:*domain ip4:*ip -all
I tried this but didn't work, it says there's a syntax error:
Address4: 'v=spf1 include:'*domain' ip4:'*ip' -all'
The implementation (I'm using these files in Ansible) seems to support concatenation, for example this works fine:
Address2: http://*domain # actually this doesn't work, I don't remember the exact example
Any ideas?
I don't exactly get why you're trying to concatinate in YAML, since Ansible uses jinja2 as a templating engine:
Defining variables in a vars file for Ansible (in YAML):
domain: example.com
ip: 1.2.3.4
Referencing variables (still in YAML):
Address4: v=spf1 include:{{ domain }} ip4:{{ ip }} -all
When you use the Address4 variable in the template module (I assume), it works the same way.

Using app engine interactive console with jinja2 template

from jinja2 import Template
template = Template('Hello {{ name }}!')
template.render(name='John Doe')
I have entered the above into app engine's interactive console and get no output. How can I get output?
I have tried adding the code I found at the following link to the console, but still no output.
Debug Jinja2 in Google App Engine
Thanks,
Brian in Atlanta
You forgot to add a print. Try this:
from jinja2 import Template
template = Template('Hello {{ name }}!')
print template.render(name='John Doe')

Resources