ansible 运行持续进程及配置环境变量

Linux Ops Ansible ansible 运行持续进程及配置环境变量 配置环境变量 ansible 是通过 ssh 登陆的,同时也是 Non-Login 的方式登陆,这种登陆情况下,部分环境参数是拿不到的。Login 和 Non-Login 的区别可以参考 Difference between Login shell and Non login shell。 那么如果一行 shell 运行的指令需要用到环境参数该怎么处理? 根据 Non-Login shell 调用顺序,~/.bashrc 是会被调用的,所以可以想办法把参数文件写在 ~/.bashrc 中,这样就可以运行 shell。 另一种思路是,不想破坏系统的 ~/.bashrc 文件,或者环境变量本身也是经常变的,那么就自制一个文件,通过 source 启用,设想很好,但是实际操作不行,我这边的环境(ubuntu 18.04 server)提示 source:command not found。有些解释是,ansible 的 shell 不是 bash,没有 bash 的特性。 但 source 的方法不行,还有另一个方法,启用 environment。 --- - hosts: all remote_user: test gather_facts: True tasks: # 在需要环境的任务下,一一指定变量,在执行过程中会导入这些变量 - name: some command need environment shell: cmd: xxx yyy zzz chdir: /your/path environment: ARGS1: 1 ARGS: 2 后台持续运行 后台持续运行的方法很多,正规点的利用 supervisord,systemd 配置一个服务,让服务在后台运行,但这需要配置文件,有没有更好的处理方法,nohup 能否在 ansible 中使用,答案是:可以。 ...

api doc的使用和部署

[[apiDoc]] api doc 的使用和部署 做 web 应用,总绕不开前后端数据接口的对接。如何才能写出一份简单、易于维护的 api 文档? 这时候 api doc 就出现了。 apiDoc 是一个简单的 RESTful API 文档生成工具,它从代码注释中提取特定格式的内容,生成文档。 目前支持支持以下语言:C#、C/C++、D、Erlang、Go、Groovy、Java、JavaScript、Pascal/Delphi、 Perl、PHP、Python、Ruby、Rust、Scala 和 Swift。 以下的使用示例以 python 语言为主。 以下是官网APIDOC。 安装 npm install apidoc -g 设置apiDoc配置 在项目文件夹根目录创建文件 apidoc.json 文件。 里面的内容是对当前项目的描述。 其中 header 或者 footer 可以指向 md 文件,可以记录头信息和尾部信息。 { "name": "example", "version": "0.1.0", "description": "apiDoc basic example", "title": "Custom apiDoc browser title", "url" : "https://api.github.com/v1", "header": { "title": "Overview", "filename": "header.md" }, "footer": { "title": "Copyright", "filename": "footer.md" } } 创建 header.md 和 footer.md (非必须) 生成 api doc apidoc -i myapp/ -o apidoc/ -i 项目文件夹 -o apidoc文件夹 生成好了,就可以打开 apidoc/ 文件夹下的 index.html 就可以了。 ...

arp 实现网络传输速率倍增

Linux Network arp 实现网络传输速率倍增 测试目标 最近想要突破千兆交换机给定的极限,尝试过 bond6 实现,发现还是需要万兆交换机支持。 换个思路,先确定下目前的需求是什么: 一个主节点,配置了五个网口,每个网口配置一个 ip,定义为 192.168.1.1-5 五个普通节点,分别有一个网口且分配一个 ip,定义为 192.168.1.11-15 现在想达到的目标是: - 五个普通节点分别向主节点的不同 ip 发数据,使得主节点的网络吞吐(进口)达到 5000 Mb/s - 五个普通节点分别要求从主节点的不同 ip 取数据,使得主节点的网络吞吐(出口)达到 5000 Mb/s 测试方案也很简单,就是讲一个大文件通过 scp 传递或者要求接收的方式判断网络吞吐。 直观来看,这两个目标很容易达到,虽然连接在千兆交换机上,但是主节点连了五根线,理论可以达到 5000 Mb/s的。 实际测试过程 定义几个概念: 主节点称为 m 主节点的网口带上数字,记为 m1-m5 普通节点称为 s,五个即 s1-25 普通节点只有一个网络端口,所以网口和普通节点同名 定义两个测试过程: s1-s5 分别向 m1-m5 发送数据,记为实验一 s1-s5 分别要求 m1-m5 发送数据,记为实验二 实验结果是: 实验一的结果是,五个发送请求的带宽总和是 3000Mb/s 实验二的结果是,五个拉取请求的带宽总和是 1000Mb/s 这个结果令我有些不解,开始用工具监控数据,但不再聚焦于五个普通节点,而是专门去监控主节点的五个网口。 分析过程 主节点的五个网口的数据,实验一中,m1:1000,m2:1000,m3:3000,m4:0,m5:0;实验二中m3:1000,其他都是0。 分析到这里能解释实验现场,但是根本原因还不明确,往底层去看,可能是网络层和链路层的问题,随之想到了 arp 协议。 arp 协议定义了 ip 和 mac 地址的关系,先查下每台机器的 arp 信息,下面是大致的信息: ...

