0
点赞
收藏
分享

微信扫一扫

五月学习之Ansible ad-hoc和常用模块

一、Ansible ad-hoc

1、ad-hoc是什么

ad-hoc简而言之就是临时命令,执行完即结束,并不会保存
应用场景1:查看多台节点的进程是否存在
应用场景2:拷贝指定的文件至本地

2、ad-hoc命令使用

ansible 'groups' -m command -a 'df -h'

五月学习之Ansible ad-hoc和常用模块_不执行

3、ad-hoc执行过程

1. 加载ansible配置文件,默认/etc/ansible/ansible.cfg 
2. 查找对应的主机配置文件,找到要执行的主机或者组,默认/etc/ansible/hosts
3. 加载对应的模块文件:command
4. ansible指挥目标主机执行命令 
  4.1 控制端生成临时执行文件          ~/.ansible/cp
  4.2 通过连接插件,与目标主机建立连接        paramiko_ssh
  4.3 通过插件功能将控制端的临时执行文件传输至目标主机的临时目录   ~/.ansible/tmp
  4.4 指挥目标主机给临时执行文件添加执行权限
  4.5 目标主机执行临时文件并将结果返回给控制主机 
  4.6 目标主机删除临时执行py文件,然后断开连接
5. ansible控制端显示结果,并退出。

4、ad-hoc执行状态

绿色:被管理端主机没有被修改

黄色:被管理端主机发现变更

红色:出现了故障,注意查看提示

二、常用模块

1、command

chdir:   #执行Ansible时,切换到指定的目录
creates: #如果文件存在,则跳过执行
removes: #如果文件存在,则执行

备份:如果备份的文件存在,则不执行备份的命令;如果文件不存在,则执行备份的命令;
ansible webservers -m command -a "creates=/tmp/log ifconfig eth0"

sersync2   --》判断该命令是否存在,如果存在就启动该命令,如果不存在就不执行;
如果文件存在,就执行,如果不存在,就不执行;
ansible webservers -m shell -a "removes=/tmp/log ifconfig eth0"

2、shell

shell支持  |管道符号;
command并不支持该类符号;
什么命令都可以执行  (SHell命令);

3、yum

name:			软件包名称
state:			状态
	present		安装
	absent		删除
	latest		最新版
enablerepo		通过哪个仓库获取
disablerepo		不使用哪些仓库的包
exclude			kernel排除
	
1.安装vsftpd软件包
ansible webservers -m yum -a 'name=vsftpd state=present'
2.删除vsftpd
ansible webservers -m yum -a 'name=vsftpd state=absent'
3.安装httpd服务,必须从epel仓库中安装(所有的被控都有这个epel仓库)
ansible webservers -m yum -a 'name=httpd state=present enablerepo=epel'
4.更新所有的软件包,唯独kernel程序不更新;
ansible webservers -m yum -a 'name=* state=present exclude="kernel*"'

4、copy

src:   控制端的源文件路径;
	dest: 被控端的文件路径;
  owner: 属主
  group: 属组
  mode:  权限
	backup: 备份;
	validate:验证;
	content   新建文件并给文件添加内容
  
1.更新nfs配置,将控制端的exports.j2 文件同步到 被控端的 /etc/exports 
ansible webservers -m copy -a 'src=./exports.j2 dest=/etc/exports owner=root group=root mode=0644 backup=yes'

2.往一个文件中写入内容的;  rsync.pass    1
ansible webservers -m copy -a 'content="oldxu.net123" dest=/etc/rsync.pass owner=root group=root mode="0600" backup=yes'

3.验证sudo配置是否正确(adhoc测试失败):
[root@manager project]# cat tt.yaml 
- hosts: webservers
  tasks:
  - name: Copy a "sudoers" file on the remote machine for editing
    copy:
      src: ./sudoers
      dest: /etc/sudoers
      validate: /usr/sbin/visudo -csf %s

5、group

name: 指定组名称
	gid: 指定gid
	state:
		present:创建  默认
		absent:删除
		
ansible webservers -m group -a 'name=www gid=666 state=present'
ansible webservers -m group -a 'name=mysqldb system=yes state=present'

6、user

