how to do a successful exit from the playbook task - ansible

I have a playbook which is repeating one task, shell is invoked from ansible task, but it does not exit successful on completion of task and repeating the shell instructions again.
Playbook:
---
hosts: dev-servers
tasks:
- name: clean up directoty
shell: rm -rf /opt/data/latest/*
- name: copy the file
copy: src=/home/data/metadata.zip dest=/opt/data
- name: Change directoty
shell: cd /opt/data
- name: copy the file
copy: src=/root/Run_Build.sh dest=/opt/deployment_scripts
- name: permission
shell: chmod +x /opt/deployment_scripts/Run_Build.sh
- name: deploy
command: sh /opt/deployment_scripts/Run_Build.sh
run_once: true
register: result
- debug: msg="{{ result.stdout }}"
- debug: msg="{{ result.stderr }}"
Run_Build.sh:
#!/bin/bash
Jboss_logs=/opt/jboss/jboss-eap-6.4/standalone/log/server.log
for i in {1..100}; do
sleep 10
if [ -f $Jboss_logs/server.log ]; then
if grep -e 'Deployed "deploy.war"' $Jboss_logs/server.log ; then
echo "Code successfully deployed"
exit 0
else
echo "*****************************************************************************"
echo " JBOSS is still loading deployment - please be patient .... Loading again in 10 sec"
echo "*****************************************************************************"
fi
else
echo "server log file has not been created yet, please wait for sometime."
fi
done

Related

Ansible: check, that file has changed after shell command

I have a shell command in ansible, that changes some file. But most of the time, file stays the same.
What is a preferred way to compare the content of the file before/after shell command and set task changed property?
Q: "Compare the content of the file before/after the shell command and set task changed."
A: Make the script report the changes, register the output of the shell command, and test changed_when.
For example, the task below sets a random environment variable RANDOM_TEXT and writes it to the file /tmp/test.ansible. If the file changes the script will report changed. Because of the potentially failing diff you have to set ignore_errors: true
- shell: "sh -c 'cp /tmp/test.ansible /tmp/test.ansible.orig;
echo $RANDOM_TEXT > /tmp/test.ansible;
if (diff /tmp/test.ansible.orig /tmp/test.ansible > /dev/null);
then echo not changed;
else echo changed;
fi'"
environment:
RANDOM_TEXT: "{{ lookup('random_choice', 'AAA', 'BBB') }}"
ignore_errors: true
register: result
changed_when: result.stdout == 'changed'
Example of a complete playbook for testing
- hosts: localhost
tasks:
- shell: "sh -c 'cp /tmp/test.ansible /tmp/test.ansible.orig;
echo $RANDOM_TEXT > /tmp/test.ansible;
if (diff /tmp/test.ansible.orig /tmp/test.ansible > /dev/null);
then echo not changed;
else echo changed;
fi'"
environment:
RANDOM_TEXT: "{{ lookup('random_choice', 'AAA', 'BBB') }}"
ignore_errors: true
register: result
changed_when: result.stdout == 'changed'
- debug:
var: result

User prompt between tasks in ansible

I need to throw a user prompt at task level hence, I cannot use var_prompt. For this I have a tiny shell script as follow which is running read command to catch user response and I am calling it from command module but its not working.
cat question.sh
read -r -p "Are you sure? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
echo "do_something"
;;
*)
echo "do_something_else"
;;
esac
My playbook:
---
- hosts: localhost
tasks:
- name: "Do you want to proceed?"
command: bash question.sh
You still need to use var_prompt. Split your play into few, and place var_prompt in between.
Was:
- hosts: foo
tasks:
- name: task1
debug:
- name: asking for foo
debug:
- name: task2
debug:
Become:
- hosts: foo
tasks:
- name: task1
debug:
- hosts: foo
vars_prompt:
- name: foo
prompt: bar
tasks:
- name: task2
debug:

How to put variable in bash script with ansible playbook

I need a put variable in script with an Ansible playbook. I get the variable from Jenkins 'build with parameter' but I can't put a " tag " variable in my script. How can I do that?
This is my script:
export JAVA_HOME=/home/asd/products/app/java8
tag=$(tag)
hzpid=$(ps aux |grep /home/asd/products/app/hzcluster/ |grep -v grep | awk '{print $2}' | sed -r 's/%/ /g')
echo $hzpid
if [[ -z $hzpid ]]; then
echo "no running applications"
else
kill -9 $hzpid && echo "running process terminated."
fi
nohup $JAVA_HOME/bin/java -Dspring.profiles.active=dxl-dev -jar /home/asd/products/app/hzcluster/new/hzcluster-$tag-SNAPSHOT.jar > $LOG_DIR/hzcluster.log &
exit 0
This is my ansible-playbook:
- hosts: '{{ hosts }}'
gather_facts: no
tasks:
- name: Run new hzcluster.jar
shell: sh '{{ sh_file }}'start-hzcluster2.sh '{{ tag }}'
In your shell script change from tag=$(tag) to tag=$1
Playbook:
- hosts: '{{ hosts }}'
gather_facts: no
tasks:
- name: Run new hzcluster.jar
shell: sh '{{ sh_file }}'start-hzcluster2.sh '{{ tag }}'

ansible overwrite variable depending on result

I am running several shell commands in an ansible playbook that may or may not modify a configuration file.
One of the items in the playbook is to restart the service. But I only want to do this if a variable is set.
I am planning on registering a result in each of the shell tasks, but I do not want to overwrite the variable if it is already set to 'restart_needed' or something like that.
The idea is the restart should be the last thing to go, and if any of the commands set the restart variable, it will go, and if none of them did, the service will not be restarted. Here is an example of what I have so far...
tasks:
- name: Make a backup copy of file
copy: src={{ file_path }} dest={{ file_path }}.{{ date }} remote_src=true owner=root group=root mode=644 backup=yes
- name: get list of items
shell: |
grep <file>
register: result
- name: output will be 'restart_needed'
shell: |
NUM=14"s"; if [ "${NUM}" != "s" ]; then sed -i "${NUM}/no/yes/g" {{ file_path }}; echo "restart_needed"; else echo "nothing_changed" ; fi
with_items: "{{ result.stdout_lines }}"
register: output
- name: output will be 'nothing_changed'
shell: |
NUM="s"; if [ "${NUM}" != "s" ]; then sed -i "${NUM}/no/yes/g" {{ file_path }}; echo "restart_needed"; else echo "nothing_changed" ;; fi
with_items: "{{ result.stdout_lines }}"
register: output
- name: Restart service
service: name=myservice enabled=yes state=restarted
In the above example, the variable output will be set to restart_needed after the first task but then will be changed to 'nothing_changed' in the second task.
I want to keep the variable at 'restart_needed' if it is already there and then kick off the restart service task only if the variable is set to restart_needed.
Thanks!
For triggering restarts, you have two options: the when statement or handlers.
When statement example:
tasks:
- name: check if string "foo" exists in somefile
shell: grep -q foo somefile
register: result
- name: restart service
service:
name: myservice
enabled: yes
state: restarted
when: result.rc == 0
Handlers example:
tasks:
- name: check if string "foo" exists in somefile
shell: grep -q foo somefile
register: result
changed_when: "result.rc == 0"
notify: restart service
handlers:
- name: restart service
service:
name: myservice
enabled: yes
state: restarted

Ansible judgment file directory

I want to do such a thing.In the presence of directory skip, otherwise will be created.
For example:
#!/bin/bash
for $i in 'a.test.com a.test.com c.test.com'
do
if [ ! -e $i ]:
then
mkdir $i
fi
done
How to use ansible-playbook implement the above code.
thanks
Looks like simple directory creation using the file module in a loop.
- name: make sure subdomain directories exist
file: path=/opt/{{item}} state=directory recursive=yes
with_items:
- a.test.com
- b.test.com
- c.test.com
If the directory is created, or skip.
- name: test log dir
shell: "test -e /data/{{item[0]}}/{{item[1]}} -o -h /data/{{item[0]}}/{{item[1]}}"
ignore_errors: True
with_nested:
- ['logs', 'html']
- ['a.test.com', 'b.test.com']
register: dir_stats
tags: check_log
- name: create log dir
file: path=/data/{{item.item[0]}}/{{item.item[1]}} state=directory owner=apache group=apache recursive=yes
with_items: dir_stats.results
when: item.rc != 0
tags: check_log

Resources