ansible set_stats to display output one after another - ansible

I have a code
- set_stats:
data:
wspi_output: "{{ wspi_output.value }}"
- set_stats:
data:
hostname: "{{ inventory_hostname }}"
when called the set_stats variable in another playbook I get the output in the variable section as shown below.
{
"wspi_output": "https://welcome.com:8531https://welcome1.com:8531",
"hostname": "xxxyyzz.xyz.comabcrzzz.xyz.com"
}
My second playbook reads the set_stats variables as shown below:
- name: content of set_stats
debug:
var: wspi_output
- name: get the hostname
debug:
var: hostname
- name: output the set_stats variable to csv
copy:
content: '{{ wspi_output }} , {{ hostname }}'
dest: /tmp/wspi_output.csv
mode: 0755
Is it possible to show something like
{
"wspi_output": "https://welcome.com:8531 , "hostname": "xxxyyzz.xyz.com"
"wspi_output": "https://welcome1.com:8531","hostname": "abcrzzz.xyz.com"
}

Related

How to get interpolated value of variable in Ansible / Jinja2

I'm trying to define Ansible variables this way:
user:
name: First Last
nick: '{{ vars["user"]["name"] | regex_replace("\W", "_") }}'
email: '{{ vars["user"]["nick"] }}#example.com'
And the result email is: "{{ vars[\"user\"][\"name\"] | regex_replace(\"\\W\", \"_\") }}#example.com.
I also tried to set email like this: {{ lookup("vars", "user.nick") }}#example.com or {{ lookup("vars", "user")["nick"] }}#example.com, and it says An unhandled exception occurred while running the lookup plugin 'vars'.
Is there a way to get resulting variable values as:
user:
name: First Last
nick: First_Last
email: First_Last#example.com
?
ansible 2.9.10,
python version = 3.8.5
It's not possible cross-reference keys in a dictionary. It's necessary to declare the variables outside the dictionary. For example, the playbook
- hosts: localhost
vars:
my_name: First Last
my_nick: "{{ my_name | regex_replace('\\W', '_') }}"
user:
name: "{{ my_name }}"
nick: "{{ my_nick }}"
email: "{{ my_nick }}#example.com"
tasks:
- debug:
var: user
gives (abridged)
user:
email: First_Last#example.com
name: First Last
nick: First_Last
A more flexible option is to create the variables in the loop. For example, the playbook
- hosts: localhost
vars:
users:
"First Last":
domain: example.com
tasks:
- debug:
msg:
- "name: {{ name }}"
- "nick: {{ nick }}"
- "email: {{ email }}"
loop: "{{ users|dict2items }}"
vars:
name: "{{ item.key }}"
nick: "{{ item.key|regex_replace('\\W', '_') }}"
email: "{{ nick ~ '#' ~ item.value.domain }}"
gives (abridged)
msg:
- 'name: First Last'
- 'nick: First_Last'
- 'email: First_Last#example.com'

Pass dictionary to jinja2 template from task loop

I pass dictionary from play to task. I use loop to call another task from separate yml file again passing the dictionary. From there I call Jinja2 template and pass the dictionary again. I cannot access the dictionary values from Jinja2.
I tried to pass the dictionary to template with_items and with_dict. Still the same problem.
play:
- role: example
vars:
brands:
brand_1:
name: "brand1"
brand_2:
name: "brand2"
brand_3:
name: "brand_3"
task in role with loop:
- name: Loop through configuration files
include_tasks: generate_config_files.yml
loop: "{{ lookup('dict', brands) }}"
loop_control:
loop_var: outer_item
generate_config_files.yml
- name: Generate the configuration files
template:
src: "consumer.properties.j2"
dest: "{{ kafka_location }}/{{ item.key }}/consumer.properties"
owner: "{{ kafka_user }}"
group: "{{ kafka_group }}"
mode: 0644
with_dict: "{{ outer_item }}"
consumer.properties.j2
{% for item in outer_item %}
Name: "{{ item.name }}"
{% endfor %}
I expect to access the dictionary value in template and generate the same file with different values based on number of brands in dictionary. So if there are 3 brands I expect to generate 3 files with different Name: inside.
Unfortunately I am getting:
"msg": "AnsibleUndefinedVariable: 'str object' has no attribute 'name'"
Any ideas?
1) Indentation of vars: is wrong.
2) The single loop does the job.
3) Iteration in the template is not necessary.
4) Numeric mode must be quoted mode: '0644'.
The playbook below
- hosts: localhost
roles:
- role: example
vars:
kafka_user: admin
kafka_group: admin
kafka_location: /scratch
brands:
brand_1:
name: "brand1"
brand_2:
name: "brand2"
brand_3:
name: "brand_3"
with tasks
$ cat roles/example/tasks/main.yml
- include_tasks: generate_config_files.yml
, with the included task
$ cat roles/example/tasks/generate_config_files.yml
- name: Generate the configuration files
template:
src: "consumer.properties.j2"
dest: "{{ kafka_location }}/{{ item.key }}/consumer.properties"
owner: "{{ kafka_user }}"
group: "{{ kafka_group }}"
mode: '0644'
loop: "{{ brands|dict2items }}"
, and with the template
$ cat roles/example/templates/consumer.properties.j2
Name: "{{ item.value.name }}"
gives
$ tree /scratch/brand_*
/scratch/brand_1
└── consumer.properties
/scratch/brand_2
└── consumer.properties
/scratch/brand_3
└── consumer.properties
$ cat /scratch/brand_*/consumer.properties
Name: "brand1"
Name: "brand2"
Name: "brand_3"
Is this what you're looking for?

