Ansible - Ad-Hoc命令行学习

Ad-Hoc命令行

一、查看模块
ansible-doc -l 查看所有模块
ansible-doc yum 查看指定模块
二、模块分类
1、执行命令模块
 command 模块
#示例
ansible web -a "hostname" (-m command 默认,可以不加)

 shell 模块
#示例
ansible web -i hosts -m shell -a 'ip a | grep eth0'

 script 模块
#示例
ansible web -m script -a "/server/scripts/yum.sh"
2、软件管理模块
 yum 模块
参数:
name #指定要安装的软件包名称
state #指定使用yum的方法 
          installed,present #安装软件包 
          removed,absent #移除软件包 
          latest #安装最新软件包 
list=ansible #列出当前仓库可用的软件包 
enablerepo 开启某个yum源 
disablerepo="epel,zabbix" #安装软件时,不从哪些仓库获取
download_only=true #仅下载软件包,不安装
exclude 排除

#示例一、安装当前最新的Apache软件,如果存在则不安装 
ansible web -m yum -a "name=httpd state=present" -i hosts

#示例二、安装当前最新的Apache软件,通过epel仓库安装 
ansible web -a "name=httpd state=present enablerepo=epel" -i hosts
ansible web -m yum -a 'name=cowsay,sl state=present enablerepo=epel'

#示例三、通过互联网的rpm进行安装
ansible web -m yum -a "name=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.0-1.el7.x86_64.rpm state=present"

#示例四、安装最新版本的Apache软件,如果存在则更新Apache (了解) 
ansible web -m yum -a "name=httpd state=latest" -i hosts

#示例五、更新所有的软件包,但排除和kernel相关的 
ansible web -m yum -a "name=* state=latest exclude=kernel"  -i hosts

#示例六、删除Apache软件 
ansible web -m yum -a "name=httpd state=absent" -i hosts

 yum_repository 模块
参数:
name #yum源的名字
baseurl #地址
file #指定yum配置文件的路径和名称 注意不需要以.repo结尾 默认使用 name的内容作为文件名
enabled yes/no 是否开启yum源 默认是 yes 开启
state #absent(删除)/present(配置 安装 这个是默认的)
description #描述信息
gpgcheck=
gpgkey=

#添加yum仓库
ansible web -i hosts -m yum_repository -a 'name=php description="php repo" baseurl="http://us-east.repo.webtatic.com/yum/el7/x86_64/" enabled=no state=present'

#删除yum仓库
ansible web -i hosts -m yum_repository -a 'name=php state=absent'

#修改yum仓库
ansible web -i hosts -m yum_repository -a 'name=epel  description=EPEL baseurl=https://download.fedoraproject.org/pub/epel/$releasever/$basearch/ gpgcheck=no enabled=no file=epel'
3、文件管理模块
 file 模块
参数:
path #指定远程主机目录或文件信息 
recurse #递归授权,只有 state 为 directory的时候 才能使用
state #状态 
         directory #在远端创建目录 
         touch #在远端创建文件 
         link #link或hard表示创建链接文件 
        absent #表示删除文件或目录 
mode #设置文件或目录权限 
owner #设置文件或目录属主信息 
group #设置文件或目录属组信息

#1. 创建目录 
ansible web -m file -a 'path=/code/src/nginx state=directory' -i hosts
#2. 创建文件 
ansible web -m file -a 'path=/code/src/nginx/test.txt state=touch' -i hosts 
#3. 递归修改权限 所有者 
ansible web -m file -a 'path=/code/src/ state=directory owner=nobody group=nobody mode=600 recurse=yes' -i hosts

 copy 模块
参数:
src #推送数据的源文件信息 
dest #推送数据的目标路径 
backup #对推送传输过去的文件,进行备份 
content #直接批量在被管理端文件中添加内容 
group #将本地文件推送到远端,指定文件属组信息 
owner #将本地文件推送到远端,指定文件属主信息 
mode #将本地文件推送到远端,指定文件权限信息