bash shell buildin command

Linux Shell bash shell buildin command 这里肤浅的以 bash 为例,实际概念应该是 shell buildin commond。 前几天,突发奇想,Linux 的可执行命令,例如 ls,cat,echo 都是可执行文件,一般在 /bin/ 下。但是 cd 却一直找不到(Ubuntu 18.04 server),通过 find 全盘搜索也找不到。 昨天在处理一个系统环境的问题时,发现 source 也找不到所在路径,就很好奇这到底是怎么了,系统如何知道 cd 或者 source 在哪里,在 path 路径中完全找不到。 在查找资料中,想起了一件事,所谓的 Linux 系统,是指 Linux 内核,而人和 Linux 打交道是通过 shell 实现的。所以 shell 可以更人性化,例如正则批量。而 source 也是 bash 的一个特性,用来导入环境变量。 可以通过 type 判断一个命令属于什么。 type: type [-afptP] name [name ...] Display information about command type. For each NAME, indicate how it would be interpreted if used as a command name. Options: -a display all locations containing an executable named NAME; includes aliases, builtins, and functions, if and only if the `-p' option is not also used -f suppress shell function lookup -P force a PATH search for each NAME, even if it is an alias, builtin, or function, and returns the name of the disk file that would be executed -p returns either the name of the disk file that would be executed, or nothing if `type -t NAME' would not return `file' -t output a single word which is one of `alias', `keyword', `function', `builtin', `file' or `', if NAME is an alias, shell reserved word, shell function, shell builtin, disk file, or not found, respectively Arguments: NAME Command name to be interpreted. Exit Status: Returns success if all of the NAMEs are found; fails if any are not found. $ type -a cd cd is a shell builtin $ type -a source source is a shell builtin $ type -a echo echo is a shell builtin echo is /bin/echo 可以看到 echo 是内建命令,但也是系统命令,那么会使用哪一个呢。 ...

bumpversion的简单学习

Python Python Module bumpversion 的简单学习 如果只是闭门造车,写点小脚本的话可以不用在意版本的问题。 但是一旦脚本或者项目需要被他人使用,如果没有版本信息,又出现了 bug,那么你很难定位他使用的是什么版本,这个 bug 到底有没有修复,有了版本信息你可以很确定一些 feather 或者 bug 是否实现。 最简单的版本信息制作,在项目下新建一个 VERSION 文件,在该文件中记录版本号,做了些代码修改就手动修改 VERSION。 但是这样会带来更多问题,你只能在修改过 VERSION 后做个 git commit 才能够更好地跟踪;多个文件会提到版本信息该怎么处理;大版本和 patch 版本如何区分? bumpversion 横空出世,给上述的问题给了些答案。Bumpversion 是一个简化工程版本号修改的工具,可以通过 pip 安装。使用它可以一键将当前工程的文件中的旧版本号替换成新版本号还可以顺便 Commit 和 打上 Tag(如有需要)。 语法 bumpversion [options] part [file] 这里面 part 是关键,它可以简化很多事。 使用 先看一个简单的例子: 工程里有 main.py 和 Makefile 两个文件。 main.py 打印当前的版本号: version = '0.0.1' if __name__ == '__main__': print(version) Makefile 将 py 文件压缩起来: VERSION = 0.0.1 .PHONY: build build: tar zcvf tellversion-v$(VERSION).tar.gz main.py 要使用 Bumpversion 需要在目录里添加 .bumpversion.cfg 文件: ...

