Linux大杂烩

常用命令

vi打开时快速移动光标,shift+g移动到最后一行,shift+4移动到行尾

du -sh xx*
ssh root@xxx ls -l /root/foo.txt
-rwxr-xr-x. 1 foo foo 0 May 22 17:08 /root/foo.txt
A=$(sed -n '$=' aa.txt)
sed $(($A-倒数行数+1)),${A}d aa.txt
增加-i删除源文件上对应行数
定时任务
crontab [-u username] [-l|-e|-r]
https://www.cnblogs.com/xd502djj/p/4292781.html
参数:
-u: 只有root才能进行这个任务,也即帮其他用户新建/删除crontab工作调度;
-e: 编辑crontab 的工作内容;
-l: 查阅crontab的工作内容;
-r: 删除所有的crontab的工作内容,若仅要删除一项,请用-e去编辑。

0 12 * * * /usr/bin/systemctl restart NetworkManager
ansible 环境 virtualenv
yum install -y gcc make gcc-c++ 
yum install zlib zlib-devel -y
yum install openssl-devel
yum install python-jinja2

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
wget http://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
tar xf Python-3.6.5.tar.xz
cd Python-3.6.5
./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall
which pip3.6
ln -s /usr/local/bin/pip3.6 /usr/local/bin/pip
pip install virtualenv
pip install --proxy http://xxx:xxx%23%40xx@xx.x.x.x:8080 --trusted-host pypi.tuna.tsinghua.edu.cn --index-url https://pypi.tuna.tsinghua.edu.cn/simple/

useradd deploy
su - deploy
virtualenv -p /usr/local/bin/python3.6 .py3-a2.5-env
cd /home/deploy/.py3-a2.5-env
(root)#yum -y install git nss curl
git clone https://github.com/ansible/ansible.git
source /home/deploy/.py3-a2.5-env/bin/activate
(xxxxx-env) pip install paramiko PyYAML jinja2
mv ansible .py3-a2.5-env/
cd .py3-a2.5-env/ansible
git checkout stable-2.5
source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
ansible --version
(deploy~$)下
.
├── deploy.yml
├── inventory
│   └── testenv
└── roles
    └── testbox
        └── tasks
            └── main.yml

testenv:

[testservers]
10.4.11.111

[testservers:vars]
server_name=xxxx
user=root
output=/root/test.txt

main.yml

- name: Print xxxx
  shell: "echo 'Currently {{ user }} is logining {{ server_name }}' > {{ output }}"

deploy.yml

- hosts: "testservers"
  gather_facts: true
  remote_user: root
  roles:
    - testbox
打印当前目录结构
tree .
(返回root)su - root
#vi /etc/hosts
10.4.11.111 xxxx 增加目标主机ip与机器名
(exit返回delop用户)创建ssh
ssh免密码秘钥认证
创建本地秘钥
#ssh-keygen -t rsa
#ssh-copy-id -i /home/deploy/.ssh/id_rsa.pub root@x.x.x.x
!!注意:这里的x.x.x.x最好与[testservers]里写的一致,用ip就用ip,用主机名就用主机名
执行ansible playbook脚本
(.py3-a2.5-env) [deploy@xxxx test_playbooks]$ ansible-playbook -i inventory/testenv ./deploy.yml
playbook常用模块

File模块
创建文件或目录,并赋予系统权限

- name: create a file
  file: 'path=/root/foo.txt state=touch mode=0775 owner=foo group=foo'

Copy模块
实现Ansible服务端到目标主机的文件传送
force=yes 强制执行

- name: copy a file
  copy: 'remote_src=no src=roles/testbox/files/foo.sh dest=/root/foo.sh mode=0644 force=yes'

Stat模块
获取远程文件状态信息
register: script_stat 把状态信息赋值给script_stat 变量

- name: check if foo.sh exists
  stat: 'path=/root/foo.sh'
  register: script_stat

Debug模块
打印语句到Ansible执行输出
debug: msg=foo.sh exists 表示输出信息为foo.sh exists

- debug: msg="foo.sh exists"
  when: script_stat.stat.exists

Command/Shell模块
用来执行Linux目标主机命令行
shell会调用linux下的bin/bash,就可以使用系统环境变量、重定向符、管道符等

- name: run the script
  command: "sh /root/foo.sh"
- name: run the script
  shell: "echo 'test' > /root/test.txt"

Template模块
实现Ansible服务端到目标主机的jinja2模板传送
nginx.conf.j2中的变量参数会调用server清单里的var变量参数值

- name: write the nginx config file
  template: src=roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf

Packaging模块
调用目标主机系统包管理工具(yum,apt)进行安装
yum包装目标系统为CentOS/Redhat,apt则为Debian/Ubuntu

- name: ensure nginx is at the latest version
  yum: pkg=nginx state=latest
- name: ensure nginx is at the latest version
  apt: pkg=nginx state=latest

Service模块
管理目标主机系统服务

- name: start nginx service
  service: name=nginx state=started

示例

cd test_playbooks
mkdir roles/testbox/files
vi roles/testbox/files/foo.sh
         echo "this is a test"
vi roles/testbox/tasks/main.yml
----------------------------------------
useradd foo
useradd deploy
mkdir /etc/nginx
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
.
├── deploy.retry
├── deploy.yml
├── inventory
│   └── testenv
└── roles
    └── testbox
        ├── files
        │   └── foo.sh
        ├── tasks
        │   └── main.yml
        └── templates
            └── nginx.conf.j2

linux任务自启动配置

#!/bin/sh
#chkconfig: 2345 96 05
#description: esuser

case "$1" in
start)
    su esuser<<!
    cd /usr/local/es7-1/elasticsearch-7.0.0 
    ./bin/elasticsearch -d
!
    echo "elasticsearch startup"
    ;;  
stop)
    es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
    kill -9 $es_pid
    echo "elasticsearch stopped"
    ;;  
restart)
    es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
    kill -9 $es_pid
    echo "elasticsearch stopped"
    su esuser<<!
    cd /usr/local/es7-1/elasticsearch-7.0.0/
    ./bin/elasticsearch -d
!
    echo "elasticsearch startup"
    ;;  
*)
    echo "start|stop|restart"
    ;;  
esac

exit $? 

systemctl enable xxx
systemctl daemon-reload
systemctl status xxx
可以先确定命令内容,在终端可执行后不报错后,再编辑到文件里。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值