Unable to start logstash using /etc/init.d/logstash start - ansible

I'm trying to start the logstash service using below playbook. Output says starting but when I checked the status its in stopped state.
---
- hosts: test
gather_facts: False
remote_user: test
become: yes
become_user: root
become_method: sudo
tasks:
- name: starting /etc/init.d/logstash start
shell: /etc/init.d/logstash start
- name: status /etc/init.d/logstash status
shell: /etc/init.d/logstash status
register: logstash_status
- name: output
debug:
msg: "{{logstash_status}}"
Output
PLAY [test] ************************************************************************************************************************************************************
TASK [starting /etc/init.d/logstash start] *****************************************************************************************************************************
changed: [192.168.1.10]
TASK [status /etc/init.d/logstash status] ******************************************************************************************************************************
fatal: [192.168.1.10]: FAILED! => {"changed": true, "cmd": "/etc/init.d/logstash status", "delta": "0:00:00.021383", "end": "2021-06-02 20:31:17.701169", "msg": "non-zero return code", "rc": 1, "start": "2021-06-02 20:31:17.679786", "stderr": "", "stderr_lines": [], "stdout": "Stopped", "stdout_lines": ["Stopped"]}
to retry, use: --limit #/home/test/logstat-config/new.retry
PLAY RECAP *************************************************************************************************************************************************************
192.168.1.10 : ok=1 changed=1 unreachable=0 failed=1

I was able to start the service by running it in background.
---
- hosts: test
gather_facts: False
remote_user: test
become: yes
become_user: root
become_method: sudo
tasks:
- name: starting /etc/init.d/logstash start
shell: nohup /etc/init.d/logstash start &
register: logstash
- debug:
msg: "{{logstash}}"
- name: status /etc/init.d/logstash status
shell: /etc/init.d/logstash status
register: logstash_status
- name: output
debug:
msg: "{{logstash_status}}"
Output:
PLAY [test] ************************************************************************************************************************************************************
TASK [starting /etc/init.d/logstash start] *****************************************************************************************************************************
changed: [192.168.1.10]
TASK [debug] ***********************************************************************************************************************************************************
ok: [192.168.1.10] => {
"msg": {
"changed": true,
"cmd": "nohup /etc/init.d/logstash start &",
"delta": "0:00:00.014488",
"end": "2021-06-03 17:31:02.914306",
"failed": false,
"rc": 0,
"start": "2021-06-03 17:31:02.899818",
"stderr": "",
"stderr_lines": [],
"stdout": "Starting logstash",
"stdout_lines": [
"Starting logstash"
]
}
}
TASK [status /etc/init.d/logstash status] ******************************************************************************************************************************
changed: [192.168.1.10]
TASK [output] **********************************************************************************************************************************************************
ok: [192.168.1.10] => {
"msg": {
"changed": true,
"cmd": "/etc/init.d/logstash status",
"delta": "0:00:00.011286",
"end": "2021-06-03 17:31:03.272873",
"failed": false,
"rc": 0,
"start": "2021-06-03 17:31:03.261587",
"stderr": "",
"stderr_lines": [],
"stdout": "Running",
"stdout_lines": [
"Running"
]
}
}
PLAY RECAP *************************************************************************************************************************************************************
192.168.1.10 : ok=4 changed=2 unreachable=0 failed=0

Related

How store output in variable while using the loop in ansible?

