Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
so-operation-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
smart-operation
so-operation-api
Commits
5c4eb808
Commit
5c4eb808
authored
Jul 03, 2023
by
魏灿
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
hosts转json
parent
f6bd70dc
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
0 deletions
+46
-0
src/common/tools/hosts
src/common/tools/hosts
+6
-0
src/common/tools/hosts.go
src/common/tools/hosts.go
+40
-0
No files found.
src/common/tools/hosts
0 → 100644
View file @
5c4eb808
[webGroup1]
192.168.0.11 ansible_ssh_host=192.168.0.11 ansible_ssh_user=root ansible_ssh_pass=123.com ansible_ssh_port=3333
192.168.0.12 ansible_ssh_host=192.168.0.12 ansible_ssh_user=root ansible_ssh_pass=1234.com ansible_ssh_port=2222
[webGroup2]
192.168.1.11 ansible_ssh_host=192.168.1.11 ansible_ssh_user=admin ansible_ssh_pass=ccc.com ansible_ssh_port=11
192.168.1.12 ansible_ssh_host=192.168.1.12 ansible_ssh_user=admin ansible_ssh_pass=111.com ansible_ssh_port=22
\ No newline at end of file
src/common/tools/hosts.go
0 → 100644
View file @
5c4eb808
package
tools
import
(
"bufio"
"log"
"os"
"strings"
)
func
HostsToJson
()
(
data
map
[
string
][]
string
,
err
error
)
{
f
,
err
:=
os
.
Open
(
`/etc/ansible/hosts`
)
if
err
!=
nil
{
return
nil
,
err
}
defer
func
()
{
if
err
=
f
.
Close
();
err
!=
nil
{
log
.
Fatal
(
err
)
}
}()
data
=
make
(
map
[
string
][]
string
)
// 以这个文件为参数,创建一个 scanner
s
:=
bufio
.
NewScanner
(
f
)
var
key
string
var
per
[]
string
// 扫描每行文件,按行读取
for
s
.
Scan
()
{
if
strings
.
HasPrefix
(
s
.
Text
(),
"["
)
&&
strings
.
HasSuffix
(
s
.
Text
(),
"]"
)
{
key
=
s
.
Text
()
per
=
[]
string
{}
data
[
key
]
=
per
}
else
{
data
[
key
]
=
append
(
data
[
key
],
s
.
Text
())
}
}
err
=
s
.
Err
()
if
err
!=
nil
{
return
nil
,
err
}
return
data
,
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment