Ansible tasks after delegate_to:127.0.0.1 are skipped - linux

My code is as below, I am reading from a local csv file use those values for tasks on remote hosts
---
- name: Empty Topics
hosts: remote_host
gather_facts: no
vars:
kafka_topics: /bin/kafka-topics
bootstrap_server: "list_of_broker_hosts"
retention_ms: 604800000
command_config: /etc/kafka/client.properties
kafka_log_dirs: /usr/bin/kafka-log-dirs
#ansible_connection: ssh
#ansible_user: ansible
#ansible_become: true
tasks:
- name: "Reading Topic Names"
read_csv:
path: topics_list.csv
register: topics
delegate_to: 127.0.0.1
- name: "Setting Topic Retention to 0"
become: yes
become_user: root
shell:
{{ kafka_topics }} --bootstrap_server {{bootstrap_server}} --alter --topic "{{ item.topic_name }}" --config retention.ms=0 --command_config #{{command_config}}
#touch /tmp/producer_test_1
loop: "{{ topics.list }}"
- name: "waiting for size to go zero "
become: yes
become_user: root
shell:
topic_size=1
while [ $topic_size -ne 0 ]
do
topic_size=`{{kafka_log_dirs}} --command_config {{command_config}} --bootstrap_server {{bootstrap_server}} --topic-list "{{ item.topic_name }}" --describe | grep -oP '(?<=size":)\d+' | awk '{ sum += $1 } END { print sum }' `
sleep 40
done
loop: "{{ topics.list }}"
- name : "Setting Topic Retention to 7"
become: yes
become_user: root
shell:
#{{ kafka_topics }} --bootstrap_server {{bootstrap_server}} --alter --topic "{{ item.topic_name }}" --config retention.ms={{retention_ms}} --command_config {{command_config}}
#touch /tmp/producer_test_2
loop: "{{ topics.list }}"
here the tasks after first task "Reading Topic Names" are skipped , if I remove this task, they succeed but that means I have to hard code values on subsequent tasks
execution log as below. How can I avoid this. current ansible version is 2.9.19 This playbook had previously worked as is on ansible version 2.9.6 in my previous organization, not sure what settings may be changed in my new company.
I tried delegate_to: localhost as well as delegate_to: 127:0:0:1
ansible#localhost[~] $ ansible-playbook empty_topics.pb -i /home/ansible/inv_sit.yml -l all
PLAY [Empty Topics] ***********************************************************************************************************
TASK [Reading Topic Names] ****************************************************************************************************
ok: [remotehost]
TASK [Setting Topic Retention to 0] *******************************************************************************************
TASK [waiting for size to go zero] ********************************************************************************************
TASK [Setting Topic Retention to 7] *******************************************************************************************
PLAY RECAP ********************************************************************************************************************
remotehost : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
ansible#localhost[~] $ ansible-playbook empty_topics.pb -i /home/ansible/inv_sit.yml -l all -vvv
ansible-playbook 2.9.19
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 3.6.8 (default, Jun 14 2022, 12:54:58) [GCC 8.5.0 20210514 (Red Hat 8.5.0-10)]
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /home/ansible/inv_sit.yml as it did not pass its verify_file() method
script declined parsing /home/ansible/inv_sit.yml as it did not pass its verify_file() method
Parsed /home/ansible/inv_sit.yml inventory source with ini plugin
Skipping callback 'actionable', as we already have a stdout callback.
Skipping callback 'counter_enabled', as we already have a stdout callback.
Skipping callback 'debug', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'full_skip', as we already have a stdout callback.
Skipping callback 'json', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'null', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
Skipping callback 'selective', as we already have a stdout callback.
Skipping callback 'skippy', as we already have a stdout callback.
Skipping callback 'stderr', as we already have a stdout callback.
Skipping callback 'unixy', as we already have a stdout callback.
Skipping callback 'yaml', as we already have a stdout callback.
PLAYBOOK: empty_topics.pb *****************************************************************************************************
1 plays in empty_topics.pb
PLAY [Empty Topics] ***********************************************************************************************************
META: ran handlers
TASK [Reading Topic Names] ****************************************************************************************************
task path: /home/ansible/empty_topics.pb:18
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<127.0.0.1> EXEC /bin/sh -c 'echo ~ansible && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ansible/.ansible/tmp `"&& mkdir "` echo /home/ansible/.ansibl e/tmp/ansible-tmp-1672893960.0795922-3957982-94418260356492 `" && echo ansible-tmp-1672893960.0795922-3957982-94418260356492="` echo /home/ansible/.ansible/tmp/ansible-tmp-1672893960.0795922-3957982-94418260356492 `" ) && sleep 0'
Using module file /usr/lib/python3.6/site-packages/ansible/modules/files/read_csv.py
<127.0.0.1> PUT /home/ansible/.ansible/tmp/ansible-local-3957970ixug96xw/tmpn373dahz TO /home/ansible/.ansible/tmp/ansible-tmp- 1672893960.0795922-3957982-94418260356492/AnsiballZ_read_csv.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/ansible/.ansible/tmp/ansible-tmp-1672893960.0795922-3957982-94418260356492/ /home/ ansible/.ansible/tmp/ansible-tmp-1672893960.0795922-3957982-94418260356492/AnsiballZ_read_csv.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/libexec/platform-python /home/ansible/.ansible/tmp/ansible-tmp-1672893960.0795922-3957982-944 18260356492/AnsiballZ_read_csv.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/ansible/.ansible/tmp/ansible-tmp-1672893960.0795922-3957982-94418260356492/ > /dev/ null 2>&1 && sleep 0'
ok: [remotehost] => {
"changed": false,
"dict": {},
"invocation": {
"module_args": {
"delimiter": null,
"dialect": "excel",
"fieldnames": null,
"key": null,
"path": "topics_list.csv",
"skipinitialspace": null,
"strict": null,
"unique": true
}
},
"list": []
}
TASK [Setting Topic Retention to 0] *******************************************************************************************
task path: /home/ansible/empty_topics.pb:23
TASK [waiting for size to go zero] ********************************************************************************************
task path: /home/ansible/empty_topics.pb:31
TASK [Setting Topic Retention to 7] *******************************************************************************************
task path: /home/ansible/empty_topics.pb:42
META: ran handlers
META: ran handlers
PLAY RECAP ********************************************************************************************************************
remotehost : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
ansible#localhost[~] $
I tried delegate_to: localhost as well as delegate_to: 127:0:0:1

