-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_playbook.yml
106 lines (83 loc) · 2.63 KB
/
example_playbook.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
---
# Sample Playbook for "sh" Ansible Module
- name: Test Play
hosts: ubuntu, centos, controller
become: true
tasks:
###################################### Example 1
- name: normal # Execute the command without condition
sh:
cmd: 'hostname'
register: debug
- debug:
var: debug
###################################### Example 2
- name: run command basd of the "stdout" of another command
sh:
cmd: 'hostnamectl'
condition: 'hostname'
lang: bash
if_stdout: 'ansible'
register: debug
- debug:
var: debug
###################################### Example 3
- name: run command based of the "rc" of another command # multi line
sh:
cmd: |
export pass='password'
export users_file=/root/users.txt
echo 'user-test' > $users_file
if [ -f $1 ] ; then
for i in $( cat /root/users.txt ); do
useradd "$i"
echo "User $i created successfully"
echo $pass | passwd "$i" --stdin
done
else
echo "Input is NOT a file"
fi
condition: 'cat /etc/passwd | grep user-test'
lang: bash
if_rc: 1
register: multi_line
- debug:
var: multi_line
###################################### Example 4
- name: run command based on Python code # if condition-command-stdout = 'open'
sh:
cmd: echo 'Execute some commands '
condition: |
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1',22))
if result == 0:
print "open"
else:
print "not-open"
sock.close()
lang: python
if_stdout: 'open'
register: test_python
- debug:
var: test_python
###################################### Example 5
- name: run command based on Python code 2 # if condition-command-rc = 1
sh:
cmd: echo 'Execute some commands '
condition: |
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1',24))
if result == 0:
print "open"
sock.close()
else:
print "not-open"
sock.close()
exit(1)
lang: python
if_rc: 1
register: test_python2
- debug:
var: test_python2