-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathzos_script.yml
156 lines (137 loc) · 6.87 KB
/
zos_script.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
###############################################################################
# © Copyright IBM Corporation 2023, 2024
###############################################################################
###############################################################################
# This playbook demonstrates how to use zos_script to run local and remote
# scripts in z/OS using Red Hat Ansible Certified Content for IBM Z.
# This playbook is divided up into three independent sections. The tasks in each
# section are grouped into 'blocks' that are tagged. The example below shows how
# the playbook can be run so that only the tasks in the block tagged as
# 'rexx_script' are run.
#
# Usage:
# ansible-playbook -i <inventory> <playbook>
#
# Example:
# ansible-playbook -i inventories zos_script.yml
# ansible-playbook -i inventories zos_script.yml --tags rexx_script
#
# Requirements:
# IBM z/OS core collection 1.8.0 or later.
#
# Configure:
# python_script_dir - Directory where the Python script will be run (mostly
# to test the chdir module option).
# Optional:
# use_custom_msg - Whether to print the custom message or use the default one
# when running the templated shell script.
# custom_msg - Content of the message printed in the templated shell script.
# some_num - A number used as a range to loop over in the templated shell
# script.
# fav_programming_languages - A list of strs which are sorted and then printed
# out in the templated shell script.
###############################################################################
- name: Sample zos_script playbook.
hosts: zos_host
collections:
- "ibm.ibm_zos_core"
gather_facts: false
environment: '{{ environment_vars }}'
vars:
python_script_dir: "/u/user_name"
use_custom_msg: True
custom_msg: "This is a custom Hello World message!!"
some_num: 3
fav_programming_languages:
- C
- rexx
- python
- cobol
tasks:
##############################################################################
# This first block shows how a value can be passed directly to a script as a
# command line arg by using the 'cmd' module option.
# Notice in the output that the value of 'ansible_user' is present.
##############################################################################
- name: Pass a value to a script in-line as a command line arg with the 'cmd'
module option and print the output.
tags: rexx_script
block:
- name: Run a Rexx script which prints a greeting to the user and prints the
current working directory on the remote system.
ibm.ibm_zos_core.zos_script:
cmd: "{{ playbook_dir }}/files/HELLO_USER_PRINT_CWD.rexx name={{ ansible_user }}"
remote_src: false
register: hello_user_output
- name: Print the Rexx script output.
ansible.builtin.debug:
msg: "{{ hello_user_output.stdout_lines }}"
##############################################################################
# This next block explores module options: 'executable' and 'chdir'.
# The default behavior of the zos_script module is to run a script as Rexx.
# The 'executable' module option enables running other types of scripts by
# passing in a path to the appropriate executable. The Ansible inventory
# variable 'ansible_python_interpreter' is used to specify the remote path
# to the python interpreter used by Ansible to run modules. The task below
# reuses this variable in the 'executable' module option since a working
# version of python is guaranteed to be at that location (given that the rest
# of this playbook ran).
# The value set in the module option 'chdir' is set to the value defined above
# in the vars section of this playbook.
# The task also passes in a command line arg just like the previous script.
# Notice in the output that the value of 'current working directory' matches
# what was specified in module option 'chdir'. Compare this to the output from
# the previous block where the 'chdir' module option was not specified.
##############################################################################
- name: Specify a remote directory to run a script in and specify which
program to use to run the script, then print the output.
tags: python_script
block:
- name: Run a Python script, with a specified python executable, in a
specified remote directory, which prints a greeting to the user and
prints the current working directory on the remote system.
ibm.ibm_zos_core.zos_script:
cmd: "{{ playbook_dir }}/files/hello_user_print_cwd.py --name={{ ansible_user }}"
chdir: "{{ python_script_dir }}"
executable: "{{ ansible_python_interpreter }}"
remote_src: false
register: python_output
- name: Print the Python script output.
ansible.builtin.debug:
msg: "{{ python_output.stdout_lines }}"
##############################################################################
# This last block runs a templated shell script.
# First, the script greets the user, prints the directory the playbook was run
# from and prints a number all for which the values are rendered in jinja and
# substitued in. The user and playbook directory values are populated by
# accessing the Ansible magic variables 'ansible_user' and 'playbook_dir'. The
# number is defined above as 'some_num' in the vars section of this playbook.
# Next the script assigns a string value to a variable '$msg' based on a
# jinja-templated conditional block. The value of the conditional is based on
# the boolean value 'use_custom_msg' defined above in the vars section of this
# playbook.
# Then the script uses a jinja-templated loop to iterate over a range from 1
# up to and including the number defined in 'some_num'. Each iteration prints
# out the value of the loop variable as well as the determined value of
# '$msg'.
# Lastly, the script uses another jinja-templated loop to iterate through the
# 'fav_programming_languages' list, sorts it in reverse alphabetical order,
# and prints the sorted list.
##############################################################################
- name: Run a templated shell script which leverages jinja templating to
substitute values in a string, to set up and evaluate a conditional
block, and to loop through both a range and a list.
tags: shell_script
block:
- name: Run a templated shell script.
ibm.ibm_zos_core.zos_script:
cmd: "{{ playbook_dir }}/files/templated_loops_and_conditional_sample.sh"
executable: /bin/sh
remote_src: false
use_template: true
template_parameters:
keep_trailing_newline: true
register: output
- name: Print shell script output.
ansible.builtin.debug:
msg: "{{ output.stdout_lines }}"