I want to check the ip on the server where ip list i have in the json file as below,
cat /tmp/iplist.json
[
"10.10.10.182",
"182.10.10.2",
"192.168.200.2"
]
now the condition is only one ip exist on the system so i was executing the loop to store the only the success output in variable but i am not able to do that does any one knows how can i do this
here is my playbook
---
- name: Load values from json file
hosts: localhost
gather_facts: false
vars:
ip: "{{ lookup('file', '/tmp/iplist.json') | from_json }}"
tasks:
- name: Loop over imported iplist
shell: ip a | grep {{ item }}
loop: "{{ ip }}"
changed_when: true
register: echo
- debug:
msg: "{{ echo }}"
And this how it getting failed error
PLAY [Load values from json file] *************************************************************************************************************************
TASK [Loop over imported iplist] *******************************************************************************************************************************
changed: [localhost] => (item=10.10.10.182)
failed: [localhost] (item=182.10.10.2) => {"ansible_loop_var": "item", "changed": true, "cmd": "ip a | grep 182.10.10.2", "delta": "0:00:00.012178", "end": "2020-05-09 11:30:06.919913", "item": "182.10.10.2", "msg": "non-zero return code", "rc": 1, "start": "2020-05-09 11:30:06.907735", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
failed: [localhost] (item=192.168.200.2) => {"ansible_loop_var": "item", "changed": true, "cmd": "ip a | grep 192.168.200.2", "delta": "0:00:00.029234", "end": "2020-05-09 11:30:07.178768", "item": "192.168.200.2", "msg": "non-zero return code", "rc": 1, "start": "2020-05-09 11:30:07.149534", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
PLAY RECAP *****************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
When you enable gather_facts: true the variable ansible_all_ipv4_addresses will keep the list of all IPv4 addresses of the host. Use intersect to find common items. For example
- debug:
msg: "{{ ansible_all_ipv4_addresses | intersect(ip) }}"

Extract values from stdout output using Ansible

I am running a command to get the status of tomcat and registering it in a variable. How do i extract the specific output of that command and put it in a variable to check further
Play -
- name: Check the State og tomcat service
shell: "svcs tomcat"
register: tomcat_status
- name: Show captured processes
debug:
msg: "{{ tomcat_status.stdout_lines|list }}"
The output of the above is -
server1 ok: {
"changed": false,
"msg": [
"STATE STIME FMRI",
"online 20:11:48 svc:/network/tomcat:tomcat"
]
}
How do i extract the value of STATE here? I want to know if it's online or disabled or shutdown etc.
NOTE - Output with -vvvv
server1 done: {
"changed": true,
"cmd": "svcs tomcat",
"delta": "0:00:00.025711",
"end": "2020-05-11 12:43:43.323017",
"invocation": {
"module_args": {
"_raw_params": "svcs tomcat",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": false
}
},
"rc": 0,
"start": "2020-05-11 12:43:43.297306",
"stderr": "",
"stderr_lines": [],
"stdout": "STATE STIME FMRI\nonline 20:11:48 svc:/network/tomcat:tomcat",
"stdout_lines": [
"STATE STIME FMRI",
"online 20:11:48 svc:/network/tomcat:tomcat"
]
}
Based on your debug about, tomcat_status.stdout_lines is a list (you don't need the |list filter here because it's already a list) that looks like:
["STATE STIME FMRI", "online 20:11:48 svc:/network/tomcat:tomcat"]
To get the STATE value, you need the first field from the second line. So:
- set_fact:
tomcat_state: "{{ tomcat_status.stdout_lines.1.split().0 }}"
That takes the second line (tomcat_status.stdout_lines.1) then splits it on whitespace (.split()), and then takes the first value (.0).
Here's a complete test:
- hosts: localhost
vars:
tomcat_status:
stdout_lines:
- "STATE STIME FMRI"
- "online 20:11:48 svc:/network/tomcat:tomcat"
tasks:
- set_fact:
tomcat_state: "{{ tomcat_status.stdout_lines.1.split().0 }}"
- debug:
var: tomcat_state
Running that playbook results in:
PLAY [localhost] *****************************************************************************
TASK [set_fact] ******************************************************************************
ok: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] => {
"tomcat_state": "online"
}
PLAY RECAP ***********************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

ansible variable reference : unable to read stdout_lines from register variable after ssh and command

Team,
My task is running she’ll commands on hosts that are pulled from registered variable. Currently two hosts but there will be 100 in production.
am unable to read out stdout or stdout_lines. my task and output is below. Its sshing to the remove server and then runnning df -h command and storing output.
actual output ( with some texts stripped but not any brackets)
ok: [localhost] => {
"raid_info.results": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "ssh -F /home/svcngcctal.net \"df -kh /raid/\"",
"delta": "0:00:02.095839",
"end": "2019-10-24 22:55:38.323679",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": {
"_raw_params": "ssh -F /home/metal.net \"df -kh /raid/\"",
"_uses_shell": true,
"warn": true
}
},
"item": {
"nodeType": "4.15.0-45-generic",
"node_name": "hostB"
},
"rc": 0,
"start": "2019-10-24 22:55:36.227840",
"stderr": "Warning: Permanently***",
"stderr_lines": [
"Warning:asd"
],
"stdout": "Filesystem Size Used Avail Use% Mounted on\n/dev/sdb1 7.0T 175G 6.5T 3% /raid",
"stdout_lines": [
"Filesystem Size Used Avail Use% Mounted on",
"/dev/sdb1 7.0T 175G 6.5T 3% /raid"
]
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "ssh -F /home/svcngcc/jenkinstal.net \"df -kh /raid/\"",
"delta": "0:00:02.115591",
"invocation": {
"module_args": {
"_raw_params": "ssh -F /home/sal.net \"df -kh /raid/\"",
"warn": true
}
},
"item": {
"nodeType": "4.15.0-45-generic",
"node_name": "hostA"
},
"rc": 0,
"start": "2019-10-24 22:55:38.467007", ",
"stderr_lines": [
"Warning: Permanently "
],
"stdout": "Filesystem Size Used Avail Use% Mounted on\n/dev/sdb1 7.0T 176G 6.5T 3% /raid",
"stdout_lines": [
"Filesystem Size Used Avail Use% Mounted on",
"/dev/sdb1 7.0T 176G 6.5T 3% /raid"
]
}
]
}
from above output am not able to read stdout lines to verify the mount point..
task:
- name: "RAID mount check for fscache on GPU Nodes"
shell: ssh -F {{ ssh_cfg_path.stdout }} {{ item.node_name }}.{{ ssh_host }} "df -kh /raid/"
ignore_errors: no
register: raid_info
failed_when: raid_info.rc != 0
with_items: "{{ gpu_nodes }}"
- name: raid_info results1_stdout_lines
debug:
var: raid_info.results[1].stdout_lines
does not output anything..
I cannot reproduce your results - I believe that you left out some information when posting.
For example, do your tasks by any chance run on different hosts? The "raid_info" variable is registered per host, so if your tasks run on different hosts this might cause your issue.
My test, with a similar loop:
- playbook.yml
---
- hosts: localhost
gather_facts: false
vars_files:
- packages.yml
tasks:
- name: Run a command
command: "echo {{item}}"
register: my_output
loop:
- 10.10.80.193
- 10.10.80.194
- name: Print the results
debug:
var: my_output
- name: Print only the second item in the list
debug:
var: my_output.results[1].stdout_lines
result:
# ansible-playbook -i inventory.yml playbook.yml
PLAY [localhost] *************************************************************************************************************************
TASK [Run a command] *********************************************************************************************************************
changed: [localhost] => (item=10.10.80.193)
changed: [localhost] => (item=10.10.80.194)
TASK [Print the results] *****************************************************************************************************************
ok: [localhost] => {
"my_output": {
"changed": true,
"msg": "All items completed",
"results": [
{ <snip>
},
{ <snip>
}
]
}
}
TASK [Print only the second item in the list] ********************************************************************************************
ok: [localhost] => {
"my_output.results[1].stdout_lines": [
"10.10.80.194"
]
}
PLAY RECAP *******************************************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Unable to stop and disable firewalld using Ansible

