设为首页 收藏本站
查看: 929|回复: 0

[经验分享] ansible-benben

[复制链接]

尚未签到

发表于 2018-7-29 06:22:18 | 显示全部楼层 |阅读模式
  一、ansible的安装
  1、yum源在线安装
  在网易镜像上下载.repo文件,添加到本地的yum源目录下
  http://mirrors.163.com/.help/centos.html
  2、检测是否安装正确:ansible --version
  3、配置文件的查看
  /etc/ansible/ansible.cfg  //ansible的主配置文件
  /etc/ansible/hosts   //这个配置文件就是默认主机清单配置文件
  4、除了以上两个重要的配置文件还有三个重要的可执行文件分别是:
  ansible 主执行程序,一般用于命令行下执行
  ansible-playbook 执行playbook中的任务
  ansible-doc 获取各模块的帮助信息
  二、基本使用
  1、定义主机组
  host_key_checking = False    //修改主配置文件,不需要检测key文件,直接登陆
  ansible web --list-hosts   //检测web包含哪些主机
  [web]  //定义组名
  192.168.4.85 ansible_ssh_user="root"
  ansible_ssh_pass="123456" ansible_ssh_port="22"
  192.168.4.86 ansible_ssh_user="root"
  ansible_ssh_pass="123456" ansible_ssh_port="22"
  //上面定义了IP地址,远程的用户,远程的密码以及端口
  2、基本的命令用法
  ansible web -m ping   测试是否通信
  ansible all -m command -a 'hostname' -k
  //远程执行命令,-m调用的模块,-a调用的命令,-k交互式输入密码
  3、批量部署密钥  ansible-doc -l | grep auth  //查看安全模块
  ansible all -m authorized_key -a &quot;user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'&quot; -k -v
  //把生成的公钥推送到所有的主机,  exclusive=true文件强制覆盖掉
  三、常用的模块:
  1、ping模块:测试主机是否是通的,用法很简单,不涉及参数:
