1、python3中ansible安装模块
在Python3中安装模块需要带上python版本、需要安装模块的版本,默认是安装最新的版本
python3 -m pip install Django==1.10.7
python3 -m pip install ansible
2、ansible切换目录并且创建文件
1.1、使用shell模块操作
[root@master ~]# ansible k8s -m shell -a "chdir=/rubbish touch test.conf"
1.2、使用command操作
[root@master ~]# ansible k8s -m command -a "chdir=/rubbish touch test.conf"
3、replace模块(查找替换)
ansible master -m replace -a "path=/etc/systemd/system/etcd.service regexp='master=' replace='$ECTD_CLUSTER' backup=yes"
path参数 :必须参数,指定要操作的文件
regexp参数 : 必须参数,指定一个 python 正则表达式,文件中与正则匹配的字符串将会被替换
replace参数 : 指定最终要替换成的字符串
backup参数 :是否在修改文件之前对文件进行备份,最好设置为yes
4、追加一行:
ansible master -m lineinfile -a "dest=/roota.sh line='--listen'"
5、带有反斜杠无法追加
下面报错
ansible master -m lineinfile -a "dest=/roota.sh line='--listen-admin \'"
6、copy模块使用
拷贝文件到所有节点并且备份
time ansible test2 -m copy -a "src=/script/test2_hostname.py dest=/root/ backup=yes"
拷贝文件到所有节点,强制覆盖
ansible k8s -m copy -a 'src=/k8s/profile/kube-controller-manager.service.template.py dest=/k8s/profile/ force=yes'
src 源目录以/结尾,拷贝了目录下的所有内容
time ansible k8s -m copy -a 'src=/root/ssl/ dest=/etc/kubernetes/cert/
源目录未以/结尾,直接将src目录本身拷贝到目的地
time ansible k8s -m copy -a 'src=/root/ssl dest=/etc/kubernetes/cert/
7、script模块使用
直接执行脚本
time ansible tes2 -m script -a "python test2_hostname.py"
8、shell模块使用
直接执行linux命令
time ansible tes2 -m shell -a "ls /home"
9、file模块使用
注意:使用file模块创建文件夹,父目录和子目录都会创建出来
创建目录并授权方式一
time ansible test2 -m file -a "path=/rubbish/test/ state=directory mode=0755"
创建目录并授权方式二
time ansible test2 -m file -a "dest=/rubbish/test/ state=directory mode=0755"
删除文件
time ansible k8s -m file -a 'path=/root/.kube/config state=absent'
修改文件的所有者
说明:不是修改目录下所有的文件,不能用/var/log/kubernetes/* 不管用,只能用shown
ansible test4 -m file -a 'path=/var/log/kubernetes/log.file owner=k8s group=k8s mode=0777'
修改文件的权限方式一
ansible test4 -m file -a 'path=/var/log/kubernetes/log.file state=touch mode="u=rw,g=r.o=r"'
说明:state=touch,如果文件存在不会再次创建
改变文件权限方式二
如果目录下有文件可以用下面来给目录下所有文件加权限,如果目录下面没有文件,执行下面会报错
ansible k8s -m shell -a 'chmod +x /opt/k8s/bin/*'
修改文件夹的权限
ansible test4 -m file -a 'path=/var/log/kubernetes/ state=directory mode="u=rw,g=r.o=r"'
说明:state取值可以是directory、file、touch、link、hard、absent
10、service模块
重启服务
time ansible k8s -m service -a 'name=nginx state=restarted'
停止服务
time ansible k8s -m service -a 'name=nginx state=stopped'
加载服务
time ansible k8s -m service -a 'name=nginx state=reloaded'
查看服务状态
time ansible k8s -m service -a 'name=nginx state=status'
设置开机自启
time ansible k8s -m service -a 'name=nginx enabled=yes'
11、unarchive使用
解压安装包到指点目录下
time ansible test1 -m unarchive -a 'src=/server/software/k8s/flannel-v0.10.0-linux-amd64.tar.gz dest=/rubbish/'
解压安装包到所有主机上
注意:不需要把安装包提前拷贝到其他节点,
time ansible test1 -m unarchive -a 'src=/server/software/k8s/flannel-v0.10.0-linux-amd64.tar.gz dest=/rubbish/'
13、ansible使用sudo
ansible k8s -m shell -a 'chmod +x /opt/k8s/bin/*' -become
参考:
https://www.jianshu.com/p/3a67ba27ba20
https://www.cnblogs.com/xiexiaoxiao/p/7147920.html
https://blog.csdn.net/bruce_6/article/details/79043529