-
机器配置yum源
-
升级ssh
yum update openssh -y
-
安装ansible
yum install -y ansible
-
查看ansible需要用到的配置文件
rpm -qc ansible
-
配置master节点与node节点的免密登录
ssh-keygen cd ~/.ssh/ cat id_rsa.pub >> authorized_keys chmod 600 ~/.ssh chmod 600 ~/.ssh/authorized_keys scp /root/.ssh/authorized_keys root@master:/root/.ssh/authorized_keys scp /root/.ssh/authorized_keys root@node01:/root/.ssh/authorized_keys scp /root/.ssh/authorized_keys root@node02:/root/.ssh/authorized_keys
-
解决连接新主机时,进行公钥确认问题
vim /root/.ssh/config StrictHostKeyChecking=no
-
配置ansible hosts文件
[all] 172.28.128.23 172.28.128.24 172.28.128.25 [web] 172.28.128.23 [postprocess] 172.28.128.24 172.28.128.25
-
执行如下命令测试是否配置成功,网络是否通畅
ansible all -m ping ansible all -m shell -a "echo \"this is an ansible test\" > /root/ansible_test"
-
测试成功
-
查看ansible支持的模块
ansible-doc -l
-
查看某个模块使用说明
ansible-doc shell
-
确认都能ping通
ansible -i environment/common-deploy/group_vars/hosts 'all' -m ping
-
如何将master机器上的文件copy到node机器上
ansible postprocess -m copy -a 'src=/root/test.tar.gz dest=/root/ mode=755 owner=root'
-
如何在node节点上执行shell命令
ansible all -m shell -a "echo \"this is an ansible test\" > /root/ansible_test"
-
如何在node节点上执行yum命令安装rpm
ansible web -m yum -a 'name=nginx state=present '
-
如何启动node节点上的服务
ansible web -m service -a 'name=nginx state=started enabled=yes runlevel=35' name :服务名 state:started|stopped|restarted enabled:yes|no 是否开机启动 runlevel : 开机启动运行在哪些级别下
-
playbook如何使用
-
playbook基础组件
hosts: 运行指定任务的目标主机,多个主机用“:”分隔。 remote_user:在远程主机上执行任务的用户,可以全局指定,也可以单个任务指定 sudo_user: 表示sudo方式运行任务时,切换为哪个用户身份运行 tasks: 任务列表 handlers: 在发生改变时执行的操作
-
playbook 语法
ansible-playbook –syntax-check test.yml 测试文件语法 ansible-playbook –check test.yml 测试执行(不是正式执行)
-
如何配置测试yml,参考下面链接
https://ansible-tran.readthedocs.io/en/latest/docs/playbooks_best_practices.html
-
目录结构参考如下
production # inventory file for production servers 关于生产环境服务器的清单文件 stage # inventory file for stage environment 关于 stage 环境的清单文件 group_vars/ group1 # here we assign variables to particular groups 这里我们给特定的组赋值 group2 # "" host_vars/ hostname1 # if systems need specific variables, put them here 如果系统需要特定的变量,把它们放置在这里. hostname2 # "" library/ # if any custom modules, put them here (optional) 如果有自定义的模块,放在这里(可选) filter_plugins/ # if any custom filter plugins, put them here (optional) 如果有自定义的过滤插件,放在这里(可选) site.yml # master playbook 主 playbook webservers.yml # playbook for webserver tier Web 服务器的 playbook dbservers.yml # playbook for dbserver tier 数据库服务器的 playbook roles/ common/ # this hierarchy represents a "role" 这里的结构代表了一个 "role" tasks/ # main.yml # <-- tasks file can include smaller files if warranted handlers/ # main.yml # <-- handlers file templates/ # <-- files for use with the template resource ntp.conf.j2 # <------- templates end in .j2 files/ # bar.txt # <-- files for use with the copy resource foo.sh # <-- script files for use with the script resource vars/ # main.yml # <-- variables associated with this role defaults/ # main.yml # <-- default lower priority variables for this role meta/ # main.yml # <-- role dependencies webtier/ # same kind of structure as "common" was above, done for the webtier role monitoring/ # "" fooapp/ # ""
-
部署测试
cd /opt/auto-deploy ansible-playbook -i ./hosts site.yml
-
centos7安装ansible
最新推荐文章于 2024-09-07 17:08:10 发布