ansible web -m ping  2、command 模块 远程执行命令  管道的命令不能执行
  ansible all -m command -a &quot;ifconfig&quot;
  3、shell模块:由于commnad只能执行裸命令(即系统环境中有支持的命令),至于管道之类的功能不支持,
  shell模块可以做到
  ansible all -m shell -a 'cat /etc/passwd | wc -l' -v
  4、script 模块:把本地的脚本传到远端执行;前提是到远端可以执行。
  ansible all -m script -a '/root/a.sh'
  5、 copy模块:不适合大的文件
  ansible web -m copy -a 'src=/etc/passwd dest=/root'
  6、 lineinfile   |   replace  模块:line配置整行,replace匹配选中的,直接修改文件
  ansible s87 -m lineinfile -a 'path=&quot;/etc/sysconfig/network-scripts/ifcfg-eth0&quot; regexp=&quot;^BOOTPROTO=&quot; line=&quot;BOOTPROTO=static&quot;'
  ansible s87 -m replace -a 'path=&quot;/etc/sysconfig/network-scripts/ifcfg-eth0&quot; regexp=&quot;^(BOOTPROTO=).*&quot; replace=&quot;\1none&quot;'
  7、 yum 模块:state(present/安装     absent/卸载   latest/更新)
  ansible web -m yum -a 'name=httpd state=present' -v
  8、service 模块:用于管理服务
  state( started,stopped,restarted,reloaded),name=服务名称,enabled=yes|no
  ansible web -m service -a 'name=&quot;httpd&quot; enabled=yes state=started'
  9、file模块:对远程文件管理的模块
  state:
  touch:创建一个新的空文件
  directory:创建一个新的目录,当目录存在时不会进行修改
  link:创建软连接,结和src一起使用此选项才生效
  hard:创建硬连接
  absent:删除文件,目录,软连接
  ansible web -m file -a 'path=/tmp/test.txt state=touch'
  四、playbook的基本使用
  playbook 是 ansible 用于配置,部署,和管理托管主机剧本。通过 playbook 的详细描述,执行其中的一系
  列 tasks,可以让进端主机达到预期的状态。执行一些简单的任务,使用ad-hoc命令可以方便的解决
  问题,但是有时一个设施过于复杂,需要大量的操作时候,执行的 ad-hoc 命令是不适合的,这时最好使用
  playbook,就像执行 shell 命令不写 shell 脚本一样,也可以理解为批处理任务
  play 中 hosts,variables,roles,tasks 等对象的表示方法都是键值中间以 &quot;: &quot; 分隔表示
  playbook 构成:
  Target: 定义将要执行 playbook 的进程主机组
  Variable: 定义 playbook 运行时需要使用的变量
  Tasks: 定义将要在进程主机上执行的任务列表
  Handler: 定义 task 执行完成以后需要调用的任务
  1、第一个playbook,vim myping.yml
  ---                                  # 第一行,表示开始
  - hosts: all
  remote_user: root
  tasks:
  - ping:
  ansible-playbook myping.yml -f 5
  -f 并发进程数量,默认是 5
  hosts 行的内容是一个或多个组或主机的 patterns,以逗号为分隔符
  remote_user 就是账户名
  2、playbook 执行命令给所有主机添加用户 plj,设置默认密码 123456
  – 要求第一次登录修改密码
  ---
  - hosts: all
  remote_user: root
  tasks:
  - name: create user plj
  user: group=wheel uid=1000 name=plj
  - shell: echo 123456 | passwd --stdin plj
  - shell: chage -d 0 plj
  3、编写 playbook 实现以下效果
  – 安装 apache      – 修改 apache 监听的端口为 8080
  – 为 apache 增加 NameServer 配置   – 设置默认主页 hello world
  – 启动服务         – 设置开机自启动
  - hosts: web
  remote_user: root
  tasks:
  - yum: name=httpd
  - name: config
  copy:
  src: /root/httpd.conf
  dest: /etc/httpd/conf/httpd.conf
  notify:       //notify模块:触发器
  - restart httpd
  handlers:     // task 执行完成以后需要调用的任务
  - name: restart httpd
  service: name=httpd state=restarted
  4、变量
  给所有主机添加用户 plj,设置默认密码 123456
  要求第一次登录修改密码(使用变量)
  ---
  - hosts: web
  remote_user: root
  vars:
  username: haha
  tasks:
  - user: name={{username}} group=users password={{'123456' | password_hash('sha512')}}
  - shell: chage -d 0 {{username}}
  //变量过滤器 password_hash
  5、ansible-playbook 对错误的处理
  默认情况判断 $?,如果 值 不为 0 就停止执行
  但某些情况我们需要忽略错误继续执行
  如:
  我们要关闭 selinux,如果 selinux 已经是关闭的,返
  回 1 ,但我们的目的就是关闭,已经关闭不算错误,
  这个情况我们就需要忽略错误继续运行,忽略错误有
  两种方法:
  shell: /usr/bin/somecommand || /bin/true
  - name: run some command
  shell: /usr/bin/somecommand
  ignore_errors: True
  完整的例子如下:
  ---
  - hosts: web
  remote_user: root
  vars:
  username: plj
  tasks:
  - name: create user &quot;{{username}}&quot;
  user: group=wheel uid=1010 name={{username}} password={{'123456'|password_hash('sha512')}}
  - shell: setenforce 0
  ignore_errors: true
  - shell: chage -d 0 {{username}}
  6、when:某些时候我们可能需要在满足特定的条件后在触发某
  一项操作,戒在特定的条件下织止某个行为,这个时候我们就需要迚行条件判断,
  when 正是解决这个问题的最佳选择,进程中的系统变量 facts 变量作为
  when 的条件,这些 facts 我们可以通过 setup 模块查看
  ---
  - name: Install VIM
  hosts: all
  tasks:
  - name: Install VIM via yum
  yum: name=vim-enhanced state=installed
  when: ansible_os_family == &quot;RedHat&quot;
  - name: Install VIM via apt
  apt: name=vim state=installed
  when: ansible_os_family == &quot;Debian&quot;
  7、register:保存shell命令的执行结果
  有时候我们可能还需要更复杂的例子,比如判断前一
  个命令的执行结果,根据结果处理后面的操作,这时候我们就需要 register
  模块来保存前一个命令的返回状态,在后面进行调用
  ---
  - hosts: web
  remote_user: root
  vars:
  username: plj
  tasks:

  - shell:>  register: result
  - name: change &quot;{{username}}&quot; password
  user: name={{username}} password={{'12345678'|password_hash('sha512')}}
  when: result
  ---
  - hosts: db
  remote_user: root
  tasks:
  - shell:  uptime | awk '{printf $(9)}' | cut -b '1-4'
  register: result
  - service: name=httpd state=stopped
  when: result.stdout | float > 0.5
  8、with_items 是 playbook 标准循环
  为不同用户定义不同组
  ---
  - hosts: db
  remote_user: root
  tasks:
  - user:
  name: &quot;{{item.name}}&quot;
  groups: &quot;{{item.group}}&quot;
  with_items:
  - {name: 'nb', group: 'root'}
  - {name: 'dd', group: 'root'}
  - {name: 'jj', group: 'usetr'}
  - {name: 'hh', group: 'usetr'}
  9、tags:给指定的任务定义一个调用标识
  ---
  - hosts: web
  remote_user: root
  vars:
  soft: httpd
  tasks:
  - name: install {{soft}}
  yum: name={{soft}}
  - name: config httpd.conf
  copy: src=/root/playbook/httpd.conf dest=/etc/httpd/conf/httpd.conf
  - name: config services
  service: enabled=yes state=restarted name={{soft}}
  tags: restartweb
  10、debug:
  ansible-playbook --syntax-check playbook.yaml   //语法检测
  ansible-playbook -C playbook.yaml      //测试运行

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-542706-1-1.html 上篇帖子: Ansible杂记(2) 下篇帖子: 源码安装Ansible-Linux ......
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表