色胃康胶囊 发表于 2018-7-30 08:31:38

ansible安装以及配置

  特性
  (1)、no agents:不需要在被管控主机上安装任何客户端;
  (2)、no server:无服务器端,使用时直接运行命令即可;
  (3)、modules in any languages:基于模块工作,可使用任意语言开发模块;
  (4)、yaml,not code:使用yaml语言定制剧本playbook;
  (5)、ssh by default:基于SSH工作;
  (6)、strong multi-tier solution:可实现多级指挥。
  (7)、支持图形化界面,10台以内免费
  优点
  (1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
  (2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
  (3)、使用python编写,维护更简单,ruby语法过于复杂;
  (4)、支持sudo。
  
  1. 安装
  yum install -y epel-release
  yum install -y ansible
  2.配置
  ssh密钥配置
  首先生成密钥对
  ssh-keygen -t rsa直接回车即可,不用设置密钥密码

  这样会在root家目录下生成.ssh目录,这里面也会生成两个文件>  然后把公钥(id_rsa.pub)内容放到对方机器的/root/.ssh/authorized_keys里面,包括本机
  vim /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
  对方机器上要配置好 authorized_keys文件的权限
  chmod 600 /root/.ssh/authorized_keys
  还需要关闭selinux
  3、测试远程登陆
  ssh client.river.com
  如果没有ssh命令则安装
  yum install -y openssh-clients
  
  4. 远程执行命令
  先配置ansible
  vim/etc/ansible/hosts//增加
  
  127.0.0.1
  10.10.13.247
  说明: testhost为主机组名字,自定义的。 下面两个ip为组内的机器ip
  ansibletesthost -m command -a 'w'
  这样就可以批量执行命令了。这里的testhost 为主机组名,当然我们也可以直接写一个ip,-m后跟模块名command,针对某一台机器来执行命令。
  错误: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
  解决: yum install -y libselinux-python
  也可以使用shell模块
  ansibletesthost -m shell -a 'w' #shell模块可以解决一些cmd没法解法的问题比如cmd模块不能使用管道
  
  5. 拷贝文件或者目录
  ansible testhost-m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0644"
  注意:源目录会放到目标目录下面去。
  
  6. 远程执行一个shell脚本
  首先创建一个shell脚本
  vim/tmp/test.sh//加入内容
  #!/bin/bash
  echo `date` > /tmp/ansible_test.txt
  然后把该脚本分发到各个机器上
  ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
  最后是批量执行该shell脚本
  ansible testhost -m shell -a "/tmp/test.sh"
  #或者不需要给文件执行权限直接使用命令ansible testhost -m shell -a "/bin/sh /tmp/test.sh"
  shell模块,还支持远程执行命令并且带管道
  ansible testhost -m shell -a "vim /etc/passwd|wc -l "
  7. cron
  ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt'weekday=6"
  若要删除该cron 只需要加一个字段 state=absent
  ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt'weekday=6 state=absent"
  
  8. yum和service
  ansible testhost -m yum -a "name=httpd"    #需要安装的包需要精准的名字不支持模糊匹配
  ansible testhost -m service -a "name=httpd state=started enabled=yes"
  文档使用:
  ansible-doc -l   列出所有的模块
  ansible-doc cron查看指定模块的文档
  
  9.playbook
  相当于把模块写入到配置文件里面
  例:
  vim/etc/ansible/test.yml
  ---
  - hosts: testhost
  remote_user: root #也可以使用user
  tasks:
  - name: test_playbook
  shell: touch /tmp/river.txt   #注意,第一行三个减号,第二行顶格,各级之间差两个空格
  说明:
  hosts参数指定了对哪些主机进行参作;
  user参数指定了使用什么用户登录远程主机操作;
  tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来。
  执行:
  ansible-playbook /etc/ansible/test.yml
  再来一个例子:
  vim/etc/ansible/create_user.yml
  ---
  - name: create_user
  hosts: testhost
  user: root            #相当于remote_user
  gather_facts: false
  vars:
  - user: "test"
  tasks:
  - name: create user
  user: name="` user `"
  说明:
  name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;
  gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;可以先使用ansible testhost -m setup收集客户端的相关信息
  vars参数,指定了变量,这里指一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;
  user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。
  
  playbook循环with_items:
  vim /etc/ansible/loop.yml
  ---
  - hosts: testhost
  user: root
  tasks:
  - name: change mod for file
  file: path=/tmp/` item ` mode=600 owner=root group=root
  with_items:
  - 1.txt
  - 2.txt
  说明: with_items 就是循环的关键
  并不会创建文件只是修改文件的权限
  playbook条件when:
  vim /etc/ansible/when.yml
  ---
  - hosts: testhost
  remote_user: root
  gather_facts: True
  tasks:
  - name: use when
  shell: touch /tmp/when.txt
  when: facter_ipaddress == "10.10.13.247"
  playbook模块handlers:
  执行task之后,服务器发生变化之后要执行的一些操作,比如我们修改了配置文件后,需要重启一下服务,具体示例
  vim /etc/ansible/handlers.yml
  ---
  - hosts: testhost
  remote_user: root
  tasks:
  - name: test copy
  copy: src=/tmp/1.txt dest=/tmp/2.txt
  notify: test handlers
  handlers:
  - name: test handlers
  shell: echo "121212" >> /tmp/2.txt
  说明,只有copy模块真正执行后,才会去调用下面的handlers相关的操作。也就是说如果1.txt和2.txt内容是一样的,并不会去执行handlers里面的shell相关命令。 这种比较适合配置文件发生更改后,重启服务的操作。
页: [1]
查看完整版本: ansible安装以及配置