sunsir 发表于 2018-7-29 10:25:28

ansible安装与配置文件

  ansible 安装

[*]  源码安装
  git clone git://github.com/ansible/ansible.git --recursive
  cd ./ansible
  source ./hacking/env-setup
  1)如果需要安装pip, 先安装对应python版本的pip:
  easy_install pip
  2)安装控制主机需要的Python模块
  pip install paramiko PyYAML Jinja2 httplib2 six
  3)更新ansible版本
  git pull --rebase
  git submodule update --init --recursive
[*]  yum 安装
  1)yum源
  centos6:rpm -Uvh http://mirrors.zju.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm
  centos7:rpm -Uvh http://mirrors.zju.edu.cn/epel/7/x86_64/e/epel_release-7-5.noarch.rpm
  yum install ansible
  3.本次为yum 安装:/etc/ansible
  配置运行环境

[*]  ansible.cfg 配置文件
  1)环境变量:export ANSIBLE_SUDO_USER=root
  2)配置参数
  inventory 资源清单,被管理的主题资源列表配置文件位置
  library 代码模块目录,多个目录可以用:隔开
  forks 设置ansible最多能有多少个进程同时工作
  sudo_user 默认指向命令用户
  remote_port 指定链接被管理节点端口,默认22
  host_key_checking 默认设置为False;类似第一次ssh时需要进行yes确认初始化known_host
  timeout ssh链接的超时间隔
  log_path 日志文件配置
  Linux主机ssh无密码访问
  测试:
  $ansible --version
  测试主机连通性/etc/ansible/host定义列表
  192.168.1.1 | SUCCESS => {
  "changed": false,
  "ping": "pong"
  }
  192.168.1.2 | SUCCESS => {
  "changed": false,
  "ping": "pong"
  }
  4.在被管理节点批量执行命令
  可以新建一个新主机清单,执行一些命令
  cat new.cfg
  
  192.168.1.1
  192.168.1.2
  $ansible test -m shell -a '/bin/echo hello ansible!' -i inventory.cfg
  $ansible test -m command -a "/bin/echohello ansible" -i new.cfg
  192.168.1.1 | SUCCESS | rc=0 >>
  hello ansible!
  192.168.1.2 | SUCCESS | rc=0 >>
  hello ansible!
  5.ansible获取帮助信息
  ansible-doc -h 帮助信息
  ansible-doc -l 列出支持的模块
  ansible-doc -s+ 直接加模块命令,显示模块描述和使用示例:ansible-doc -s yum
  执行ansible命令时在命令后加-vvv可以获得详细输出结果
页: [1]
查看完整版本: ansible安装与配置文件