centos-root LVM 扩容问题

Linux [[CentOS]] file system centos-root LVM 扩容问题 最近开了一个 jumpserver 服务,突然挂了,一查原来是系统的根目录只有50G,数据写不下了,只剩 40k 的空间。好在 centos7 默认使用 LVM 进行文件系统的挂载,那只有加盘了。 加上 1 TiB 的硬盘,通过 pvcreate /dev/sdb ,出现 Device /dev/sdb excluded by a filter。此时因为该盘可能因为有分区,所以不行,可以尝试 wipefs -a /dev/sdb 来解决问题。 然后再让 vg:centos 增加一块 pv :vgextend centos /dev/sdb。此时可能会出现 Couldn't create temporary archive name。这是因为根目录可用空间太少了,删点日志再执行这个指令就可以了。 接着 vg 就有可用空间了,使用 lvextend 增加容量:lvextend -L +1024G /dev/mapper/centos-root。这里一般不会有什么问题,会直接通过。 但是上部操作完成后,查看 df -h 还是只有 50G,因为没有让系统知道这件事。国内的资料显示需要执行:e2fsck -f /dev/mapper/centos-root 和 resize2fs /dev/mapper/centos-root。但我执行都不可以,会说 root 已经挂载,那么只能在维护模式下使用?那不就还是要重启? 在一篇英文资料中提到,可以执行 fsadm resize /dev/centos/root 通知系统文件系统大小已经变更。 ...

ceph 安装细节

Linux [[Ceph]] ceph 安装细节 根据 存储集群快速入门 进行操作。对内部一些没说明白的进行补充。 创建独立用户 useradd -m -s /bin/bash storage echo "storage ALL = (root) NOPASSWD:ALL" | tee /etc/sudoers.d/storage chmod 0440 /etc/sudoers.d/storage passwd storage 块设备使用 文件系统使用 常见问题 librados: client.bootstrap-osd authentication error 问题 参考: 解决。是base64加密的问题。 $ ceph auth get client.bootstrap-osd --name mon. --keyring /var/lib/ceph/mon/ceph-ceph211/keyring exported keyring for client.bootstrap-osd [client.bootstrap-osd] key = AQCi8/peqAO2FRAAyVYDh3SoE2MuTe8hmv0mzg== caps mon = "allow profile bootstrap-osd" ceph HEALTH_WARN no active mgr ceph-deploy mgr create host-name RBD image feature set mismatch 参考: RBD kernel module fails to map an image to a block device。 ...

Cobbler 安装与配置

