ddddddf 发表于 2018-7-29 12:15:46

ansible 自动化基础

  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:可实现多级指挥。
  优点
  (1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
  (2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
  (3)、使用python编写,维护更简单,ruby语法过于复杂;
  (4)、支持sudo。
  1. 安装
  yum install -y epel-release
  yum install -y ansible
  2.配置
  (1) ssh密钥配置
  首先生成密钥对
  ssh-keygen -t rsa直接回车即可,不用设置密钥密码

  这样会在root家目录下生成.ssh目录,这里面也会生成两个文件>  然后把公钥(id_rsa.pub)内容放到对方机器的/root/.ssh/authorized_keys里面,包括本机
  cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
  对方机器上要配置好 authorized_keys文件的权限
  chmod 600 /root/.ssh/authorized_keys
  还需要关闭selinux
  (2) ansible 配置
  vi/etc/ansible/hosts//增加
  
  192.168.1.2
  192.168.1.5
  说明: testhost为主机组名字,自定义的。 下面两个ip为组内的机器ip。
  3. 远程执行命令
  ansibletesthost -m command -a 'w'
  这样就可以批量执行命令了。这里的testhost 为主机组名,当然我们也可以直接写一个ip,针对某一台机器来执行命令。
  错误: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
  解决: yum install -y libselinux-python
  4. 拷贝文件或者目录
  ansible testhost-m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0644"
  注意:源目录会放到目标目录下面去。
  5. 远程执行一个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 mod=0755"
  最后是批量执行该shell脚本
  ansible testhost -m shell -a "/tmp/test.sh"
  shell模块,还支持远程执行命令并且带管道
  ansible testhost -m shell -a "cat /etc/passwd|wc -l "
  6. 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"
  7. 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查看指定模块的文档
  8.playbook
  相当于把模块写入到配置文件里面
  例:
  cat/etc/ansible/test.yml
  ---
  - hosts: testhost
  remote_user: root
  tasks:
  - name: test_playbook
  shell: touch /tmp/test.txt
  说明:
  hosts参数指定了对哪些主机进行参作;
  user参数指定了使用什么用户登录远程主机操作;
  tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来。
  执行:
  ansible-playbook test.yml
  再来一个例子:
  vim/etc/ansible/create_user.yml
---  
- name: create_user
  
hosts: testhost
  
user: root
  
gather_facts: false
  
vars:
  
    - user: "test"
  
tasks:
  
    - name: create user
  
      user: name="{{user}}"
  说明:
  name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;
  gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;
  vars参数,指定了变量,这里指字一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;
  user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。
  循环with_items:
---  
- 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 就是循环的关键
  条件when:
---  
- hosts: testhost
  
remote_user: root
  
gather_facts: True
  
tasks:
  
    - name: use when
  
      shell: touch /tmp/when.txt
  
      when: facter_ipaddress == "192.168.1.6"
  模块handlers:
  执行task之后,服务器发生变化之后要执行的一些操作,比如我们修改了配置文件后,需要重启一下服务,具体示例
---  
- 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相关命令。 这种比较适合配置文件发生更改后,重启服务的操作。
  9. 实际应用- 安装nginx
  nginx.yaml
  ---
  - hosts: all
  tasks:
  - name: Install Nginx Package
  yum: name=nginx state=present
  - name: Copy Nginx.conf
  template: src=./nginx.conf.j2 dest=/etc/nginx/nginx.conf owner=root group=root mode=0644 validate='nginx -t -c %s'
  notify:
  - Restart Nginx Service
  handlers:
  - name: Restart Nginx Service
  service: name=nginx state=restarted
  --------------------------------------------
  也可以用源码脚本方式安装
vim /ansible/roles/nginx/tasks/main.yml  
- name: copy nginx_tar_gz to client
  
copy: src=nginx-1.8.0.tar.gz dest=/tmp/nginx-1.8.0.tar.gz
  
- name: copy install_shell to client
  
copy: src=install_nginx.sh dest=/tmp/install_nginx.sh
  
- name: install nginx
  
shell: /bin/bash /tmp/install_nginx.sh
vim install_nginx.sh  
#!/bin/bash
  
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
  
groupadd -r nginx
  
useradd -s /sbin/nologin -g nginx -r nginx
  
cd /tmp
  
tar xf nginx-1.8.0.tar.gz;cd nginx-1.8.0
  
mkdir /application/nginx/;chown nginx.nginx /application/nginx/
  
./configure \
  
--prefix=/usr \
  
--sbin-path=/usr/sbin/nginx \
  
--conf-path=/etc/nginx/nginx.conf \
  
--error-log-path=/var/log/nginx/error.log \
  
--pid-path=/application/nginx/nginx.pid \
  
--user=nginx \
  
--group=nginx \
  
--with-http_ssl_module
  
make && make install
  
egrep -v "#|^$" /etc/nginx/nginx.conf >/etc/nginx/nginx.conf.bak
  
rm -rf /etc/nginx/nginx.conf
  
mv /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf
  
sed -i"/^\s*index / i proxy_pass http://localhost:8080;" /etc/nginx/nginx.conf
  
/usr/sbin/nginx
  ===================================
  ===================================
  tomcat.yml
  #这个是用yum模块安装jdk,有需要的可以用
  #- name: install java
  #yum: name=java-1.8.0-openjdk.x86_64   state=present
  ===============================================================
  #创建用户
  - name: group
  group: name=tomcat
  - name: user
  user: name=tomcat group=tomcat home=/usr/tomcat
  sudo: True
  ##############################这个源码包安装JDK#############################
  #复制jdk到tmp目录下
  - name: copy jdk-8u73-linux-x64.gz
  copy: src=jdk-8u73-linux-x64.gz dest=/tmp/jdk-8u73-linux-x64.gz
  #解压jdk包到/application
  - name: Extract archive jdk
  command: /bin/tar xf /tmp/jdk-8u73-linux-x64.gz -C /application
  #改名
  - name: java
  shell: mv /application/jdk1.8.0_73 /application/java
  #添加环境变量
  - name: add /etc/profile
  lineinfile: dest=/etc/profile regexp="^JAVA_HOME=" line="JAVA_HOME=/application/java/"
  - name: add /etc/profile
  lineinfile: dest=/etc/profile regexp="^CLASS_PATH=" line="CLASS_PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
  - name: add /etc/profile
  lineinfile: dest=/etc/profile regexp="^PATH=\$PATH:\$JAVA_HOME" line="PATH=$PATH:$JAVA_HOME/bin"
  - name: add /etc/profile
  lineinfile : dest=/etc/profile regexp="^export JAVA_HOME" line="export JAVA_HOME"
  ##########################安装tomcat###########################################
  - name: copy tomcat_tar_gz
  copy: src=apache-tomcat-8.0.29.tar.gz dest=/tmp/apache-tomcat-8.0.29.tar.gz
  #解压tomcat到opt目录
  - name: Extract archive
  command: /bin/tar xf /tmp/apache-tomcat-8.0.29.tar.gz -C /opt
  #创建软连接
  - name: Symlink install directory
  file: src=/opt/apache-tomcat-8.0.29/ dest=/application/tomcat state=link
  #赋予目录权限
  - name: Change ownership of Tomcat installation
  file: path=/application/tomcat/ owner=tomcat group=tomcat state=directory recurse=yes
  #推送配置文件
  - name: Configure Tomcat users
  template: src=tomcat-users.xml dest=/application/tomcat/conf/
  notify: restart tomcat
  #安装tomcat。init启动脚本
  - name: Install Tomcat init script
  copy: src=tomcat-initscript.sh dest=/etc/init.d/tomcat mode=0755
  #开启tomcat
  - name: Start Tomcat
  service: name=tomcat state=started enabled=yes
页: [1]
查看完整版本: ansible 自动化基础