As suggested by β.εηοιτ.βε, I checked the csv file, although there were contents , it was missing the header row, hence the issue. Now with header row added to the csv file, the code works.

Related

Ansible - passing vars from a playbook to another

I am working with Ansible and I have defined a var (varx) in inventory file (inventory1.yml) like this (only for env2):
env1:
hosts:
env1.domain.com:
env2:
hosts:
env2.domain.com:
vars:
varx: 'valuex'
I am running playbook1 and calling playbook2 like this:
ansible-playbook -i inventory/inventory1.yml playbooks/playbook1.yml --extra-vars "env=env1"
playbook1.yml:
---
roles:
- role1
role1 - main.yml:
---
- name: role1
command: bash script.sh {{ env }} {{ 'varx='~varx if varx is defined else '' }}
Inside script.sh (linux bash) I am calling playbook2:
#!/bin/bash
ENV=$1
varx=$2
do_stuff() {
local arguments= "env=$ENV { 'varx='~varx if varx is defined else '' }}"
ansible-playbook -i inventory/inventory2.yml playbook2.yml --extra-vars "$arguments"
}
do_stuff
The idea is to pass varx from inventory1.yml if it exists in inventory1.yml to script.sh when it runs playbook2.yml. If it doesn't exist in inventory1.yml it will look for it in inventory2.yml.
The code as it is its looking for it in inventory2.yml, so I am not being able to retrieve it from inventory1.yml.
I believe that there is some issue in script.sh.
How can I achieve to bring varx from inventory1.yml and if doesn't exist to capture it from inventory2.yml?
I want to make some changes in role1 so that I can pass the vars to script.sh (in a string for example,or in a better way - using ansible-playbook and passing the vars) and than capturing the vars in script.sh and launching playbook2. Any ideas how I can acomplish that?
Basically what I want to do is something like this:
bash script.sh args
and the arguments should all go inside args variable (args= env varx).
What i have done is:
role1 - main.yml:
---
- name: role1
command: bash script.sh arguments
arguments: "{{ env }} {{ 'varx='~varx if varx is defined else '' }}"
But when It reaches script.sh the varx is not being read.
Minimal reproducible example
Given the inventory
shell> cat inventory1
env1:
hosts:
env1.domain.com:
ansible_host: 10.1.0.61
ansible_user: admin
env2:
hosts:
env2.domain.com:
ansible_host: 10.1.0.62
ansible_user: admin
vars:
varX: valueX
, the playbook
shell> cat pb1.yml
- hosts: all
gather_facts: false
tasks:
- command: "sh -c '/tmp/script.sh {{ env }} {{ _varX }}'"
register: out
vars:
_varX: "varX={{ varX|default('') }}"
- debug:
var: out.stdout
, and the scripts at the remote hosts
shell> ssh admin#10.1.0.61 cat /tmp/script.sh
echo ansible-playbook -i inventory2 pb2.yml -e "$1 $2"
shell> ssh admin#10.1.0.62 cat /tmp/script.sh
echo ansible-playbook -i inventory2 pb2.yml -e "$1 $2"
The playbook gives
shell> ansible-playbook -i inventory1 pb1.yml -e "env=env1"
PLAY [all] ************************************************************************************
TASK [command] ********************************************************************************
changed: [env2.domain.com]
changed: [env1.domain.com]
TASK [debug] **********************************************************************************
ok: [env1.domain.com] =>
out.stdout: ansible-playbook -i inventory2 pb2.yml -e env1 varX=
ok: [env2.domain.com] =>
out.stdout: ansible-playbook -i inventory2 pb2.yml -e env1 varX=valueX
PLAY RECAP ************************************************************************************
env1.domain.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
env2.domain.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Fo me it worked like this:
---
- name: role1
command: bash script.sh
env={{env}}
{{ 'varx='~varx if varx is defined else '' }}
In the shell script I capture vars like this:
#!/bin/bash
for arg in "$#"; do
arguments+="$arg "
done
do_stuff() {
ansible-playbook -i inventory/inventory2.yml playbook2.yml --extra-vars "$arguments"
}
do_stuff