This is my playbook to stop and disable firewalld :
---
- hosts : openstack
connection : ssh
remote_user : ec2-user
become_method : sudo
become : yes
gather_facts : no
tasks :
- command: "{{ item }}"
with_items:
- systemctl stop firewalld
- systemctl disable firewalld
Error :
failed: [ec2-52-87-240-155.compute-1.amazonaws.com] (item=systemctl stop firewalld) => {"changed": true, "cmd": ["systemctl", "stop", "firewalld"], "delta": "0:00:00.009282", "end": "2016-10-27 13:37:20.620051", "failed": true, "item": "systemctl stop firewalld", "rc": 5, "start": "2016-10-27 13:37:20.610769", "stderr": "Failed to stop firewalld.service: Unit firewalld.service not loaded.", "stdout": "", "stdout_lines": [], "warnings": []}
failed: [ec2-52-87-240-155.compute-1.amazonaws.com] (item=systemctl disable firewalld) => {"changed": true, "cmd": ["systemctl", "disable", "firewalld"], "delta": "0:00:00.004876", "end": "2016-10-27 13:37:20.816710", "failed": true, "item": "systemctl disable firewalld", "rc": 1, "start": "2016-10-27 13:37:20.811834", "stderr": "Failed to execute operation: Access denied", "stdout": "", "stdout_lines": [], "warnings": []}
Could anyone help me out with this?
There a few things wrong with this playbook:
don't set a space character between parameter and :
use service module instead of command module
This should work:
---
- hosts: openstack
connection: ssh
remote_user: ec2-user
become: True
gather_facts: False
tasks:
- name: Stop and disable firewalld.
service:
name: firewalld
state: stopped
enabled: False
if firewalld not installed/not running you can simply ignore error message using "failed_when:"
To avoid Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg
- name: 'Disable firewalld Services'
service:
name: "{{item}}"
state: stopped
enabled: no
loop:
- firewalld
register: firewalld_service_disable
failed_when: "firewalld_service_disable|failed and ('Could not find the requested service' not in firewalld_service_disable.msg)"
ignore_errors: yes
tags: test
Below is the anisble playbook execution output
# ansible-playbook main.yml --tags test
PLAY [all] **********************************************************
TASK [Gathering Facts] **********************************************
ok: [ANSIBLECLIENTNODE]
TASK [hardening : Disable firewalld Services] ***********************
changed: [ANSIBLECLIENTNODE] => (item=firewalld)
PLAY RECAP **********************************************************
ANSIBLECLIENTNODE : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
If your ansible version 2.9 and later , follow below "service_facts" method
- name: 'Populate service facts'
service_facts:
- name: 'Disable firewalld Services'
service:
name: "{{item}}"
state: stopped
enabled: no
loop:
- firewalld
when: ansible_facts.services[item] is defined
ignore_errors: yes

