ansible

下载epel源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

安装

yum install -y ansible 

ansible命令格式

Usage: ansible <host-pattern> [options]
    -a MODULE_ARGS 模块参数
    -C, --check  检查语法
    -f FORKS 并发
    --list-hosts 列出主机列表
    -m MODULE_NAME 模块名字

想要使用管控机控制远程主机: 需要使用ssh认证方式进行认证

ssh-copy-id 远程主机ip

  ssh认证方式 :

    密码认证

    秘钥认证 :

      ssh-Keygen   生成秘钥对

      ssh-copy-id 远程主机ip  复制公钥到远程主机

      私钥加密, 公钥解密

连接成功之后, 可以对已经连接的机器操作

ansible 192.168.12.26 -m ping  ping 一台机器
ansible 192.168.12.26,192.168.12.28 -m ping ping多台机器
ansible all -m ping ping所有机器
ansible web -m ping ping 一个组
ansible 'web:!db' -m ping ping web中有但是db中没有
ansible "web:&db" -m ping ping web和db的交集
ansible "web:db" -m ping  ping web和db的并集

ansible配置文件hosts的内容 :

vim /etc/ansible/hosts
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character #是注释
#   - Blank lines are ignored 空行被忽略
#   - Groups of hosts are delimited by [header] elements []表示主机组
#   - You can enter hostnames or ip addresses  可以输入主机名或者ip地址
#   - A hostname/ip can be a member of multiple groups 一台主机可以被分配多个组
 www[001:006].example.com www001到www006.example.com

 host-pattern格式 :

  • 单个的机器
  • 多个的机器,逗号隔开
  • 全部机器,all
  • 可以写一个分组
  • 可以写多个分组 
    • 并集
    • 逗号隔开
    • 冒号隔开
    • 交集,:&隔开
    • 差集: :!隔开

ansible-doc查看模块帮助信息 :

 ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
 -j 以json格式显示所有模块信息
 -l 列出所有的模块
 -s 显示模块的摘要信息
 # 直接显示模块的所有帮助信息

  ansible特性: 幂等性, 不管执行几次, 结果都是相同的


命令相关

command :

ansible web -a 'ls'
ansible web -a 'chdir=/tmp pwd'  # 先切换目录,在执行相应的命令,一般情况下在编译时候使用
ansible web -a 'creates=/tmp pwd' # 如果creates的文件存在,则不执行后面的操作
ansible web -a 'removes=/tmp pwd' # 如果removes的文件存在,则执行后面的操作
ansible web -a 'removes=/tmp mkdir /data' # 会执行后面的mkdir命令
ansible web -a 'creates=/data2 mkdir /data2' #会执行后面的mkdir命令

补充 :

查看用户是否被创建成功
tail -1 /etc/passwd
tail -1 /etc/shadow
id
echo '1' | passwd --stdin xxx 非交互式设置密码

shell :

<>|;& $ 这些特殊字符command不支持
ansible web -m shell -a 'echo "1" | passwd --stdin xxx' 设置xxx的密码
ansible 192.168.12.25 -m shell -a '/root/a.sh' 执行shell脚本,前提是脚本有可执行权限
ansible 192.168.12.25 -m shell -a '/root/a.py' 执行python脚本,前提是脚本有可执行权限

script :

ansible db -m script -a '/root/m.sh' 执行管控机上的文件
ansible web -m script -a 'creates=/root/a.sh /root/m.sh' # 查看的是被管控机上的文件是否存在

文件相关的模块

copy :

ansible 远程主机的ip/组 -m copy -a "dest=预copy文件路径/文件名 src=远程主机存放路径/文件名"
 ansible db -m copy -a "dest=/tmp/a.sh src=/root/m.sh" 复制文件到远程主机
 ansible db -m copy -a "dest=/tmp/a.sh src=/root/m.sh backup=yes" 复制文件并备份远程文件
 ansible web -m copy -a "dest=/tmp/a.sh src=/root/m.sh owner=xxx mode=700" 修改复制后的文件的属主和权限
 ansible web -m copy -a "src=/etc/init.d dest=/tmp" 复制目录到远程主机
 ansible web -m copy -a "src=/etc/init.d/ dest=/tmp" 复制目录里面的文件到远程主机
 ansible web -m copy -a "src=/etc/ansible dest=/tmp owner=xxx" 复制目录到远程主机,并修改目录的属主,并且里面文件的属主也被修改了
 ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语' dest=/tmp/b.txt"  直接将content里面的内容添加到dest的文件里面

file :

ansible cache -m file -a "path=/tmp/ddd state=directory" 创建一个目录
ansible cache -m file -a "path=/tmp/ddd.txt  state=touch" 创建一个文件
ansible cache -m file -a "path=/tmp/t  state=link src=/etc/init.d" 创建软连接 path是目标文件 src是源文件
ansible cache -m file -a "path=/tmp/t  state=absent " 删除文件

补充 :

ln -s 原文件地址 目标文件地址   创建软连接
ln 创建硬链接
原文地址:https://www.cnblogs.com/dong-/p/10375608.html