Linux Ubuntu [[Cobbler]] Cobbler 安装与配置 Cobbler 介绍 Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等。 Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。 Cobbler是较早前的kickstart的升级版,优点是比较容易配置,还自带web界面比较易于管理。 Cobbler内置了一个轻量级配置管理系统,但它也支持和其它配置管理系统集成,如Puppet,暂时不支持SaltStack。 安装 以下操作,皆在 CentOS Linux release 7.7.1908 (Core) 实现。 检测基础环境 # 查看 selinux 和防火墙是否关闭 [root@Cobbler ~] getenforce Disabled [root@Cobbler ~] systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:firewalld(1) 如果 getenfore 的结果不是 Disabled,那么根据 getenforce命令 去关闭 SELinux。 更新yum源 [root@Cobbler ~] curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo [root@Cobbler ~] curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo [root@Cobbler ~] yum clean all [root@Cobbler ~] yum makecache 安装 cobbler [root@Cobbler ~] yum -y install cobbler cobbler-web dhcp tftp-server pykickstart httpd 启动服务 [root@Cobbler ~] systemctl start httpd.service [root@Cobbler ~] systemctl start cobblerd.service cobbler check [root@Cobbler ~] cobbler check 配置 cobbler 开机自启 $ chkconfig httpd on $ chkconfig cobblerd on $ chkconfig dhcpd on $ /etc/init.d/httpd restart $ /etc/init.d/cobblerd restart $ /etc/init.d/dhcpd restart 配置 cobbler [root@Cobbler ~] cobbler check The following are potential configuration items that you may want to fix: 1 : The 'server' field in /etc/cobbler/settings must be set to something other than localhost, or kickstarting features will not work. This should be a resolvable hostname or IP for the boot server as reachable by all machines that will use it. 2 : For PXE to be functional, the 'next_server' field in /etc/cobbler/settings must be set to something other than 127.0.0.1, and should match the IP of the boot server on the PXE network. 3 : change 'disable' to 'no' in /etc/xinetd.d/tftp 4 : Some network boot-loaders are missing from /var/lib/cobbler/loaders, you may run 'cobbler get-loaders' to download them, or, if you only want to handle x86/x86_64 netbooting, you may ensure that you have installed a *recent* version of the syslinux package installed and can ignore this message entirely. Files in this directory, should you want to support all architectures, should include pxelinux.0, menu.c32, elilo.efi, and yaboot. The 'cobbler get-loaders' command is the easiest way to resolve these requirements. 5 : enable and start rsyncd.service with systemctl 6 : debmirror package is not installed, it will be required to manage debian deployments and repositories 7 : The default password used by the sample templates for newly installed machines (default_password_crypted in /etc/cobbler/settings) is still set to 'cobbler' and should be changed, try: "openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'" to generate new one 8 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them Restart cobblerd and then run 'cobbler sync' to apply changes. 上面指出了八项问题,逐一解决。 ...

cobbler 服务在 docker 下运行

Linux [[Docker]] Cobbler cobbler 服务在 docker 下运行 主要参考 Cobbler 批量装机。 在宿主机上的 /mnt 内挂载镜像。 $ sudo mkdir /mnt/ubuntu18.04 $ sudo mkdir /mnt/centos $ sudo mount -t iso9660 -r -o ro,loop /tmp/ubuntu-18.04.3-server-amd64.iso /mnt/ubuntu18.04 $ sudo mount -t iso9660 -r -o ro,loop /tmp/CentOS-7-x86_64-DVD-1908.iso /mnt/centos 编辑自动应答脚本 ubuntu1804.seed: d-i debian-installer/locale string en_US.UTF-8 d-i console-setup/ask_detect boolean false d-i keyboard-configuration/layoutcode string us d-i keyboard-configuration/variantcode string d-i netcfg/choose_interface select auto d-i netcfg/get_hostname string $myhostname d-i netcfg/get_nameservers string 192.168.31.1 d-i netcfg/get_ipaddress string d-i netcfg/get_netmask string 255.255.255.0 d-i netcfg/get_gateway string 192.168.31.1 d-i netcfg/confirm_static boolean true d-i netcfg/without_default_route boolean true d-i netcfg/enable boolean false d-i time/zone string Asia/Shanghai d-i clock-setup/utc boolean true d-i clock-setup/ntp boolean true d-i clock-setup/ntp-server string ntp.ubuntu.com d-i mirror/country string manual d-i mirror/http/hostname string $http_server d-i mirror/http/directory string $install_source_directory d-i mirror/http/proxy string d-i live-installer/net-image string http://$http_server/cobbler/links/$distro_name/install/filesystem.squashfs # d-i partman-auto/disk string /dev/sda # d-i partman-auto/method string regular # d-i partman-auto/choose_recipe select fsm # d-i partman-lvm/device_remove_lvm boolean true # d-i partman-md/device_remove_md boolean true # d-i partman-auto/expert_recipe string \ # fsm :: \ # 1024 100% 1024 linux-swap method{ swap } \ # format{ } \ # . \ # 20480 20480 20480 ext4 method{ format } \ # mountpoint{ /tmp } \ # format{ } use_filesystem{ } filesystem{ ext4 } \ # options/relatime{ relatime } \ # . \ # 1 2048 1000000000 ext4 method{ format } \ # mountpoint{ /data } \ # format{ } use_filesystem{ } filesystem{ ext4 } \ # options/relatime{ relatime } \ # . # d-i partman-lvm/confirm_nooverwrite boolean true # d-i partman-lvm/confirm boolean true # d-i partman-partitioning/confirm_write_new_label boolean true # d-i partman/confirm_nooverwrite boolean true # d-i partman/confirm boolean true # d-i partman/choose_partition \ # select Finish partitioning and write changes to disk d-i passwd/root-login boolean true d-i passwd/root-password-crypted paddssword $1$root$6lvA6eQ6m1Qum8aZ4VWPV1 d-i passwd/make-user boolean true d-i passwd/user-fullname string firefly d-i passwd/username string firefly d-i passwd/user-password-crypted password $1$firefly$AbmnMjNadI/O7S/2vlojK. d-i passwd/user-uid string d-i passwd/user-default-groups string sudo adm cdrom dialout lpadmin plugdev sambashare d-i user-setup/allow-password-weak boolean false d-i user-setup/encrypt-home boolean false d-i apt-setup/services-select multiselect security d-i apt-setup/security_host string mirrors.aliyun.com d-i apt-setup/security_path string /ubuntu d-i debian-installer/allow_unauthenticated string false $SNIPPET('preseed_apt_repo_config') d-i pkgsel/include string ntp ssh wget vim d-i pkgsel/include string vim openssh-server d-i grub-installer/skip boolean false d-i lilo-installer/skip boolean false d-i grub-installer/only_debian boolean true d-i grub-installer/with_other_os boolean true d-i finish-install/keep-consoles boolean false d-i finish-install/reboot_in_progress note d-i cdrom-detect/eject boolean true d-i debian-installer/exit/halt boolean false d-i debian-installer/exit/poweroff boolean false d-i preseed/early_command string wget -O- \ http://$http_server/cblr/svc/op/script/$what/$name/?script=preseed_early_default | \ /bin/sh -s d-i preseed/late_command string wget -O /target/etc/apt/sources.list http://$http_server/sources.list ; \ wget -O /target/etc/locale.conf http://$http_server/locale.conf ; \ wget -O /target/etc/default/locale http://$http_server/locale ; \ cd /target ; \ chroot ./ apt-get update 启动虚拟机,并进入。 ...