user:
    name: 				创建的名称
    uid: 				指定uid
    group: 				指定基本组
	  shell: 				登录shell类型默认/bin/bash
	  create_home			是否创建家目录
	  password			设定对应的密码,必须是加密后的字符串才行,否则不生效;
	  system				系统用户
	  groups: admins,dev  附加组
	  append: yes			追加
    state: absent		删除
    remove: yes			家目录一起结束
    
1.创建www用户,指定uid 666,基本组www
ansible webservers -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=no'

2.创建db用户,基本组是root,附加组,adm,sys
ansible webservers -m user -a 'name=db group=root groups=adm,sys append=yes shell=/bin/bash create_home=yes'

3.创建一个ddd用户,密码123,需要正常登录系统;
ansible webservers -m user -a "name=ddd password=$6$salt$jkHSO0tOjmLW0S1NFlw5veSIDRAVsiQQMTrkOKy4xdCCLPNIsHhZkIRlzfzIvKyXeGdOfCBoW1wJZPLyQ9Qx/1 shell=/bin/bash create_home=yes"

4.创建一个dev用户,并为其生成对应的秘钥
ansible webservers -m user -a 'name=dev generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=~/.ssh/id_rsa'

7、file

创建文件:
	创建目录:
	授权:
  file:
    path: 		在被控端创建的路径;
    owner: 		属主;
    group: 		属组;
    mode: 		权限
	  state: 		类型
		touch: 文件
		directory: 目录
		link: 软链接
		hard:硬链接
	  recurse: yes 递归授权
	
前置:
useradd www -u 666
useradd -u 666 -g 666 www
1.创建一个/data目录,授权为www身份
ansible webservers -m file -a 'path=/data owner=www group=www mode="0755" state=directory recurse=yes'

2.在/data目录中创建一个文件
ansible webservers -m file -a 'path=/data/books owner=www group=www mode="0644" state=touch'

8、systemd

name			服务名称
	state			服务状态
		started		启动
		stopped		停止
		restarted	重启
		reloaded	重载
	enabled			开启自启动|  yes 启   no 不
	daemon_reload: yes

1.启动服务,并加入开机自启动
ansible webservers -m systemd -a 'name=nfs state=started enabled=yes'

2.停止服务,并关闭开机自启动
ansible webservers -m systemd -a 'name=nfs state=stopped enabled=no'

9、mount

src: 源设备路径,或网络地址;
    path: 挂载至本地哪个路径下;
    fstype: 设备类型; nfs
    opts:   挂载的选项
    state:  挂载还是卸载;
		present			永久挂载,但没有立即生效
		absent			卸载,临时挂载+永久挂载
		mounted			临时挂载
		unmounted		临时卸载;

1.将172.16.1.7的/data目录挂载到  172.16.1.8 /opt目录;
ansible 172.16.1.8 -m mount -a 'src=172.16.1.7:/data path=/opt fstype=nfs opts=defaults state=mounted'

10、cron

name: 		描述信息,描述脚本的作用;
  minute: 	分钟;
  hour:		小时;
	weekday:	周;
	user:		该任务由哪个用户取运行;默认root
  job: 		任务


1.每天凌晨3点执行 /bin/bash /scripts/client_push_data_server.sh &>/dev/null
ansible webservers -m cron -a 'name="backups app data scripts" hour=03 minute=00 job="/bin/bash /scripts/client_push_data_server.sh &>/dev/null"'

[root@web01 ~]# crontab  -l
#Ansible: backups app data scripts
00 03 * * * /bin/bash /scripts/client_push_data_server.sh &>/dev/null

2.删除 backups app data script  执行;
ansible webservers -m cron -a 'name="backups app data scripts" hour=03 minute=00 job="/bin/bash /scripts/client_push_data_server.sh &>/dev/null" state=absent'


3.注释 backups app data script  执行;
ansible webservers -m cron -a 'name="backups app data scripts" hour=03 minute=00 job="/bin/bash /scripts/client_push_data_server.sh &>/dev/null" disabled=yes'

11、get_url

get_url:
    url: 下载地址
    dest: 下载到本地的路径;
    mode: 权限;
	  checksum:对资源做校验;

1.下载一个资源到本地/tmp目录;
ansible webservers -m get_url -a 'url=http://fj.xuliangwei.com/config_vpn_new.zip dest=/tmp mode=0666'


