ansible之安装及模块一
控制主机安装ansible
mv /etc/yum.repos.d/epel-7.repo{,_bak}
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y ansible
创建秘钥对配置ssh秘钥登录
ssh-keygen #生成秘钥对
ssh-copy-id #复制公钥到远程主机
ssh-copy-id 192.168.8.121 -p 22
ansible命令格式
ansible
-a MODULE_ARGS, --args MODULE_ARGS #模块的参数
-C, --check #检查
-f
--list-hosts #列出主机列表
-m MODULE_NAME #模块名
--syntax-check #语法检查
#查看ansible生成文件
rpm -ql ansible|more
/etc/ansible
/etc/ansible/ansible.cfg #主配置文件
/etc/ansible/hosts #主机配置文件
/etc/ansible/roles
/etc/ansible/hosts #主机配置文件
# This is the default ansible 'hosts' file.
#
# 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
# - A hostname/ip can be a member of multiple groups 一个主机可以在多个组中
192.168.8.32
192.168.8.33
192.168.8.34
#若不是默认root,默认22端口,可通过配置指定
ansible_ssh_user=root ansible_ssh_port=22000
ping模块 不是ICMP协议ping
ansible 192.168.8.34 -m ping
192.168.8.34 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
#也可以通过密码登录加-k参数 注意要使用ssh先登录一次
ansible 192.168.8.34 -m ping -k
#检查所有主机
ansible all -m ping
#部分主机
ansible 192.168.8.32,192.168.8.33 -m ping
#分组
[web]
192.168.8.32
192.168.8.33
192.168.8.34
[db]
192.168.8.35
192.168.8.36
[cache]
192.168.8.36
#分组范围
[web]
192.168.8.[32:34]
#查看组中所有主机
ansible web --list-hosts
#并集
ansible web,db -m ping
ansible 'web:db' -m ping
#交集
ansible 'web:&db' -m ping
#差集
ansible 'web:!db' -m ping #web中有db中无
ansible-doc 查看模块帮助信息
-j json #返回帮助信息
-l #列出所有ansible模块
-s #片段式显示帮助信息
命令相关模块 默认
command
ansible web -m command -a 'ls /' #默认是command
ansible web -a 'ls /' # 可以简写
#参数
chdir #切换目录
ansible web -a 'chdir=/tmp pwd' #编译安装时
creates #如果文件或文件夹不存在就执行 否则跳过
ansible web -a 'creates=/tmp2 pwd'
removes #如果文件或文件夹存在就执行 否则跳过
ansible web -a 'removes=/tmp pwd'
例子
#批量创建用户
ansible web -a 'useradd ropon'
#查看是否创建成功
ansible web -a 'id ropon'
#批量设置密码
ansible web -a 'echo "west.cn"|passwd --stdin ropon'
#因command模块不支持特殊字符 < > | ; & 所以批量设置密码不成功 需要使用shell模块
shell模块
ansible web -m shell -a 'echo "west.cn"|passwd --stdin ropon'
#参数
chdir #切换目录
ansible web -m shell -a 'chdir=/tmp pwd' #编译安装时
creates #如果文件或文件夹不存在就执行 否则跳过
ansible web -m shell -a 'creates=/tmp2 pwd'
removes #如果文件或文件夹存在就执行 否则跳过
ansible web -m shell -a 'removes=/tmp pwd'
#执行远程主机上脚本
cat test.sh
#!/bin/bash
touch ansible_test_x.txt
ansible 192.168.8.32 -m shell -a 'bash /root/test.sh'
ansible 192.168.8.32 -m shell -a '/root/test.sh' #前提此脚本文件有x执行权限
cat python_test.py
#!/bin/python
print "python echo test!"
#同理可执行python脚本
ansible 192.168.8.32 -m shell -a 'python /root/python_test.py'
ansible 192.168.8.32 -m shell -a '/root/python_test.py' #前提此脚本文件有x执行权限
script模块 执行控制主机上脚本
cat test.sh
#!/bin/bash
touch /root/control_ansible_test.txt
ansible web -m script -a '/root/test.sh'
#查看是否创建成功
ansible web -a 'ls /root'
#参数
chdir #切换目录
ansible web -m shell -a 'chdir=/tmp pwd' #编译安装时
creates #如果远程主机上此文件或文件夹不存在就执行控制主机上脚本 否则跳过
ansible 192.168.8.32 -m script -a 'creates=/root/root.sh /root/test.sh'
removes #如果远程主机上此文件或文件夹存在就执行控制主机上脚本 否则跳过
ansible 192.168.8.32 -m script -a 'removes=/root/root.sh /root/test.sh'
文件相关模块
copy模块
#参数
backup #创建一个备份文件,以时间戳结尾 backup=yes
dest #目的地址
content
group #修改所属组
mode #修改权限
owner #修改所有者
src #源文件
#复制控制主机本地文件到远程主机
ansible db -m copy -a 'src=/root/test.sh dest=/root/test_copy.sh'
ansible db -m shell -a 'ls -l /root'
192.168.8.35 | CHANGED | rc=0 >>
总用量 20
-rw-r--r--. 1 root root 8956 2月 13 2019 1.sh
-rw-------. 1 root root 1468 2月 13 2019 anaconda-ks.cfg
-rw-r--r-- 1 root root 52 12月 21 15:17 test_copy.sh
ansible db -m copy -a 'src=/root/test.sh dest=/root/test_copy.sh mode=744' #带权限复制
ansible web -m copy -a 'src=/root/test.sh dest=/root/test_copy.sh group=ropon' #带所需组复制
ansible web -m copy -a 'src=/root/test.sh dest=/root/test_copy.sh owner=ropon' #带所有者复制
#复制本地目录到远程主机
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 'content="测试将内容写入远程主机\n" dest=/tmp/b.txt'
file模块 创建删除文件相关
#参数
group #所属组
mode #权限
owner #所有者
path
src #link hard才有效
state
directory
file
touch
link
hard
absent #删除
#创建文件夹
ansible 192.168.8.32 -m file -a 'path=/root/testdir state=directory'
#创建文件
ansible 192.168.8.32 -m file -a 'path=/root/testdir.txt state=touch'
#删除文件或文件夹
ansible 192.168.8.32 -m file -a 'path=/root/testdir.txt state=absent'
#创建软硬连接
ansible 192.168.8.32 -m file -a 'path=/root/testlink src=/etc/fstab state=link'
ansible 192.168.8.32 -m file -a 'path=/root/testlink2 src=/etc/fstab state=hard'
fetch模块 下载文件
#参数
dest #控制主机
src #远程主机
#拷贝远程主机日志信息到控制主机root目录下 给每台主机创建一个文件夹并保留原来目录结构
ansible 192.168.8.32 -m fetch -a 'dest=/root src=/var/log/messages'