CTF(鶸)密码学

Python Algorithm CTF(鶸)密码学 一、摩斯密码 1、特点 题面只有三个值。 2、解题思路 转换成 ascii,出现 flag 标识符即结束,否则根据转后的数据进行下一步处理。 二、栅栏密码 1、特点 密文字符串出间隔性的出现 flag 的标识符。 2、解题思路 分栏破译。 def inputData(): string = input("请输入栅栏加密的文字:") code = input("请输入分栏:(0代表从2到6逐个分栏):") code = int(code) return string,code def code2(string): string_temp = [] string_temp.append(string[0::2]) string_temp.append(string[1::2]) print("分成2栏的结果是:%s" % (''.join(string_temp))) def code3(string): string_temp = [] string_temp.append(string[0::3]) string_temp.append(string[1::3]) string_temp.append(string[2::3]) print("分成3栏的结果是:%s" % (''.join(string_temp))) def code4(string): string_temp = [] string_temp.append(string[0::4]) string_temp.append(string[1::4]) string_temp.append(string[2::4]) string_temp.append(string[3::4]) print("分成4栏的结果是:%s" % (''.join(string_temp))) def code5(string): string_temp = [] string_temp.append(string[0::5]) string_temp.append(string[1::5]) string_temp.append(string[2::5]) string_temp.append(string[3::5]) string_temp.append(string[4::5]) print("分成5栏的结果是:%s" % (''.join(string_temp))) def code6(string): string_temp = [] string_temp.append(string[0::6]) string_temp.append(string[1::6]) string_temp.append(string[2::6]) string_temp.append(string[3::6]) string_temp.append(string[4::6]) string_temp.append(string[5::6]) print("分成6栏的结果是:%s" % (''.join(string_temp))) def main(): string, code = inputData() if (code == 0): code2(string) code3(string) code4(string) code5(string) code6(string) elif (code == 2): code2(string) elif (code == 3): code3(string) elif (code == 4): code4(string) elif (code == 5): code4(string) elif (code == 6): code4(string) else: print("error") if __name__ == "__main__": main() 三、Rot13 1、特点 凯撒加密的第十二种方式,但是可以字符和数字混合在其中。 ...