2.对下载的资源做验证:
ansible webservers -m get_url -a 'url=http://fj.xuliangwei.com/config_vpn_new.zip dest=/opt mode=0666 checksum=md5:7107b0e2e01facb067842a6c4c7ffa31'

12、unarchive

unarchive:
    src: 			控制端的源文件
    dest: 			解压到被控端的路径
	  remote_src: yes	源文本是否在被控端,yes则在,no则不在

1.将控制端的压缩包,解压到被控端;  remote_src: no
ansible webservers -m unarchive -a 'src=./test.tar.gz dest=/mnt'

2.将被控端的压缩包解压到被控端:    remote_src: yes   config_vpn_new.zip
ansible webservers -m unarchive -a 'src=/tmp/config_vpn_new.zip dest=/mnt remote_src=yes'

13、archive

1.将被控端的/opt 打包到 /mnt 目录下,并命名为 opt.tar.gz 
ansible webservers -m archive -a 'path=/opt dest=/mnt/opt.tar.gz format=gz'

14、selinux

ansible webservers -m selinux -a 'state=disabled'

15、firewalld

zone:		要操作的区域  默认public
	source:	来源地址
  service: 	 服务名称 http,https,sshd,......
	port:		端口
  permanent:	永久生效,但不会立即生效
	immediate:	临时生效;
    state: 		启用和关闭;
		disabled
		enabled

1.让被控端都放行80端口;
ansible webservers -m systemd -a 'name=firewalld state=started'
ansible webservers -m firewalld -a 'port=80/tcp immediate=yes state=present'


2.让被控端都放行https端口;
ansible webservers -m systemd -a 'name=firewalld state=started'
ansible webservers -m firewalld -a 'service=https immediate=yes state=present'

16、iptables

iptables:
	  table: 					表
    chain: 					链
    source: 				来源IP
	  destination				目标IP
	  destination_port		目标端口
	  protocol	协议
    jump: DROP	动作
	  action		如何添加规则
		  insert:插入
		  append:追加

1.来源IP是192.168.1.1 目标地址 1.1.1.1 目标端口 80  协议 tcp  则拒绝; 规则要写入第一行;
ansible webservers -m iptables -a 'table=filter chain=INPUT source=192.168.1.1/32 destination=1.1.1.1 destination_port=80 protocol=tcp jump=DROP action=insert'

2.NAT:SNAT和DNAT:
	DNAT: 如果请求1.1.1:80端口,则DNAT到2.2.2.2:8800
	ansible webservers -m iptables -a 'table=nat chain=PREROUTING protocol=tcp destination=1.1.1.1 destination_port=80 jump=DNAT to_destination="2.2.2.2:8800"'

	DNAT: 如果请求1.1.1:81端口,则DNAT到3.3.3.3:8800
	ansible webservers -m iptables -a 'table=nat chain=PREROUTING protocol=tcp destination=1.1.1.1 destination_port=81 jump=DNAT to_destination="3.3.3.3:8800"'

SNAT:  
	POSTROUTING
	iptables -t nat -I POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 5.5.5.5

ansible webservers -m iptables -a 'table=nat chain=POSTROUTING source=172.16.2.0/24 jump=SNAT to_source=6.6.6.6'
ansible webservers -m iptables -a 'table=nat chain=POSTROUTING source=172.16.3.0/24 jump=SNAT to_source=7.7.7.7 action=insert'

17、lineinfile

path 目标文件
  regexp 正则表达式,要修改的行
  line 最终修改的结果
  insertafter 将文本插到指定的行之后
  insertbefore 将文本插入到指定的行之前
  state 可以选择absent删除,默认是present替换
  backup 是否在修改文件之前对文件进行备份
  create 文件不存在时,是否要创建文件
  
1.替换httpd.conf文件中, ^Listen   为  Linsten 8080;
ansible webservers -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^Listen" line="Listen 8080"'
2.给主机增加一个网关;
ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth1 line="GATEWAY=172.16.1.200"'
3.删除主机的网关;
ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth1 regexp="^GATEWAY" state=absent'
4.给主机增加一个网关,但需要增加到ONBOOT下面;
ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth1 insertafter="ONBOOT=yes" line="GATEWAY=172.16.1.200"'
5.给主机增加一个网关,但需要增加到ONBOOT上面;
ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth1 insertbefore="ONBOOT=yes" line="test=172.16.1.200"'

举报

相关推荐

0 条评论