Filter line from Ansible stdout_lines result - linux

I am trying to filter out a line out of Ansible stdout_lines result. The ansible playbook task I ran was the following shell argument:
- name: VERIFY | Confirm that queue exists properly
shell: aws sqs list-queues --region {{region}}
environment: "{{ aws_cli_environment|d({}) }}"
register: sqs_list
To see the results, I followed it up with a debug:
- debug:
msg: "{{ sqs_list.stdout_lines|list }}"
which gives the following result during the playbook run:
TASK [sqs : debug] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
"{",
" \"QueueUrls\": [",
" \"sampleurl1\",",
" \"sampleurl2\",",
" \"sampleurl3\",",
" \"sampleurl4\",",
" \"https://sampleurl5/test_sqs\"",
" ]",
"}"
]
}
I want ONLY the last url, "test_sqs" to appear under "msg":, I tried the following filter but had no luck:
- name: VERIFY | Find SQS url
command: "{{ sqs_list.stdout_lines|list }} -- formats | grep $sqs"

aws cli generates JSON output by default, use this advantage:
---
- hosts: localhost
gather_facts: no
tasks:
- shell: aws sqs list-queues --region eu-west-1
register: sqs_list
- debug:
msg: "{{ (sqs_list.stdout | from_json).QueueUrls | last }}"
Output:
PLAY [localhost] **********************************
TASK [command] ************************************
changed: [localhost]
TASK [debug] **************************************
ok: [localhost] => {
"msg": "https://eu-west-1.queue.amazonaws.com/xxxxx/test"
}

Script:
---
- name: A simple template
hosts: local
connection: local
gather_facts: False
tasks:
- name: list queue
shell: aws sqs list-queues --region us-east-1 --queue-name-prefix test_sqs --query 'QueueUrls[*]' --output text
register: sqs_list
- debug:
msg: "{{ sqs_list.stdout_lines|list }}"
Output:
ok: [localhost] => {
"msg": [
"https://queue.amazonaws.com/xxxxxxxxx/test_sqs-198O8HG46WK1Z"
]
}
You can make use of the --queue-name-prefix parameter to list only queues which is starting with name test_sqs. If there is only one queue with that name, you can use this solution.

Related

how to traverse through each line of a text file in ansible

I have to traverse through package list file which contains list of packages with their architecture. How can I feed those input to my playbook file? I found a way to get the package names alone but architecture version is not coming. This is my package_list file
nginx | x86_64
telnet| x86_64
openssh | i386
This is my playbook
- name: get contents of package.txt
command: cat "/root/packages.txt"
register: _packages
- name: get contents of architecture from packages.txt
command: cat "/root/packages.txt" | awk '{print $3}'
register: _arch
- name: Filter
theforeman.foreman.content_view_filter:
username: "admin"
password: "mypass"
server_url: "myhost"
name: "myfilter"
organization: "COT"
content_view: "M_view"
filter_type: "rpm"
architecture: "{{ _arch }}"
package_name: "{{ item }}"
inclusion: True
loop: "{{ _packages.stdout_lines }}"
loop: "{{ _arch.stdout_lines }}"
Any help would be appreciated
The required output is package name and architecture should be read from packages.txt file through ansible-playbook
try this playbook:
- name: Reproduce issue
hosts: localhost
gather_facts: no
tasks:
- name: get contents of package.txt
command: cat "/root/packages.txt"
register: _packages
- debug:
msg: "package: {{ line.0 }}, arch: {{ line.1 }}"
loop: "{{ _packages.stdout_lines }}"
vars:
line: "{{ item.split('|')|list }}"
result:
ok: [localhost] => (item=nginx | x86_64) => {
"msg": "package: nginx , arch: x86_64 "
}
ok: [localhost] => (item=telnet| x86_64) => {
"msg": "package: telnet, arch: x86_64 "
}
ok: [localhost] => (item=openssh | i386) => {
"msg": "package: openssh , arch: i386 "
}
for your case:
- name: Filter
theforeman.foreman.content_view_filter:
:
:
architecture: "{{ line.1 }}"
package_name: "{{ line.0 }}"
inclusion: True
loop: "{{ _packages.stdout_lines }}"
vars:
line: "{{ item.split('|')|list }}"
following the version of ansible you could write too line: "{{ item | split('|') | list }}"
You need to split up the line into the necessary values by filtering.
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Gahter package list
shell:
cmd: cat package.txt
register: _packages
- name: Show packages
debug:
msg: "Name: {{ item.split('|')[0] }}, Arch: {{ item.split('|')[1] }}"
loop: "{{ _packages.stdout_lines }}"
Further Documentation
Playbook filters - Manipulating strings
Jinja Template Designer - Filters
Further Q&A
Split string into list in Jinja?

Skip current host and print the rest in a config file ansible

