ansible学习
一、ansible安装 #只需要管理端安装 yum install epel-release yum install ansible 二、配置 vim /etc//ansible/hosts#添加远程被管理端主机 192.168.10.148 k8s-master k8s-node-2 三、 生成公钥放到被管理端 ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.10.148 ssh-copy-id -i ~/.ssh/id_rsa.pub root@k8s-master ssh-copy-id -i ~/.ssh/id_rsa.pub root@k8s-node-2 管理端测试:ansible all -m ping ansible all -m ping -u alex#以alex用户执行 ansible all -m ping -u alex --sudo --sudo-user batman#sudo方式运行 四、命令 #开启两个进程并行执行,关闭atlanta组的所有主机 ansible atlanta -a "/sbin/reboot" -f 2 #拷贝文件到atlanta组的主机 ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts" #修改为文件权限 ansible atlanta -m file -a "/tmp/hosts mode=777" #创建目录 ansible atlanta -m file -a "dest=/tmp/c mode=755 owner=nginx group=nginx state=directory" #删除目录 ansible atlanta -m file -a "dest=/tmp/c state=absent" #确认软件包是否安装,但不去升级 ansible atlanta -m yum -a "name=nginx state=present" #确认一个软件包没有安装 ansible atlanta -m yum -a "name=nginx state=absent" #使用 ‘user’ 模块可以方便的创建账户,删除账户,或是管理现有的账户: ansible all -m user -a "name=foo password=<crypted password here>" ansible all -m user -a "name=foo state=absent" #启动服务 ansible atlanta -m service -a "name=docker state=started" #重启服务 ansible atlanta -m service -a "name=docker state=restarted" #停止服务 ansible atlanta -m service -a "name=docker state=stopped" #需要长时间运行的命令可以放到后台去,在命令开始运行后我们也可以检查运行的状态.如果运行命令后,不想获取返回的信息, 可执行如下命令: ansible all -B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff" #如果你确定要在命令运行后检查运行的状态,可以使用 async_status 模块.前面执行后台命令后会返回一个 job id, 将这个 id 传给 async_status 模块: ansible web1.example.com -m async_status -a "jid=488359678239.2844" #获取状态的命令如下: ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff" 其中-B1800表示最多运行30分钟,-P60表示每隔60秒获取一次状态信息. 五、http安装配置文件修改例子 vim /etc/ansible/hosts #添加主机组 [testhost] 192.168.10.148 192.168.10.224 编写yaml文件 vim http.yml#修改httpd.conf文件时,notify会通知客户端重启httpd - hosts: testhost vars: src_http_dir: "/etc/httpd" dest_http_dir: "/tmp" remote_user: root tasks: - name: instal httpd service yum: name=httpd state=present - name: copy httpd conf copy: src="`src_http_dir`/conf/httpd.conf" dest=/etc/httpd/conf/httpd.conf notify: - restart httpd service - name: start httpd service service: name=httpd state=started enabled=true handlers: - name: restart httpd service service: name=httpd state=restarted 变量使用 vim test1.yml - hosts: testhost vars: src_http_dir: "/etc/httpd" dest_http_dir: "/tmp" remote_user: root tasks: - name: copy httpd conf copy: src="`src_http_dir`/conf/httpd.conf" dest="`dest_http_dir`/http.conf.ansible" 模板使用 vim httpd.conf #修改以下内容 Listen `ansible_all_ipv4_addresses`.`0`:`http_port` ServerName `ansible_nodename` vim test2.yml - hosts: testhost vars: http_port: 8010 http_dir: /etc/httpd/conf remote_user: root tasks: - name: copy httpd conf template: src=/etc/ansible/httpd.conf dest="`http_dir`/httpd.conf" notify: - restart httpd service handlers: - name: restart httpd service service: name=httpd state=restarted 本文转自小白的希望 51CTO博客,原文链接:,http://blog.51cto.com/haoyonghui/1970029如需转载请自行联系原作者