3

My Ansible Playbook:

#Tag --> B.6 -->
  - name: Change the Security Realm to CustomRealm from ManagementRealm
command: /jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect--command="/core-service=management/management-interface=http-interface:read-attribute(name=security-realm)"
register: Realm

  - debug:
  msg: "{{ Realm.stdout_lines }}"

The output for the above command in the message is as follows:

ok: [342f2f7bed8e] => {
"msg": [
"{",
"    \"outcome\" => \"success\","
"    \"result\" => \"ManagementRealm\"",
"}"
]
}

is there a way to just exact \"result\" => \"ManagementRealm\"". I tried using the

Realm.stdout_lines.find('result')

but that fails, AWk & grep commands doesn't seem to be working here.

Any thoughts is greatly appreciated.

Thak you

anish anil
  • 2,299
  • 7
  • 21
  • 41

1 Answers1

2

I think there are a few ways you could handle this.

1) Grep the output before it gets to Ansible:

# Note the change of 'command' to 'shell'
- name: Change the Security Realm to CustomRealm from ManagementRealm
  shell: /jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect--command="/core-service=management/management-interface=http-interface:read-attribute(name=security-realm)" | grep -o 'result.*'
  register: Realm

2) If the output from the source script is always 4 lines long, you can just grab the 3rd line:

# List indexes start at 0
- debug:
  msg: "{{ Realm.stdout_lines[2] | regex_replace('^ *(.*$)', '\\1') }}"

3) The nicest way if you have an option to modify jboss-cli.sh, would be to get the jboss-cli.sh to output valid JSON which can then be parsed by Ansible:

# Assuming jboss-cli.sh produces {"outcome": "success", "result": "ManagementRealm"}
- set_fact:
    jboss_data: "{{ Realm.stdout | from_json }}"
- debug:
    var: jboss_data.result
clockworknet
  • 2,736
  • 1
  • 15
  • 19
  • 1
    (1) what is the point of using ansible if You in fact use shell to perform tasks and have to use grep? (2) What is the point of using automation tool if You have to know before which line number is interesting for You and You cannot efficeicntly react when this asumption is not true? (3) what is the point of using ansible if basic, simple task like filtering strin require to start json analizing machinery? It reminds mi idea to put result in MS SQL Enterprise edition for duing simple select. Maybe it is forth option? –  Sep 06 '19 at 07:18
  • 2
    Lol. (1) What is the point of adding a detailed comment implying an offered answer is not good enough, without providing the 'right' answer yourself? (2) What is the point of failing to spot that the task of filtering a string is entirely different to the task of automating the task of filtering a string, probably just one step in a large number of related tasks? Presumably you know that under the skin Ansible largely just automates running shell scripts & commands via SSH, albeit wrapped in a chunk of Python? – clockworknet Sep 07 '19 at 08:37