Folks, I'm trying to achieve the below while running my task to edit an XML
Requirement:
Skip current host in which config file is created and print the rest of host from the inventory group to the config file
<ManagerNode ip = "**node IP**" port = "**node port**"/> <haNodes>IP2:port,IP3:port</haNodes> <!-- Comma Seperated Node IPs with port, ,Except the Same Node -->
Can anyone help with achieving this goal?
Q: "Skip current host ... and print the rest of hosts from the inventory group."
A: Create a list of all IPs and use filter difference to remove the current IP. For example the inventory
shell> cat hosts
[ha]
test_01 IP=10.1.0.11
test_02 IP=10.1.0.12
test_03 IP=10.1.0.13
[ha:vars]
port=4567
and the playbook
shell> cat playbook.yml
- hosts: ha
tasks:
- set_fact:
all_IP: "{{ groups.ha|map('extract', hostvars, 'IP')|list }}"
run_once: true
- debug:
msg: "{{ all_IP|difference([IP])|
product([port])|
map('join', ':')|
list }}"
give (abridged)
shell> ansible-playbook -i hosts playbook.yml
ok: [test_01] =>
msg:
- 10.1.0.12:4567
- 10.1.0.13:4567
ok: [test_02] =>
msg:
- 10.1.0.11:4567
- 10.1.0.13:4567
ok: [test_03] =>
msg:
- 10.1.0.11:4567
- 10.1.0.12:4567
Limit the play to test_01 gives abridged
shell> ansible-playbook -i hosts -l test_01 playbook.yml
ok: [test_01] =>
msg:
- 10.1.0.12:4567
- 10.1.0.13:4567
defined a variable
tg_hosts: : "{{ groups['tgzone']|map('extract',hostvars,'ansible_host')|list }}"
used template as:
{{ tg_hosts | difference([ansible_host])| list | join(':port,') + ':port' }}

Unable to fail terminate Ansible playbook upon string match condition

I wish to fail and terminate my ansible playbook when Number=TKT334
Below is my playbook:
---
- name: "Play 1"
hosts: localhost
any_errors_fatal: True
serial: 1
tags: always
tasks:
- name: Setting string variable.
set_fact:
inlinevar: '2020-06-10 20:22:16\tTKT334\tKIRAN\tDeployed\tPRODUCTION'
- name: Setting string variable.
set_fact:
environment: 'PRODUCTION'
- block:
- name: "Search to avoid duplicate CR Numbers user:{{ ansible_user_id }}"
shell: "echo SUCCESSSSSSS"
failed_when: (inlinevar is search( Number ) and environment == "PRODUCTION")
- debug:
msg: This is FINAL inlinevar `{{ inlinevar }}`
rescue:
- name: Print custom conditional debug message
fail:
msg: >-
{{
command_result_app.stdout is search( Number ) |
ternary(
"This CR is already Deployed. Hence retry with a Unique CR Number.",
"The Dtabase is not reachable. Unable to connect To the Database."
)
}}
However, when I run the playbook I do not fail and continues to print the debug:
I was expecting it to fail but it shows ansible run as successful and does not terminate the playbook executions.
See the output below:
ansible-playbook test.yml -e Number=TKT334
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [Play 1] *************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]
TASK [Setting string variable.] ****************************************************************
ok: [localhost]
TASK [Setting string variable.] ****************************************************************
ok: [localhost]
TASK [Search to avoid duplicate CR Numbers user:{{ ansible_user_id }}] ***
changed: [localhost]
TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
"msg": "This is FINAL inlinevar `2020-06-10 20:22:16\\tTKT334\\t KIRAN\\tDeployed\\tPRODUCTION`"
}
PLAY RECAP *************************************************************************************************************************************************************
localhost : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Can you please suggest?
environment is a special option used to set environment variables for tasks, e.g.
- name: Do something
shell: echo "whatever"
environment:
http_proxy: http://my.server.com:8080
no_proxy: my.domain.com
In your case, environment is always empty after set_fact and environment == "PRODUCTION" is always false.
Rename your variable to something else, don't use environment (see this other question for more discussion), e.g.
[...]
- name: Setting string variable.
set_fact:
deploy_environment: 'PRODUCTION'
- block:
- name: "Search to avoid duplicate CR Numbers user:{{ ansible_user_id }}"
shell: "echo SUCCESSSSSSS"
failed_when: (inlinevar is search( Number ) and deploy_environment == "PRODUCTION")
[...]

Print json output in Ansible and store in a list?

Ansible playbook:
---
- task:-
- code goes here
- name: Trying to get instance Private IP from ASG
ec2_instance_facts:
instance_ids:
- "{{ item }}"
with_items: "{{ INSTANCE_IDS_FROM_ASG }}"
register: instance_ids_result
- set_fact:
msg: "{{ instance_ids_result | json_query('results[*].instances[*].network_interfaces[*].private_ip_address') }} "
- debug: var=msg
I have the output as follows:
ok: [localhost] => {
"msg": [
[
"172.31.144.74"
],
[
"172.31.147.69"
]
]
}
But, I would need the output in a list as ["172.31.144.74", "172.31.147.69"] or "172.31.147.69" "172.31.147.69".
What is the best way to print it that way?
You could flatten your list using the filter
- set_fact:
msg: "{{ instance_ids_result | json_query('results[*].instances[*].network_interfaces[*].private_ip_address') | flatten }} "

