ansible安装ansible :yum install -y ansible查看版本:ansible --version配置ssh免密登录:ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ""配置主机组:如果没有ansible目录创建即可,touch /etc/ansible/hosts配置ansible配置文件:touch /etc/ansible/ansible.cfg测试: ansible -m ping allAnsible常用模块使用shell模块:ansible k8s -m shell -a "chdir=/rubbish touch test.conf"command模块:ansible k8s -m command -a "chdir=/rubbish touch test.conf"replace模块(查找替换)ansible master -m replace -a "path=/etc/systemd/system/etcd.service regexp='master=' replace='$ECTD_CLUSTER' backup=yes"lineinfile模块:ansible master -m lineinfile -a "dest=/roota.sh line='listen'"copy模块:ansible test2 -m copy -a "src=/script/test2_hostname.py dest=/root/ backup=yes"script模块使用:ansible tes2 -m script -a "python test2_hostname.py"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'修改文件的所有者ansible test4 -m file -a 'path=/var/log/kubernetes/log.file state=touch mode="u=rw,g=r.o=r"'说明:state取值可以是directory、file、touch、link、hard、absentservice模块:ansible k8s -m service -a 'name=nginx state=restarted'unarchive模块:time ansible test1 -m unarchive -a 'src=/server/software/k8s/flannel-v0.10.0-linux-amd64.tar.gz dest=/rubbish/'Ansible最佳实战(初始化roles进行部署nginx)初始化一个role ansible-galaxy init /etc/ansible/roles/websrvs配置变量all文件,注意:名字只能写成all,写其他的就报错配置site.yaml作为执行入口文件,里面定义都对哪些roles操作配置handlers文件,就是触发器,比如满足条件后启动nginx配置tasks, 这是具体执行操作的yaml文件配置templates。 就是准备需要的模板文件,没有就不用准备执行部署 ansible-playbook site.yaml