介绍一款主流的配置管理工具ansible

发表于:2024-4-07 09:46

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:佚名    来源:51CTO博客

  Ansible自动化配置管理
  1.什么是Ansible
  Ansible是一个IT自动化的配置管理工具,自动化主要体现在Ansible集成了丰富的模块,丰富的功能的组件,可以通过一个命令完成一系列的操作。进而能减少我们的重复性工作和维护成本,以提高工作效率
  2.Ansible可以完成哪些功能
  1)批量执行远程命令,可以对N多台主机同时进行命令的执行。
  2)批量配置软件服务,可以用进行自动化的方式配置和管理服务。
  3)实现软件开发功能,jumpserver底层使用ansible来实现的自动化管理。
  4)编排高级的IT任务,ansible的Playbook是一门编程语言,可以用来描绘一套IT架构。
  3.Ansible的特点
  1)容易学习,无代理模式,不像salt stack既要学习客户端又要学习服务端,还要学习客户端与服务端之间通讯协议。
  2)操作灵活,体现在Ansible有较多的模块,提供了丰富的功能、playbook则提供类似于编程语言的复杂功能。
  3)简单易用,体现在Ansible一个命令可以完成很多事情。
  4)安全性高,因为Ansible使用了SSH协议进行通讯,即安全也稳定。
  5)移植性高,可以将写好的playbook拷贝至任意机器进行执行。
  4.Ansible基础架构
  ]
  5.Ansible配置
  1)安装Ansible
  [root@manager ~]# yum install ansible -y
  2)查看Ansible版本
  [root@manager ~]# ansible --version
  ansible 2.8.5#版本
    config file = /etc/ansible/ansible.cfg#配置文件所在路径
    configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']#模块的搜索路径
    ansible python module location = /usr/lib/python2.7/site-packages/ansible#python模块所在的路径
    executable location = /usr/bin/ansible#执行命令的位置
    python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]#python版本
  测试ansible配置文件的优先级
  ANSIBLE_CONFIG#一个配置文件(最高优先级1)
  ansible.cfg#当前项目目录中(2)
  .ansible.cfg#当前执行用户的家目录(3)
  /etc/ansible/ansible.cfg#配置文件中(4)
  1.定义配置文件
  [root@manager ~]# export ANSIBLE_CONFIG="/tmp/ansible.cfg"#定义一个ansible
  [root@manager ~]# touch /tmp/ansible.cfg
  [root@manager ~]# ansible --version
  ansible 2.8.5
    config file = /tmp/ansible.cfg
    configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
    ansible python module location = /usr/lib/python2.7/site-packages/ansible
    executable location = /usr/bin/ansible
    python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
  2。在projcet1上查看优先级
  [root@manager ~]# mkdir /project1
  [root@manager ~]# touch /project1/ansible.cfg
  [root@manager project1]# ansible --version
  ansible 2.8.5
    config file = /root/project1/ansible.cfg
    configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
    ansible python module location = /usr/lib/python2.7/site-packages/ansible
    executable location = /usr/bin/ansible
    python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
  3。在projcet2上查看优先级
  [root@manager ~]# mkdir /project2
  [root@manager ~]# touch /project2/ansible.cfg
  [root@manager project1]# ansible --version
  ansible 2.8.5
    config file = /root/project2/ansible.cfg
    configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
    ansible python module location = /usr/lib/python2.7/site-packages/ansible
    executable location = /usr/bin/ansible
    python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
  配置选项详解
  6.ansibleinventory主机清单
  1)基于ip地址+密码的方式
  [root@manager project1]# vim hosts
  [webservers]#定义组名
  172.16.1.7 ansible_ssh_user='root' ansible_ssh_pass='1'
  172.16.1.8 ansible_ssh_user='root' ansible_ssh_pass='1'
  2)基于密钥的方式
  1.创建密钥
  [root@manager ~]# mkdir .ssh     #创建密钥所需要存放的目录
  [root@manager ~]# ssh-keygen -t rsa -b 4096#生成密钥,一路回车
  2.将公钥推送至对端主机
  [root@manager ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
  [root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8
  3.配置组
  [root@manager project1]# vim hosts
  [webservers]
  172.16.1.7
  172.16.1.8
  3)场景三,主机组使用方式
  3.场景三、主机组使用方式 
  [lbservers] #定义lbservers组 
  172.16.1.5 172.16.1.6
  [webservers] #定义webserver组 
  172.16.1.7 172.16.1.8
  [servers:children]  #定义servers组包括两个子组[lbservers,webserver] 
  lbservers webserver
  4)查看组所有信息
  [root@manager project1]# ansible all --list-host -i hosts
    hosts (2):
      172.16.1.7
      172.16.1.8
  Ansible常用模块
  1.什么是ad-hoc
  ad-hoc简而言之就是临时命令,执行完就结束,不会保存。
  2.ad-hoc模式的使用场景
  多台主机查看某个进程是否启动,或拷贝指定文件到本地。
  3.ad-hoc模式命令的使用
  ansible oldboy -m command -a 'df -h'
  4.使用ad-hoc执行一次远程命令,注意观察返回结果的颜色
  绿色:代表被管理端主机没有被修改。
  黄色:代表被管理端主机修改成功。
  橘红色:代表出现了故障。
  5.ansible模块
  command#执行命令默认不支持管道
  shell#执行命令支持管道
  yum_reposity#yum仓库配置
  yum#yum安装软件
  get_url#和liunx的 wget 一致
  copy#拷贝配置文件
  server|systemd#启动服务
  user#创建用户
  group#创建组
  file#创建目录、创建文件、递归授权
  mount#挂载
  cron#定时任务
  firewalld#防火墙
  selinux#selinux
  command
  ansible webservers -a "ps axu|grep nginx" -i hosts  #不支持管道(简单命令、单条命令)
  shell
  ansible webservers -m shell -a "ps axu|grep nginx" i hosts  #支持管道
  yum
  state:
  present安装
  absent卸载
  latest最新
  enablerepo#指定使用某个仓库
  disablerepo#指定排除某个仓库
  #1.安装最新的httpd服务,排除webtatic-php该仓库
  [root@manager project1]# ansible webserver -m yum -a "name=httpd state=latest disablerepo=webtatic-php" -i hosts
  #2.移除httpd服务
  [root@manager project1]# ansible webserver -m yum -a "name=httpd state=absent" -i hosts
  #3.安装httpd指定从某个仓库安装
  [root@manager project1]# ansible webserver -m yum -a "name=httpd state=latest enablerepo=testing" -i hosts
  #4.通过url方式进行安装
  [root@manager project1]# ansible webservers -m yum -a "name=https://mirrors.aliyun.com/zabbix/zabbix/3.0/ rhel/7/x86_64/zabbix-agent-3.0.0-1.el7.x86_64.rpm state=present disablerepo=webtatic-php" -i hosts
  #5.通过软件包安装
  - name: install nginx rpm from a local file  (软件包 必须在被控端主机) [root@manager project1]# ansible webservers -m yum -a "name=/root/zabbix-agent-4.0.0-2.el7.x86_64.rpm  state=present disablerepo=webtatic-php" -i hosts
  copy
  src#本地路径,可以是相对,可以是绝对
  dest#目标位置
  owner#属主
  group#属组
  mode#权限
  backup#备份
  #拷贝本地文档到对端主机
  [root@manager project1]# ansible webservers -m copy -a "src=./file/ansible.oldxu.com.conf dest=/etc/nginx/conf.d/ansible.oldxu.com.conf owner=root group=root mode=644" -i hosts
  #拷贝本地文档到对端主机,并备份源文件
  [root@manager project1]# ansible webservers -m copy -a "src=./file/ansible.oldxu.com.conf dest=/etc/nginx/conf.d/ansible.oldxu.com.conf owner=root group=root mode=644 backup=yes" -i hosts
  service/systemd
  state
  started#启动
  stopped#停止
  restarted#重启
  reloaded#重载
  
  enabled#是否开机自启
  yes#是
  no#否
  
  [root@manager project1]# ansible webservers -m systemd -a "name=nginx state=restarted enabled=yes" -i hosts
  file
  #创建 /code/ansible
  path#路径
  state
  touch#创建文件
  directory#创建目录
  owner#属主
  group#属组
  mode#权限
  #准备站点
  [root@manager project1]# ansible webservers -m file -a "path=/code/ansible state=directory mode=755 owner=www group=www" -i hosts
  #准备站点代码
  [root@manager project1]# ansible webservers -m copy -a "src=./file/index.html dest=/code/ansible/index.html owner=www group=www mode=644" -i hosts
  user group
  [root@manager project1]# ansible webservers -m group -a  "name=www gid=666 state=present" -i hosts
  #user
  name#名称
  uid#uid
  group#组名或gid
  create_home#是否创建家目录
  system#是否作为系统组
  shell#登录shell
  state#状态
  present创建默认
  absent删除
  remove#加上remove清除家目录
  groups#附加组
  append#
  password#创建密码
  # 程序使用    www    666 666 /sbin/nologin    /home  -->无 
  [root@manager project1]# ansible webservers -m user -a "name=www uid=666 group=666 create_home=no shell=/sbin/nologin state=present" -i hosts
  # 正常用户    oldxu  1000 1000 /bin/bash   /home/oldxu 
  [root@manager project1]# ansible webservers -m user -a "name=oldxu" -i hosts
  # 移除oldxu用户,并删除家目录所有内容. 
  [root@manager project1]# ansible webservers -m user -a "name=oldxu state=absent remove=yes" -i hosts
  # 创建 other用户.有两个附加组root bin,创建家目录,指定登录 shell,设定密码123
  #生成一个密码 
  ansible all -i localhost, -m debug -a "msg={{ '123' | password_hash('sha512', 'mysecretsalt') }}"
  [root@manager project1]# ansible webservers -m user -a 'name=other groups='root,bin' create_home=yes shell=/bin/bash password="$6$mysecretsalt$gIIYs0Xgc7sSQkH.zKaz8/Afa MomYzR1QZYtccwmJcUt8VpLq4D055UCCX4MlwgePOP80ZRwhppv BF72RIAVi/"' -i hosts
  mount
  #提前准备好nfs服务端 
  [root@web01 ~]# showmount -e 172.16.1.31 
  Export list for 172.16.1.31: 
  /data/zrlog 172.16.1.0/24 
  /data/zh    172.16.1.0/24 
  /data/edu   172.16.1.0/24
  /data/blog  172.16.1.0/24
  #用管理端操作被控端,让被控端挂载nfs存储数据 
  present     #写入/etc/fstab 
  absent      #卸载/etc/fstab
  mounted     #临时挂载 
  unmounted   #卸载当前挂载
  #挂载过程中,如果目录不存在,则会创建该目录 
  [root@manager project1]# ansible webservers -m mount -a "src=172.16.1.31:/data/zrlog path=/test_zrlog fstype=nfs opts=defaults state=mounted" -i hosts
  #卸载过程中,会把该目录删除
  [root@manager project1]# ansible webservers -m mount -a "src=172.16.1.31:/data/zrlog path=/test_zrlog fstype=nfs opts=defaults state=unmounted" -i hosts
  错误实例:
  [root@manager project1]# ansible webservers -m mount -a "src=172.16.1.31:/data/blog fstype=nfs opts=defaults state=mounted" -i hosts
  172.16.1.7 | FAILED! => {
      "ansible_facts": {
          "discovered_interpreter_python": "/usr/bin/python"
      }, 
      "changed": false, 
      "msg": "missing required arguments: path"
  }
  172.16.1.8 | FAILED! => {
      "ansible_facts": {
          "discovered_interpreter_python": "/usr/bin/python"
      }, 
      "changed": false, 
      "msg": "missing required arguments: path"
  }
  cron
  minute      #分 
  hour        #时 
  day         #日 
  month       #月 
  week        #周 
  job         #执行任务的命令
  #增加定时任务
  [root@manager project1]# ansible webservers -m cron -a 'name=test_job minute=00 hour=02 job="/bin/bash /server/scripts/client_to_data_server.sh &>/dev/null"' -i hosts
  #移除定时任务
  [root@manager project1]#  ansible webservers -m cron -a 'name=test  job="/bin/bash /server/scripts/test.sh &>/dev/null" state=absent' -i hosts
  firewalld
  1.开启防火墙
  [root@manager project1]# ansible webservers -m systemd -a "name=firewalld state=started" -i hosts
  2.针对服务
  [root@manager project1]# ansible webservers -m firewalld -a "service=http state=enabled" -i hosts
  3.针对端口
  [root@manager project1]# ansible webservers -m firewalld -a "port=9999/tcp state=enabled" -i hosts
  4.针对source来源
  [root@manager project1]# ansible webservers -m firewalld -a "source=172.16.1.0/24 state=enabled permanent=yes" -i hosts
  172.16.1.8 | CHANGED => {
      "ansible_facts": {
          "discovered_interpreter_python": "/usr/bin/python"
      }, 
      "changed": true, 
      "msg": "Permanent operation, Added 172.16.1.0/24 to zone public"
  }
  172.16.1.7 | CHANGED => {
      "ansible_facts": {
          "discovered_interpreter_python": "/usr/bin/python"
      }, 
      "changed": true, 
      "msg": "Permanent operation, Added 172.16.1.0/24 to zone public"
  }
  5.针对rule
  [root@manager project1]# ansible webservers -m firewalld -a 'rich_rule="rule family=ipv4 source address=10.0.0.1/32 service name=http accept" state=enabled' -i hosts
  selinux
  [root@manager project1]# ansible webservers -m selinux -a "state=disabled" -i hosts
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号