How can I compare a remote file with a reference on controller without failing the playbook when different?

I have a very peculiar problem, in my code I have to compare ansible value with customer expected value and customer expected un_matched value, however the final playbook output should be failed =0 instead of failed =1.
The code is:
- name: show file contents of customer-expects.txt
debug:
msg: "{{ lookup('file', '/customer-expects.txt') }}"
- shell: cat /etc/issue
register: issue
changed_when: false
ignore_errors: yes
- assert:
that:
- lookup('file', '/customer-expects.txt') == issue.stdout
success_msg: "matched! {{ lookup('file', '/customer-expects.txt') }} = {{ issue.stdout }}"
- name: show file contents customer-expects_unmatched.txt
debug:
msg: "{{ lookup('file', '/customer-expects_unmatched.txt') }}"
- shell: cat /etc/issue
register: issue
changed_when: false
ignore_errors: yes
- assert:
that:
- lookup('file', '/customer-expects_unmatched.txt') == issue.stdout
fail_msg: "unmatched! {{ lookup('file', '/customer-expects_unmatched.txt') }} = {{ issue.stdout }}"
success_msg: "matched! {{ lookup('file', '/customer-expects.txt') }} = {{ issue.stdout }}"
Output is:
[root#ansible-master /]# ansible-playbook tab8.role.yml -v
Using /etc/ansible/ansible.cfg as config file
PLAY [This output is for Tab-8 of Function Design document] *********
TASK [Gathering Facts] **********************************************
ok: [ansible-client1]
TASK [issue_tab8 : show file contents of customer-expects.txt] ******
ok: [ansible-client1] => {
"msg": ""
}
TASK [issue_tab8 : shell] *******************************************
ok: [ansible-client1] => {"changed": false, "cmd": "cat /etc/issue", "delta": "0:00:00.005200", "end": "2022-01-25 14:17:57.070688", "rc": 0, "start": "2022-01-25 14:17:57.065488", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [issue_tab8 : assert] ******************************************
ok: [ansible-client1] => {
"changed": false,
"msg": "matched! = "
}
TASK [issue_tab8 : show file contents customer-expects_unmatched.txt] *
ok: [ansible-client1] => {
"msg": "abc"
}
TASK [issue_tab8 : shell] *******************************************
ok: [ansible-client1] => {"changed": false, "cmd": "cat /etc/issue", "delta": "0:00:00.004603", "end": "2022-01-25 14:17:57.674059", "rc": 0, "start": "2022-01-25 14:17:57.669456", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [issue_tab8 : assert] ******************************************
fatal: [ansible-client1]: FAILED! => {
"assertion": "lookup('file', '/customer-expects_unmatched.txt') == issue.stdout",
"changed": false,
"evaluated_to": false,
"msg": "unmatched! abc = "
}
PLAY RECAP **********************************************************
ansible-client1 : ok=6 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
You are taking this the wrong way and make it unnecessarily complicated IMO.
You have to understand that laying out a playbook in ansible consists of describing the state in which you want to find the target machine. In this particular situation, you want to describe the state in which you want to find a particular file on the target. This is basically done with a task using the copy module. Once you have done that, knowing if the file is similar or different is just a matter of command line switches or task options.
For more on the below explanation see Validating tasks
Ansible has a check_mode which will let you run an entire playbook just to verify what it would do. You can also unconditionally apply that mode to a single task so that it always runs as a check (check_mode: true), or always makes changes to the remote target however the playbook was called (check_mode: false). We are interested here in the first form.
Similarly, ansible has a diff possibility that will let you see what are the differences between the state you described and the modification it (should have to) apply on the target to reach that state.
An example whith your above scenario. I made the test targeting my local machine only but you can get the exact same result on any remote target.
First let's create the reference file we want to check against in our ansible directory.
mkdir -p files
echo "I'm the reference file" > files/ansible_reference.txt
For the example, I'll now create one identical and one different file on our target so that we can compare both cases:
echo "I'm the reference file" > /tmp/ansible_similar.txt
echo "I'm a different file" > /tmp/ansible_different.txt
This is the playbook compare.yml
---
- name: compare remote file to a local reference
hosts: localhost
gather_facts: false
vars:
local_reference: ansible_reference.txt
remote_files_2_check:
- /tmp/ansible_similar.txt
- /tmp/ansible_different.txt
tasks:
- name: Dry run a copy with diff to check if remote file is aligned
copy:
src: "{{ local_reference }}"
dest: "{{ item }}"
check_mode: true
diff: true
loop: "{{ remote_files_2_check }}"
Which gives:
$ ansible-playbook compare.yml
PLAY [compare remote file to a local reference] *****************************
TASK [Dry run a copy with diff to check if remote file is aligned] **********
ok: [localhost] => (item=/tmp/ansible_similar.txt)
--- before: /tmp/ansible_different.txt
+++ after: /home/user/ansible_project/files/ansible_reference.txt
## -1 +1 ##
-I'm a different file
+I'm the reference file
changed: [localhost] => (item=/tmp/ansible_different.txt)
PLAY RECAP ******************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Notes:
Although Ansible reports changed for the different file this is only what would happen. Since we are in dry-run for the given task, the above will not change your target. You can run it any number of times, it will report the same result as long as you don't change the reference or the target state.
I you are not happy with the default reporting of the task, you can register the result, explore its content and use it in subsequent tasks to meet your exact requirement.
Note that the above may report a difference on the permission of the target file. You may have to tune this in the copy module to avoid false positive.

Why can't I capture an environment variable with Ansible?

I am trying to get and print the value of a given environment variable (ENV_VAR):
$ cat ~/.bash_profile
ENV_VAR=Updated
ENV_VAR2=Updated
$ source ~/.bash_profile && echo $ENV_VAR
Updated
I can successfully retrieve it via terminal, however by using the Ansible playbook below I get an error:
# YAML
---
- hosts: all
vars:
env_var: "{{ lookup('env','ENV_VAR') }}"
tasks:
- name: Add/Update an environment variable to the remote user's shell
lineinfile:
dest: ~/.bash_profile
regexp: '^ENV_VAR='
line: "ENV_VAR=Updated2"
- name: Get the value of the environment variable we just added
shell: source ~/.bash_profile && echo $ENV_VAR
register: env_var_updated
- name: Print the value of the environment variable
debug:
msg: "var1: {{ env_var }} - var2 {{ env_var_updated.stdout }}"
Executing:
$ ansible-playbook playbook.yml
PLAY [all] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [192.168.0.222]
TASK [Add/Update an environment variable to the remote user's shell] ***********************************************
ok: [192.168.0.222]
TASK [Get the value of the environment variable we just added] *****************************************************
fatal: [192.168.0.222]: FAILED! => {"changed": true, "cmd": "source ~/.bash_profile && echo $ENV_VAR", "delta": "0:00:00.002337", "end": "2020-12-02 10:20:21.963968", "msg": "non-zero return code", "rc": 127, "start": "2020-12-02 10:20:21.961631", "stderr": "/bin/sh: 1: source: not found", "stderr_lines": ["/bin/sh: 1: source: not found"], "stdout": "", "stdout_lines": []}
PLAY RECAP *********************************************************************************************************
192.168.0.222 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Results after execution:
$ cat ~/.bash_profile
ENV_VAR=Updated2
ENV_VAR2=Updated
$ source ~/.bash_profile && echo $ENV_VAR
Updated2
I am logged as the same user (In the terminal window and SSH for Ansible)
As shown by the following guide:
https://docs.ansible.com/ansible/2.5/modules/shell_module.html
Ansible shell actually runs in /bin/sh and not /bin/bash. You can specify the shell as /bin/bash through:
- name: Get the value of the environment variable we just added
shell: source ~/.bash_profile && echo $ENV_VAR
register: env_var_updated
args:
executable: /bin/bash

Ansible: A registered variable is undefined when even after debug shows it's contents

Ansuble Register Variables
I have task list where I register output from a shell task. The shell command lists a directory and then pipes the output to grep. The task works and I get the output I wanted. Eariler in the playbook SVN source files are checked outed and transferred to {{ DestRemoteDir }} directory. The code in question builds a list from the output of the command. I take those lists to set various other facts later in the playbook. [ Mapping a list of Debian packages in a specific order of 2nd list with Ansible ] (I know of ansible's find module, but I found "ls -1" far easier to use.)
Playbook (tasks/main.yml)
## use List -1 to find the file names for the deb files.| grep
## It stays, The tasks work as expected.
- name: Find the needed deb files
shell: "ls -1 {{ DestRemoteDir }} | grep .*.deb" # Local dir
register: Deb_List
ignore_errors: yes
delegate_to: 127.0.0.1 #localhosts
- name: Find the needed IXAD list file
shell: "ls -1 {{ DestRemoteDir }} | grep IA-XD" # Local dir
register: IXAD_List_Source
ignore_errors: yes
delegate_to: 127.0.0.1 #localhosts
- name: Print the Deblist for IX-AD
debug:
msg:
- "Deb file list: {{ Deb_List.stdout_lines }}"
- "IXAD file {{ IXAD_List_Source.stdout_lines }}"
- name: Print if undefined Deb_list
debug:
msg: "Deb_List is not real"
when: Deb_list is undefined
- name: Print if undefined Deb_list.stdout_lines
debug:
msg: "Deb_List.stdout_lines is not real"
when: Deb_list.stdout_lines is undefined
Output
changed: [AnsibleTest2 -> 127.0.0.1] => {
"changed": true,
"cmd": "ls -1 /home/ansible/IA-XD | grep .*.deb",
***
"invocation": {
"module_args": {
***
},
"rc": 0,
"stderr": "",
"stderr_lines": [],
"stdout": "iaxd_1.2.7.6-15_all.deb\niaxdautocfg_1.0.0.0_all.deb\nlibjpegdeb7u1_amd64.deb\nopenjdk-6-jre_610-1~deb7u1_amd64.deb\nopenjdk-6-jre-headless_610-1~deb7u1_amd64.deb\nopenjdk-610-1~deb7u1_all.deb",
"stdout_lines": [
"iaxd_1.2.7.6-15_all.deb",
"iaxdautocfg_1.0.0.0_all.deb",
"libjpeg7u1_amd64.deb",
"openjdk-6-jre_610-1~deb7u1_amd64.deb",
"openjdk-6-jre-headless_610-1~deb7u1_amd64.deb",
"openjdk-6-jre-lib_610-1~deb7u1_all.deb"
]
}
changed: [AnsibleTest2 -> 127.0.0.1] => {
"changed": true,
"cmd": "ls -1 /home/ansible/IA-XD | grep IA-XD",
"invocation": {
"module_args": {
"_raw_params": "ls -1 /home/ansible/IA-XD | grep IA-XD",
***
}
},
"rc": 0,
"stderr": "",
"stderr_lines": [],
"stdout": "IA-XD_List",
"stdout_lines": [
"IA-XD_List"
]
}
Debug
ok: [AnsibleTest2] => {
"msg": [
"Deb file list: [u'iaxd_1.2.7.6-15_all.deb',u'iaxdautocfg_1.0.0.0_all.deb',u'libjpegdeb7u1_amd64.deb',u'openjdk-6-jre_610-1~deb7u1_amd64.deb',u'openjdk-6-jre-headless_610-1~deb7u1_amd64.deb',u'openjdk-610-1~deb7u1_all.deb']",
"IXAD file [u'IA-XD_List']"
]
}
Above both "stdout_lines": [ "IA-XD_List" ] and "stdout_lines": [ "iaxd_1.2.7.6-15_all.deb",...] are ready.
So from the debug task I see that the variables are there and defined (Or so I thought). But when I test to see if there if the shell command failed.
Error Message
The status clearly shows that Deb_List is undefined. why?
I just debug thad value of that variable.
TASK [server_iaxd_install : Print if undefined Deb_list] *********************************************************************
task path: /home/ansible/ansible_server_working/roles/server_iaxd_install/tasks/main.yml:64
Tuesday 25 February 2020 11:50:12 +0100 (0:00:00.222) 0:00:07.890 ******
ok: [AnsibleTest2] => {
"msg": "Deb_List is not real"
}
Read vars_file '/home/ansible/.ssh/SudoPassKey'
Try to test the key data I want "Deb_list.stdout_lines", Ansible errors as the variable is not there.
TASK [server_iaxd_install : Print if undefined Deb_list.stdout_lines] ********************************************************
task path: /home/ansible/ansible_server_working/roles/server_iaxd_install/tasks/main.yml:69
Tuesday 25 February 2020 11:50:12 +0100 (0:00:00.197) 0:00:08.087 ******
fatal: [AnsibleTest2]: FAILED! => {
"msg": "The conditional check 'Deb_list.stdout_lines is undefined' failed. The error was: error while evaluating conditional (Deb_list.stdout_lines is undefined): 'Deb_list' is undefined\n\nThe error appears to have been in '/home/ansible/ansible_server_working/roles/server_iaxd_install/tasks/main.yml': line 69, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Print if undefined Deb_list.stdout_lines\n ^ here\n"
}
Either this is a "forest for the trees" (I can't see the obvious) error or an annoying quirk of Ansible that I need to kluge around.
Yes, it was a "forest for the trees" (I can't see the obvious) error.
After looking for the error, I change the code.
- name: Print if undefined Deb_List
debug:
msg: "Deb_List is not real"
when: Deb_List is undefined
- name: Print if undefined Deb_List.stdout_lines
debug:
msg: "Deb_List.stdout_lines is not real"
when: Deb_List.stdout_lines is undefined
And now the output is far better:
TASK [server_iaxd_install : Print if undefined Deb_List] *********************************************************************
task path: /home/ansible/ansible_server_working/roles/server_iaxd_install/tasks/main.yml:77
Tuesday 25 February 2020 16:00:34 +0100 (0:00:00.242) 0:00:08.013 ******
skipping: [AnsibleTest2] => {}
Read vars_file '/home/ansible/.ssh/SudoPassKey'
TASK [server_iaxd_install : Print if undefined Deb_List.stdout_lines] ********************************************************
task path: /home/ansible/ansible_server_working/roles/server_iaxd_install/tasks/main.yml:82
Tuesday 25 February 2020 16:00:34 +0100 (0:00:00.065) 0:00:08.078 ******
skipping: [AnsibleTest2] => {}

ansible start process if not running using ps

i am using ansible to start the namenode process if incase its not running, I am using jps to check for the process and check the status using service_namenode_status and start the process using service_namenode_start any idea how to use this properly...?
- name: Check if Namenode is running
shell: jps | grep " NameNode" | grep -v grep
ignore_errors: yes
changed_when: false
register: service_namenode_status
- name: Report status of Namenode
fail:
msg: |
Service NameNode is not running.
Return code from `jps | grep " NameNode" | grep -v grep`:
{{ service_namenode_status.rc }}
when: service_namenode_status.rc != 0
register: service_namenode_start
- name: Start Namenode
command: su - hdfs -c "/usr/hdp/current/hadoop-client/sbin/hadoop-daemon.sh --config $HADOOP_CONF_DIR start namenode"
tags: namenode
when: service_namenode_status.rc != 0
OUTPUT:
TASK [namenode : Check if Namenode is running] *********************************
fatal: [server1.hdp.vagrant.data.uc]: FAILED! => {"changed": false, "cmd": "jps | grep \" NameNode\" | grep -v grep", "delta": "0:00:00.192755", "end": "2016-11-09 10:59:10.360552", "failed": true, "rc": 1, "start": "2016-11-09 10:59:10.167797", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []}
...ignoring
TASK [namenode : Report status of Namenode] ************************************
fatal: [server1.hdp.vagrant.data.uc]: FAILED! => {"changed": false, "failed": true, "msg": "Service NameNode is not running.\nReturn code from `jps | grep \" NameNode\" | grep -v grep`:\n1 \n"}
NO MORE HOSTS LEFT *************************************************************
RUNNING HANDLER [metastore : restart postgresql] *******************************
to retry, use: --limit #/Users/krisdigitx/myLab/hdp/provisioning/site.retry
PLAY RECAP *********************************************************************
server1.hdp.vagrant.data.uc : ok=86 changed=32 unreachable=0 failed=1
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.
Try this:
- name: Check if Namenode is running
tags: namenode
shell: jps | grep " NameNode" | grep -v grep
ignore_errors: yes
changed_when: false
register: service_namenode_status
- name: Start Namenode
tags: namenode
command: su - hdfs -c "/usr/hdp/current/hadoop-client/sbin/hadoop-daemon.sh --config $HADOOP_CONF_DIR start namenode"
when: service_namenode_status.rc == 1
My changes were:
I added the "tags: namenode" line to the 1st part; you'll need to collect the register value if you are filtering on this tag
I removed your 2nd part and went straight to the 3rd.
A similar config is working for me for a similar problem.

Resources