how to create Ansible playbook to obtain OS versions of the remote hosts?

I'm new to ansible. I have a requirement that requires me to pull OS version for of more than 450 linux severs hosted in AWS. AWS does not provide this feature - it rather suggests us to get it from puppet or chef.
I created few simple playbooks which does not run
---
- hosts: testmachine
user: ec2-user
sudo: yes
tasks:
- name: Update all packages to latest
yum: name=* state=latest
task:
- name: obtain OS version
shell: Redhat-release
playbook should output a text file with hostname and OS version. Any insight on this will be highly appreciated.
Use one of the following Jinja2 expressions:
{{ hostvars[inventory_hostname].ansible_distribution }}
{{ hostvars[inventory_hostname].ansible_distribution_major_version }}
{{ hostvars[inventory_hostname].ansible_distribution_version }}
where:
hostvars and ansible_... are built-in and automatically collected by Ansible
ansible_distribution is the host being processed by Ansible
For example, assuming you are running the Ansible role test_role against the host host.example.com running a CentOS 7 distribution:
---
- debug:
msg: "{{ hostvars[inventory_hostname].ansible_distribution }}"
- debug:
msg: "{{ hostvars[inventory_hostname].ansible_distribution_major_version }}"
- debug:
msg: "{{ hostvars[inventory_hostname].ansible_distribution_version }}"
will give you:
TASK [test_role : debug] *******************************************************
ok: [host.example.com] => {
"msg": "CentOS"
}
TASK [test_role : debug] *******************************************************
ok: [host.example.com] => {
"msg": "7"
}
TASK [test_role : debug] *******************************************************
ok: [host.example.com] => {
"msg": "7.5.1804"
}
In a structured way:
- hosts: all
become: no
vars:
output_file: os.csv
tasks:
- block:
# For permisison setup.
- name: get current user
command: whoami
register: whoami
run_once: yes
- name: clean file
copy:
dest: "{{ output_file }}"
content: 'hostname,distribution,version,release'
owner: "{{ whoami.stdout }}"
run_once: yes
- name: fill os information
lineinfile:
path: "{{ output_file }}"
line: "{{ ansible_hostname }},\
{{ ansible_distribution }},\
{{ ansible_distribution_version }},\
{{ ansible_distribution_release }}"
# Tries to prevent concurrent writes.
throttle: 1
delegate_to: localhost
Creates a comma separated file named os.csv in execution folder. You can use any variables you want editing line:.
Ansible already provides a lot of information about the remote host in the "hostvars" variable that is automatically available.
To see information of your host named "my_remote_box_name", e.g. do
- debug: var=hostvars['my_remote_box_name']
Some OS information is in
hostvars['my_remote_box_name']['ansible_lsb']
Which, for one of my ubuntu hosts would be along:
{
"hostvars['my_remote_box_name']['ansible_lsb']": {
"codename": "xenial",
"description": "Ubuntu 16.04.1 LTS",
"id": "Ubuntu",
"major_release": "16",
"release": "16.04"
}
You can just use those variables in your playbooks and templates, using the "{{ variable_name }}" notation.
- debug: msg="My release is {{ansible_lsb.release}}"
output:
"msg": "My release is 16.04"
For a couple of windows instances:
- debug:
msg:
- "ansible_distribution {{ hostvars[inventory_hostname].ansible_distribution }}"
- "major version {{ hostvars[inventory_hostname].ansible_distribution_major_version }}"
- "version {{ hostvars[inventory_hostname].ansible_distribution_version }}"
gives:
ok: [server1] => {
"msg": [
"ansible_distribution Microsoft Windows Server 2008 R2 Standard ",
"major version 6",
"version 6.1.7601.65536"
]
}
ok: [server2] => {
"msg": [
"ansible_distribution Microsoft Windows Server 2016 Standard",
"major version 10",
"version 10.0.14393.0"
]
}
"AWS does not provide this feature " - you can check file /etc/os-release to get details of aws instance.
For example
[ec2-user#ip-xx-xx-xx ~]$ cat /etc/os-release
NAME="Amazon Linux AMI"
VERSION="2016.03"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2016.03"
PRETTY_NAME="Amazon Linux AMI 2016.03"
ANSI_COLOR="0;33"
CPE_NAME="cpe:/o:amazon:linux:2016.03:ga"
HOME_URL="http://aws.amazon.com/amazon-linux-ami/"
- name: obtain OS version
shell: Redhat-release
register: result
- name: print OS version
debug: var=result.stdout

Resources