Ansible task variable output not displaying

Below is my simple playbook
name: "test"
hosts: webservers
tasks:
- name: Echo my_env_var
shell: "echo $MY_ENV_VARIABLE"
environment:
MY_ENV_VARIABLE: whatever_value
- name: Echo my_env_var again
shell: "echo $MY_ENV_VARIABLE"
register: stdd
- debug: msg={{stdd.stdout_lines}}
My output is always msg:"" or msg: []. Why am i not able to see the value of variable
I took your example and changed it from debug msg to debug var. I also simplified it by only running the task once, and found the error in the process. The environment argument is specific to a task. You aren't including it in your second shell task.
Here's the example I used.
echo.yml
- hosts: localhost
tasks:
- name: Echo my_env_var
shell: "echo $MY_ENV_VARIABLE"
environment:
MY_ENV_VARIABLE: whatever_value
register: stdd
- debug: var=stdd
execution
$ ansible-playbook -c local -i "localhost," echo.yml
PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [Echo my_env_var] *******************************************************
changed: [localhost]
TASK: [debug var=stdd] ********************************************************
ok: [localhost] => {
"var": {
"stdd": {
"changed": true,
"cmd": "echo $MY_ENV_VARIABLE",
"delta": "0:00:00.005332",
"end": "2016-07-25 19:42:54.320667",
"invocation": {
"module_args": "echo $MY_ENV_VARIABLE",
"module_complex_args": {},
"module_name": "shell"
},
"rc": 0,
"start": "2016-07-25 19:42:54.315335",
"stderr": "",
"stdout": "whatever_value",
"stdout_lines": [
"whatever_value"
],
"warnings": []
}
}
}
PLAY RECAP ********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0

Resources