-
Notifications
You must be signed in to change notification settings - Fork 0
/
facts2mediawiki.sh
executable file
·207 lines (165 loc) · 6.44 KB
/
facts2mediawiki.sh
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
# Create pages on Mediawiki with information from Ansible
# Create a config file with the following keys:
#ANSIBLE_DIR="/opt/ansible/projects/myproject"
#FACTS_CACHE="$ANSIBLE_DIR/cache"
#MAIN_PLAYBOOK="main.yml"
#ANSIBLE_VAULT_PASSWORD_FILE=".ansible-vault"
#
# ARA reporting settings
#ARA_SERVER=https://ara.example.nl
# How many hosts to show
#LIMIT=20
# Extra options required for ARA api client
#ARA_OPTIONS='--insecure' # Run `ara host list help` for more information
#
# MediaWiki settings
# for direct use with Mediawiki
#WIKI="root@wiki"
# Copy the files to your docker volume for processing.
# This requires a docker image that periodically imports
# all files in the configured location
#DOCKER_STORAGE="root@docker:/some/docker/volume"
. /opt/ansible/facts2mediawiki.conf
export ANSIBLE_VAULT_PASSWORD_FILE
# Load GPG agent's soocket if it isn't there
[ -z "$SSH_AUTH_SOCK" ] && export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
cd $ANSIBLE_DIR
# The following variable controls when the page of a host should be stale and marked as such
STALE=$(date -d '2 weeks ago' +%s)
###
# Generate per host status page
###
for HOST_NAME in $(/usr/bin/ansible-playbook playbooks/$MAIN_PLAYBOOK --list-hosts | tail -n+7)
do
[[ $HOST_NAME == '' ]] && continue
STALE_NOTE=''
[[ ! -f $FACTS_CACHE/$HOST_NAME ]] && {
cat <<EOF >/tmp/INV:${HOST_NAME}.wiki
Hostname: ${HOST_NAME%%.*}
No further information available, this host hasn't been inventoried by Ansible just yet.
Inventory date: $(date "+%Y/%m/%d %H:%M:%S")
This page is generated automatically, edits will be lost.
[[Category: Systems]]
[[Category: Unknown]]
EOF
continue
}
cd $FACTS_CACHE
HOST_FQDN=$(cat $HOST_NAME | jq .ansible_fqdn)
HOST_DISTRO=$(cat $HOST_NAME | jq .ansible_distribution)
HOST_DISTRO_MAJOR=$(cat $HOST_NAME | jq .ansible_distribution_major_version)
HOST_DISTRO_VERSION=$(cat $HOST_NAME | jq .ansible_distribution_version)
HOST_MEMORY=$(cat $HOST_NAME | jq .ansible_memtotal_mb)
HOST_VIRT=$(cat $HOST_NAME | jq .ansible_virtualization_type)
HOST_ARCH=$(cat $HOST_NAME | jq .ansible_architecture)
HOST_CPUS=$(cat $HOST_NAME | jq .ansible_processor_vcpus)
DATE=$(cat $HOST_NAME | jq -r .ansible_date_time.epoch)
DATE_HUMAN=$(date -d@$DATE "+%Y/%m/%d %H:%M:%S")
ACTIVE="Active"
[ $DATE -lt $STALE ] && {
STALE_NOTE="This information is considered stale, the host might not be active anymore"
ACTIVE="Stale"
}
echo $HOST_DISTRO >> /tmp/distrolist.wiki
cat <<EOF >/tmp/INV:${HOST_NAME}.wiki
Hostname: ${HOST_NAME%%.*}
FQDN: $HOST_FQDN
OS: $HOST_DISTRO ${HOST_DISTRO_VERSION}
Hardware type: $HOST_VIRT
Architecture: $HOST_ARCH
CPU Sockets: $HOST_CPUS
RAM: $HOST_MEMORY MB
Inventory date: $DATE_HUMAN
$STALE_NOTE
This page is generated automatically, edits will be lost.
[[Category: Systems]]
[[Category: ${HOST_DISTRO}-${ACTIVE}]]
[[Category: ${ACTIVE}]]
[[Category: ${HOST_DISTRO}]]
[[Category: ${HOST_DISTRO}-${HOST_DISTRO_MAJOR}]]
[[Category: ${HOST_ARCH}]]
EOF
done
###
# Generate Systems page
###
cat <<EOF >/tmp/Category:Systems.wiki
WARNING: These pages are generated automatically!
This page contains an overview of all systems known to Ansible.
At the bottom of this page is an overview per OS using categories.
[[Category: Unknown]]
[[Category: x86_64]]
[[Category: i386]]
[[Category: Active]]
[[Category: Stale]]
$(for DISTRO in $(cat /tmp/distrolist.wiki | sort -u)
do
echo \[\[Category:$DISTRO\]\]
done)
EOF
cat <<EOF >/tmp/Category:Stale.wiki
WARNING: These pages are generated automatically!
The systems on this page have not been inventoried by Ansible in the last 14 days and may no longer be active.
$(for DISTRO in $(cat /tmp/distrolist.wiki | sort -u)
do
echo \[\[Category:${DISTRO}-Stale\]\]
done)
EOF
cat <<EOF >/tmp/Category:Active.wiki
WARNING: These pages are generated automatically!
The systems on this page have been inventoried by Ansible in the last 14 days.
$(for DISTRO in $(cat /tmp/distrolist.wiki | sort -u)
do
echo \[\[Category:${DISTRO}-Active\]\]
done)
EOF
# ARA report generation
ARA_OPTS="--server $ARA_SERVER --client http $ARA_OPTIONS"
cat <<EOF >/tmp/REP:Ansible_changed_systems.wiki
WARNING: This page is generated automatically!
The following list are the most recent $LIMIT systems that have had changes in their Ansible runs
== Changed hosts ==
{| class="wikitable"
! Date !! Hostname !! Link to report
EOF
/usr/local/bin/ara host list $ARA_OPTS --with-changed -c name -c playbook -c id -c updated --limit $LIMIT -f value |
awk -v ara="$ARA_SERVER" '{ printf("|- \n| %s || %s || [%s/playbooks/%s.html?host=%s&changed=true#results ARA]\n", $4, $2, ara, $3, $1) }' >> /tmp/REP:Ansible_changed_systems.wiki
echo '|}' >> /tmp/REP:Ansible_changed_systems.wiki
cat <<EOF >>/tmp/REP:Ansible_changed_systems.wiki
== Failed hosts ==
The following list are the most recent $LIMIT systems that have had errors in their Ansible runs
{| class="wikitable"
! Date !! Hostname !! Link to report
EOF
/usr/local/bin/ara host list $ARA_OPTS --with-failed --without-unreachable -c name -c playbook -c id -c updated --limit $LIMIT -f value |
awk -v ara="$ARA_SERVER" '{ printf("|- \n| %s || %s || [%s/playbooks/%s.html?host=%s&status=failed#results ARA]\n", $4, $2, ara, $3, $1) }' >> /tmp/REP:Ansible_changed_systems.wiki
echo '|}' >> /tmp/REP:Ansible_changed_systems.wiki
cat <<EOF >>/tmp/REP:Ansible_changed_systems.wiki
== Unreachable hosts ==
The following list are the most recent $LIMIT systems that have been unreachable for their Ansible runs
{| class="wikitable"
! Date !! Hostname !! Link to report
EOF
/usr/local/bin/ara host list $ARA_OPTS --without-failed --with-unreachable -c name -c playbook -c id -c updated --limit $LIMIT -f value |
awk -v ara="$ARA_SERVER" '{ printf("|- \n| %s || %s || [%s/playbooks/%s.html?host=%s&status=unreachable#results ARA]\n", $4, $2, ara, $3, $1) }' >> /tmp/REP:Ansible_changed_systems.wiki
echo '|}' >> /tmp/REP:Ansible_changed_systems.wiki
sed -i 's/"//g' /tmp/*.wiki
# Copy the files to your wiki server and process them.
function upload_wiki() {
scp -q /tmp/*.wiki $WIKI:/tmp
ssh $WIKI "
/usr/bin/php \
/opt/mediawiki/maintenance/importTextFiles.php \
-u Ansible \
--bot \
--overwrite \
/tmp/*.wiki 2>/dev/null
rm /tmp/*.wiki"
}
function upload_docker() {
scp -q /tmp/*.wiki $DOCKER_STORAGE
}
[[ -n "$WIKI" ]] && upload_wiki
[[ -n "$DOCKER_STORAGE" ]] && upload_docker
rm /tmp/*.wiki