#1.拷贝文件至被控节点 
ansible web -m copy -a "src=/etc/hosts dest=/tmp/test.txt" 
#2.对远端已有文件进行备份,按照时间信息备份 
ansible web -m copy -a "src=/etc/hosts dest=/tmp/test.txt backup=yes" 
#3.复制目录 并修改所有者与权限 
ansible web -m copy -a 'src=/etc/sysconfig/network-scripts/ dest=/tmp/ owner=nobody group=nobody mode=600' -i hosts 
#4 content 内容 写入文件内容 重定向 > 
ansible web -m copy -a 'content="weblinux.cn" dest=/tmp/web.txt' -i hosts
ansible web -a 'cat /tmp/web.txt' -i hosts
#5.向被控端主机写入数据,并且会覆盖远端文件内原有数据信息
ansible web -m copy -a "content='weblinux.cn' dest=/tmp/test.txt"

 url_get 模块
参数:
url #文件在网络上的具体位置 
dest #下载到被控端的哪个目录下 
checksum #校验(md5 sha256)

#1.通过get_url下载文件或者软件 
ansible web -m get_url -a 'url=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-get-5.0.1-1.el7.x86_64.rpm dest=/tmp/' 
#2.下载一个文件前先进行md5校验,通过则下载,不通过则失败 
ansible web -m get_url -a "url=http,https dest=/opt mode=0777 checksum=md5:76eb3af80ffd" -i ./hosts
4、服务管理模块
 systemd 模块
参数:
name # 定义要启动服务的名称 
state # 指定服务状态 
     started #启动服务 
     stopped #停止服务 
     restarted #重启服务 
     reloaded #重载服务
enabled #开机自启

#开启或关闭服务
ansible all -m systemd -a 'name=crond state=stopped' 
ansible all -m systemd -a 'name=crond state=started' 
ansible all -m systemd -a 'name=crond state=reloaded或restarted'

#开机自启动 
ansible all -m systemd -a 'name=crond enabled=yes' 

#开机自启动并启动服务 
ansible all -m systemd -a 'name=crond enabled=yes state=started'

 service 模块
参数和systemd一样

#1.启动crond服务,并加入开机自启
ansible webservers -m service -a "name=crond state=started enabled=yes"
#2.停止crond服务,并删除开机自启 
ansible webservers -m service -a "name=crond state=stopped enabled=no" 
#3.重启crond服务 
ansible webservers -m service -a "name=crond state=restarted"
#4.重载crond服务 优雅的重启 重新读取配置文件 
ansible webservers -m service -a "name=crond state=reloaded"
5、用户管理模块
 group 模块
参数:
name #指定创建的组名 
gid #指定组的gid 
state:
   absent #移除远端主机的组 
   present #创建远端主机的组(默认)
#示例
ansible web -m group -a "name=rsync2 gid=10086 state=present" 

 user 模块
参数:
uid #指定用户的uid 
group #指定用户组名称 
groups #指定附加组名称
password #给用户添加密码(记得单引号)如:-a "name=www password='加密后的密码'" 
shell #指定用户登录shell 
create_home #是否创建家目录 
state #present /absent

#1.创建用户指定uid和gid,不创建家目录也不允许登陆
ansible web -m user -a "name=rsync2 uid=10086 group=rsync2 shell=/sbin/nologin create_home=no"
#2.删除用户 指定用户名即可 userdel 
ansible web -m user -a "name=rsync2 state=absent"
6、定时任务模块
 crond 模块
参数:
name #指定名字 
minute #分
hour #时
day #日
month #月
weekday #周
job #指定脚本、命令
state present(添加 默认)/absent(删除)
disabled 是否注释

# 使用ansible添加一条定时任务
ansible webservers -m cron -a "minute=* hour=* day=* month=* weekday=* job='/bin/sh test.sh'"
ansible webservers -m cron -a "job='/bin/sh test.sh'" 

# 设置定时任务注释信息,防止重复,name设定 
ansible webservers -m cron -a "name='cron01' job='/bin/sh test.sh'"

# 删除相应定时任务 
ansible webservers -m cron -a "name='ansible cron02' minute=0 hour=0 job='/bin/sh test.sh' state=absent" 
ansible webservers -m cron -a "name='ansible cron02' state=absent" 

# 注释相应定时任务,使定时任务失效 
ansible webservers -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh test.sh' disabled=yes"
ansible webservers -i hosts -m cron -a 'name="sync02" minute="*/2" job="/sbin/ntpdate ntp1.aliyun.com &>/dev/null"'

# 检查
ansible webservers -i hosts -a 'crontab -l'

# 注释+检查
ansible webservers -i hosts -m cron -a 'name="sync02" minute="*/2" job="/sbin/ntpdate ntp1.aliyun.com &>/dev/null" disabled=yes' 
ansible webservers -i hosts -a 'crontab -l'
7、磁盘挂载模块
 mount 模块