How to extract the output from stdout.lines in ansible

---
- name: Mikrotik info
hosts: mikrotik
connection: network_cli
remote_user: root
gather_facts: false
tasks:
- name: show info
routeros_command:
commands: /system routerboard print
register: rb_info
- name: Debug info
debug:
msg: "{{ rb_info.stdout_lines }}"
Output:
routerboard: yes
model: 751G-2HnD
serial-number: 3A6502B2A2E7
firmware-type: ar7240
factory-firmware: 3.0
current-firmware: 6.42.3
upgrade-firmware: 6.43.4
I need to filter it for "upgrade-firmware" string and get output like this:
upgrade-firmware: 6.43.4
I should use regex_replace? Or I can use grep or something like that?
Any thoughts are greatly appreciated.
Thank you
(update)
Use from_yaml and combine a dictionary. For example
- set_fact:
minfo: "{{ minfo|default({})|combine(item|from_yaml) }}"
loop: "{{ rb_info.stdout_lines }}"
- debug:
var: minfo['upgrade-firmware']
give
minfo['upgrade-firmware']: 6.43.4
(for the record)
Robust solution is to write the data to template and include_vars. The tasks below
- tempfile:
register: tempfile
- template:
src: minfo.j2
dest: "{{ tempfile.path }}"
- include_vars:
file: "{{ tempfile.path }}"
name: minfo
- debug:
var: minfo
with the template
shell> cat minfo.j2
{% for item in rb_info.stdout_lines %}
{{ item }}
{% endfor %}
should give
"minfo": {
"current-firmware": "6.42.3",
"factory-firmware": 3.0,
"firmware-type": "ar7240",
"model": "751G-2HnD",
"routerboard": true,
"serial-number": "3A6502B2A2E7",
"upgrade-firmware": "6.43.4"
}
The tasks below creates variable upgrade_firmware
- set_fact:
upgrade_firmware: "{{ item.split(':').1|trim }}"
loop: "{{ rb_info.stdout_lines|map('quote')|map('trim')|list }}"
when: item is search('^upgrade-firmware')
- debug:
var: upgrade_firmware
It is possible to put all the parameters into the dictionary
- set_fact:
minfo: "{{ minfo|default({})|
combine({item.split(':').0: item.split(':').1|trim}) }}"
loop: "{{ rb_info.stdout_lines|map('quote')|map('trim')|list }}"
- debug:
var: minfo['upgrade-firmware']

fetching contents of json file and modifying the content using ansible

I have JSON file with contents
env_variables.json
{ "server": "{{server}}" , "notes": "{{notes}}" }
and ansible-playbook as below: (to upload a lambda with the environment variables)
playbook.yml
- hosts: localhost
vars:
server: localhost
notes: hello this is localhost
tasks:
- name: Lambda creation/updation
lambda:
name: ansible_test
state: present
region: "eu-west-1"
role: 'arn:aws:iam::xyz:role/xyz_lambda'
zip_file: '{{ item.path }}'
runtime: 'python3.6'
environment_variables: "{{ lookup('file','/env_variables.json') | from_json }}"
handler: 'lambda_function.lambda_handler'
How can I use the above-mentioned "env_variables.json" file format to insert the environment variables with values mentioned in the "vars" directive of playbook.yml?
Use include_vars for that.
- name: Load data from json
include_vars: file=/env_variables.json
If this file is outside of normal lookup pathes, you can use set_fact module:
- name: Load data from json
set_fact:
loaded_data: '{{ lookup('file','/env_variables.json') | from_json }}'

