Joyber 发布的文章

# hostnamectl -h

  -h --help              显示帮助
     --version           显示安装包的版本
     --transient         修改临时主机名
     --static            修改瞬态主机名
     --pretty            修改灵活主机名
  -P --privileged        在执行之前获得的特权
     --no-ask-password   输入密码不提示
  -H --host=[USER@]HOST  操作远程主机

Commands:
  status                 显示当前主机名设置
  set-hostname NAME      设置系统主机名
  set-icon-name NAME     为主机设置icon名
  set-chassis NAME       设置主机平台类型名

CentOS7中有三种定义的主机名:
静态的(static)、瞬态的(transient)、和灵活的(pretty)。
静态主机名也称为内核主机名,是系统在启动时从/etc/hostname内自动初始化的主机名。
瞬态主机名是在系统运行时临时分配的主机名。
灵活主机名则允许使用特殊字符的主机名。

1.查看状态

# hostnamectl 或者 # hostnamectl status   (显示的结果都一样)

2.修改主机名称

# hostnamectl set-hostname Linuxprobe
# hostnamectl status

有时候,我们害怕别人修改我们创建的文件,或者是误删我们创建的文件,那么我们可以使用下面的方法进行控制即可

1.创建不可删除文件

Linux:/qinys/oliver # touch test.sh
Linux:/qinys/oliver # chattr +i test.sh 
Linux:/qinys/oliver # rm -rf test.sh 
rm: cannot remove `test.sh': Operation not permitted

2.将文件设置可以删除

Linux:/qinys/oliver # chattr -i test.sh 
Linux:/qinys/oliver # rm -rf test.sh

此时就可以删除了!

详解:

一 chattr命令格式
chattr [+-=] [选项] 文件或目录名
+:增加权限
-:删除权限
=:等于某权限
chattr选项包括:i和a
a等价于append
i等价于insert
i:如果对文件设置了i属性,那么不允许对文件进行删除,改名,也不能添加和修改数据;如果对目录设置了i属性,那么只能修改目录下文件的数据,但不允许建立和删除文件。
a:如果对文件设置了a属性,那么只能在文件中增加数据,但是不能删除也不能修改数据;如果对目录设置了a属性,那么只允许在目录中建立和修改文件,但是不允许删除。
注意:对root用户生效
 
二 lsattr命令格式
lsattr 选项 文件名
选项:
-a 显示所有文件和目录
-d 若目标是目录,仅列出目录本身的属性,而不是子文件的属性
————————————————
版权声明:本文为CSDN博主「cakincqm」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/chengqiuming/article/details/78602006

新手学习教程:
https://www.w3cschool.cn/automate_with_ansible

官方文档:
https://docs.ansible.com/

安装:

yum install -y ansible

配置文件:/etc/ansible/hosts (例子:https://www.w3cschool.cn/automate_with_ansible/automate_with_ansible-1khc27p1.html)

#ansible_ssh_host
#ansible_ssh_port
#ansible_ssh_user
#ansible_ssh_pass
#ansible_ssh_private_key_file

[dev]
192.168.0.200 ansible_ssh_port=123456

[online]
pc1 ansible_ssh_host=192.168.0.200 ansible_ssh_user=root ansible_ssh_port=123456

若有对 Control Machine 本机操作的需求,建议于 /etc/ansible/hosts 补上 local 的设定。

# For root user.
$ /bin/echo -e "[local]\nlocalhost ansible_connection=local" >> /etc/ansible/hosts

# For sudo user.
$ sudo su -c '/bin/echo -e "[local]\nlocalhost ansible_connection=local" >> /etc/ansible/hosts'

Hello World
当已上的设置都完成了,您可以试著在终端机里用 Ansible 呼叫本机印出 Hello World。

$ ansible dev -m command -a 'echo Hello World.'
localhost | SUCCESS | rc=0 >>
Hello World.

欢迎来到 Ansible 的世界!:D

如果遇到第一次连接会检查是否有链接过的记录时,可能需要把下面这个设置启用(不检查):
Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host.

配置:/etc/ansible/ansible.conf

host_key_checking = False 

实操技巧:
使用剧本的时候 -e :

# 多个变量间用空格隔开,遇到值有空格时用引号
ansible-playbook xxx.yml -e "var1=val1 var2=val2 var3='val val'"

# 将参数写到文件中,再引用,文件内容格式为json: {"a":"123", "b": "321"}
ansible-playbook xxx.yml -e "@path/to/file"

# 刷本中写变量,-e 传入的变量会覆盖的, xxx.yml:
- name: xxx
  hosts: xxx
  vars_files:
    - xxx.json
  vars:
    a: 123
    b: 321

上传文件的时候,可能需要上传多个文件,怎么写:

tasks:
    - name: upload cert files
      copy: src={{ item.src }} dest={{ item.dest }}
      with_items:
        - { src: '/root/site/{{ mark }}.key', dest: '/data/cert/{{ mark }}.key' }
        - { src: '/root/site/{{ mark }}.pem', dest: '/data/cert/{{ mark }}.pem' }

b=${a/123/321};将${a}里的第一个123替换为321

b=${a//123/321};将${a}里的所有123替换为321

b=${a//\//\\\/};将${a}里的所有/替换为\/

#shell调试:

sh -x aaa.sh

  平常在写shell脚本都是用$1,$2....这种方式来接收参数,然而这种接收参数的方式不但容易忘记且不易于理解和维护。Linux常用的命令都可指定参数名和参数值,然而我们怎样才能给自己的shell脚本也采用参数名和参数值这样的方式来获取参数值呢?而不是通过$1,$2这种方式进行获取。下面的例子定义了短参数名和长参数名两种获取参数值的方式。其实是根据getopt提供的特性进行整理而来。

#!/bin/sh
#说明
show_usage="args: [-l , -r , -b , -w]\
                                  [--local-repository=, --repository-url=, --backup-dir=, --webdir=]"
#参数
# 本地仓库目录
opt_localrepo=""

# git仓库url
opt_url=""

# 备份目录
opt_backupdir=""

# web目录
opt_webdir=""

GETOPT_ARGS=`getopt -o l:r:b:w: -al local-repository:,repository-url:,backup-dir:,webdir: -- "$@"`
eval set -- "$GETOPT_ARGS"
#获取参数
while [ -n "$1" ]
do
        case "$1" in
                -l|--local-repository) opt_localrepo=$2; shift 2;;
                -r|--repository-url) opt_url=$2; shift 2;;
                -b|--backup-dir) opt_backupdir=$2; shift 2;;
                -w|--webdir) opt_webdir=$2; shift 2;;
                --) break ;;
                *) echo $1,$2,$show_usage; break ;;
        esac
done

if [[ -z $opt_localrepo || -z $opt_url || -z $opt_backupdir || -z $opt_webdir ]]; then
        echo $show_usage
        echo "opt_localrepo: $opt_localrepo , opt_url: $opt_url , opt_backupdir: $opt_backupdir , opt_webdir: $opt_webdir"
        exit 0
fi