-
Notifications
You must be signed in to change notification settings - Fork 1
/
8.Conditionals.yml
89 lines (82 loc) · 2.72 KB
/
8.Conditionals.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
--- # ------------------------------
# The given playbook attempts to start mysql service on all_servers.
# Use the when condition to run this task if the host (ansible_host) is the database server.
# Refer to the inventory file to identify the name of the database server.
# ------------------------------
# BEFORE
# ------------------------------
#-
# name: Execute a script on all web server nodes
# hosts: all_servers
# tasks:
# -
# service: name=mysql state=started
#
# ------------------------------
# AFTER
# ------------------------------
- name: Execute a script on all web server nodes
hosts: all_servers
tasks:
- service: name=mysql state=started
when: ansible_host=='server4.company.com'
# The playbook has a variable defined - age. The two tasks attempt to print if I am a child or an Adult.
# Use the when conditional to Print if I am a child or an Adult based on weather my age is < 18 (child)
# or >= 18 (Adult).
# Refer to the inventory file to identify the name of the database server.
# ------------------------------
# BEFORE
# ------------------------------
#-
# name: Am in an Adult or a Child
# hosts: localhost
# vars:
# age: 25
# tasks:
# -
# command: echo "I am a Child"
#
# -
# command: echo "I am an Adult"
#
# ------------------------------
# AFTER
# ------------------------------
- name: Am in an Adult or a Child
hosts: localhost
vars:
age: 25
tasks:
- command: echo "I am a Child"
when: age<18
- command: echo "I am an Adult"
when: age>=18
# The given playbook attempts to add an entry into the resolv.conf file for nameserver.
# First, we run a command to get the contents of resolv.conf file and then we add a new
# line containing the name server data into the file
# However, when this playbook is run multiple times,
# it keeps adding new entries of same line into the resolv.conf file.
# Add a register directive to store the output of the first command and add a
# conditional to the second command to check if the output NOT contains the name server ("10.0.250.10") already
# ------------------------------
# BEFORE
# ------------------------------
#-
# name: Add name server entry if not already entered
# hosts: localhost
# tasks:
# - command: cat /etc/resolv.conf
#
# -
# command: echo "nameserver 10.0.250.10" >> /etc/resolv.conf
#
# ------------------------------
# AFTER
# ------------------------------
- name: Add name server entry if not already entered
hosts: localhost
tasks:
- command: cat /etc/resolv.conf
register: commnd_output
- command: echo "nameserver 10.0.250.10" >> /etc/resolv.conf
when: command_output.stdout.find('10.0.250.10') == -1