How to use variables in with_items in anisble playbook

I have create ansible role to create multiple lambda function, where I am passing some parameters from variable file. My variable file looks like
Variable file
S3BucketName: "test_bucket"
S3Key1: "test.zip"
runtime: "python3.6"
handler1: "test.lambda_handler"
role1: "test_role_arn"
memory_size: "128"
timeout: "180"
s3_key2: "temp.zip"
role2: "temp_role_Arn"
handler2: "temp.lambda_handler"
In my playbook, I am using ansible loop to create multiple aws lambda functions at the same time. when I am using variable in with_items.
Playbook file
- hosts: localhost
roles:
- ansible-lambda
vars_files:
- "ansible-lambda/vars/cf_vars.yaml"
lambda:
name: '{{ item.name }}'
region: "{{ aws_region }}"
state: "{{state}}"
runtime: "{{ runtime }}"
timeout: "{{timeout}}"
memory_size : "{{memory_size}}"
s3_bucket: "{{ S3BucketName}}"
s3_key: '{{ item.s3_key }}'
role: '{{ item.role }}'
handler: '{{ item.handler }}'
with_items:
- name: test
s3_key: "{{ S3Key1 }}" #refering to variable 1
- name: temp
s3_key: "{{ S3Key2 }}" #refering to variable 2
- debug:
msg: "Lambda creation Complete!!"
Following is the error:
fatal: [localhost]: FAILED! => {"msg": "'S3Key1' is undefined"}
This playbook works, when I pass the absolute values instead of variables. I mean s3_key: test.zip
how to use variables in with item?
-------------- var file ---------------
aws_region: austin
lambda_list:
- name: lambda1
state: "UR STATE HERE"
S3BucketName: "test_bucket"
S3Key: "test.zip"
runtime: "python3.6"
handler: "test.lambda_handler"
role_desc: "test_role_arn"
memory_size: "128"
timeout: "180"
- name: lambda2
state: "UR STATE HERE"
S3BucketName: "test_bucket"
S3Key: "test2.zip"
runtime: "python2.7"
handler: "test.lambda_handler"
role_desc: "test_role_ARN"
memory_size: "256"
timeout: "150"
---------------playbook------------------------
- hosts: localhost
vars_files: "ansible-lambda/vars/cf_vars.yaml"
tasks:
lambda:
name: '{{ item.name }}'
region: "{{ aws_region }}"
state: "{{ item.state }}"
runtime: "{{ item.runtime }}"
timeout: "{{ item.timeout }}"
memory_size : "{{ item.memory_size }}"
s3_bucket: "{{ item.S3BucketName }}"
s3_key: "{{ item.s3_key }}"
role: "{{ item.role_desc }}"
handler: "{{ item.handler }}"
with_items:
- "{{ lambda_list }}"
Here's a snippet of the correct way IMHO how to achieve what you're trying to do, sure there are few other ways but to be both efficient and easy config in example above you can see that there's a dict which holds every lambda's info as a key dict, when u use with_items it iterates each key to the task while using the item's data as {{ item.name }}.
You could even put a dict/list in a dict. for exmaple:
lambda_list:
- name: lambda1 # <--- each dash('-') is a key with a value, that value is a dict
S3: #
S3BucketName: "test_bucket"
S3Key: "test.zip"
- name: lambda2
S3: # <-- without the dash its indicated as a list inside the dict.
S3BucketName: "test_bucket"
S3Key: "test2.zip"
in this case to access your nested list you would use {{ item.S3.S3BucketName }} or {{ item['S3']['S3BucketName'] }}
if it was a dict in a dict you would get the key/value of each key without a proper way to access a specific key(with loops you can iterate the dict and use 'when' to get the desired key.)
Here's few references worth reading about loops, dicts and how to access them.
http://ansible-docs.readthedocs.io/zh/stable-2.0/rst/playbooks_loops.html#nested-loops

Resources