参数:
src 指定源 
path 指定目标 挂载点 
fstype 指定文件系统类型 nfs 
state
    present # 仅修改配置 开机挂载,仅将挂载配置写入/etc/fstab 
    mounted # 挂载+修改配置 挂载设备,并将配置写入/etc/fstab
    unmounted # 卸载设备,不会清除/etc/fstab写入的配置 
    absent # 卸载设备,会清理/etc/fstab写入的配置 
    remounted #重新挂载
    
#在backup服务器上安装nfs 
ansible web -i hosts -m yum -a 'name=nfs-utils state=present' 
#添加配置文件
ansible web -i hosts -m copy -a 'content="/backup/ 172.16.1.0/24(rw,all_squash)" dest=/etc/exports backup=yes' 
#创建目录 修改所有者 
ansible web -i hosts -m file -a 'path=/backup/ owner=nfsnobody group=nfsnobody state=directory'
#启动服务并开机自启动 
ansible web -i hosts -m service -a 'name=rpcbind state=started enabled=yes' 
ansible web -i hosts -m service -a 'name=nfs state=started enabled=yes'
#backup上面进行挂载(本地测试) 
ansible backup -i hosts -m mount -a 'src=172.16.1.41:/backup/ path=/mnt/ fstype=nfs state=mounted' 
#web服务器进行挂载 挂载到web服务器的 /code/upload/img 
ansible web -i hosts -m mount -a 'src=172.16.1.41:/backup path=/code/upload/img fstype=nfs state=mounted'

#10.0.0.7作为nfs服务端,10.0.0.8作为nfs客户端挂载 
ansible web01 -m yum -a 'name=nfs-utils state=present' -i ./hosts 
ansible web01 -m file -a 'path=/data state=directory' -i ./hosts 
ansible web01 -m copy -a 'content="/data 172.16.1.0/24(rw,sync,no_all_squash)" dest=/etc/exports' - i ./hosts 
ansible web01 -m systemd -a "name=nfs state=started enabled=yes" -i ./hosts 
#配置挂载 
ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=present" 
ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=mounted" 
ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=unmounted"
ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=absent"
8、防火墙管理模块
 Selinux 模块
ansible webservers -m selinux -a "state=disabled" -i ./hosts

 firewalld 模块
参数:
service #指定开放或关闭的服务名称 
port #指定开放或关闭的端口 
masquerade #开启地址伪装 
immediate #临时生效 
permanent #是否添加永久生效 
state #开启或是关闭 
zone #指定配置某个区域 
rich_rule #配置富规则 
source #指定来源IP
#示例
ansible webservers -m systemd -a "name=firewalld state=started" -i ./hosts
ansible webservers -m firewalld -a "service=http immediate=yes permanent=yes state=enabled" - i ./hosts 
ansible webservers -m firewalld -a "port=8080-8090/tcp immediate=yes permanent=yes state=enabled" -i ./hosts 

 iptables 模块
参数:
table #-t 
action #默认是append追加-A insert #插入-I 
chain #指定链 
source #-s 指定源ip ※※※※※ 
destination #-d 指定目标ip 
protocol #-p 指定协议 
source_port #--sport指定源端口 
destination_port #--dport指定目标端口 ※※※※ 
jump #-j DROP/ACCEPT 
state #present(默认,添加规则) absent(删除)
				#ansible ad-hoc练习案例 
1.安装nginx服务 
#yum_repository 
ansible web -i hosts -m yum_repository -a 'name=nginx description="nginx repo" baseurl=http://nginx.org/packages/centos/7/x86_64/ enabled=yes gpgcheck=no state=present' 
#yum 
ansible web -i hosts -m yum -a 'name=nginx state=installed'
2.编写简单网页测试内容 
ansible web -i hosts -m copy -a 'content="This is a test page." dest=/usr/share/nginx/html/index.html'
3.启动服务不加入开机自启 #systemd/service 
ansible web -i hosts -m systemd -a 'name=nginx state=started enabled=yes' 
4.放行对应的端口 #iptables 
ansible web -i hosts -m iptables -a 'table=filter action=append chain=INPUT protocol=tcp destination_port=80 jump=ACCEPT'
分享到:
关键词:Ansible

网友留言(0 条)

发表评论