vim /etc/ansible/ansible.cfg
取消注释 使用shell模块
module_name = shell
[root@ansible ~]# ansible-doc -s copy
#content可以新建文件内容 并发送到目标主机
# 执行命令
[root@ansible ~]# ansible all -m copy -a "content='hello\nthanks\n' dest=/root/f2"
#在目标主机查看
[root@c7-48 ~]# cat f2
hello
thanks
[root@ansible ~]# ansible-doc -s fetch
#只能抓取单个文件
[root@ansible ~]# ls /data
[root@ansible ~]# ansible all -m fetch -a 'src=/var/log/messages dest=/data'
[root@ansible ~]# ls /data/
10.0.0.44 10.0.0.48 10.0.0.49
[root@ansible ~]# tree /data/
/data/
├── 10.0.0.44
│ └── var
│ └── log
│ └── messages
├── 10.0.0.48
│ └── var
│ └── log
│ └── messages
└── 10.0.0.49
└── var
└── log
└── messages
[root@ansible data]# ansible 10.0.0.44 -m shell -a 'tar jcf log.tar.xz /var/log/*.log'
[root@c7-44 ~]# ls /root
log.tar.xz f2
[root@ansible data]# ansible 10.0.0.44 -m fetch -a 'src=/root/log.tar.xz dest=/data'
#查看
[root@ansible ~]# tree /data/
/data/
├── 10.0.0.44
│ ├── root
│ │ └── log.tar.xz #在这
│ └── var
│ └── log
│ └── messages
#验证
[root@ansible ~]# cd /data/10.0.0.44/root/
[root@ansible root]# ls
log.tar.xz
[root@ansible root]# tar tvf log.tar.xz #预览
[root@ansible ~]# ansible-doc -s unarchive
[root@ansible ~]# ansible-doc -s archive
[root@ansible ~]# ansible-doc -s file
name | path|src是等价的
ansible all -m file -a 'name=/data/f3 state=touch'
ansible all -m file -a 'path=/data/f3 state=touch'
[root@ansible ~]# ansible all -m file -a 'name=/data/f3 state=absent'
[root@ansible ~]# ansible all -m file -a 'path=/data/f3 state=absent'
[root@ansible ~]# ansible all -m file -a 'path=/data/dir2 state=directory'
[root@ansible ~]# ansible all -m file -a 'name=/data/dir1 state=directory'
[root@ansible ~]# ansible all -m file -a 'name=/data/dir1 state=absent'
[root@ansible ~]# ansible all -m file -a 'path=/data/dir2 state=absent'
[root@ansible ~]# ansible all -m file -a 'src=/etc/fstab dest=/data/fstab.link state=link'
#查看
[root@ansible ~]# ansible all -a 'ls -l /data'
[root@ansible ~]# ansible all -m file -a 'dest=/data/fstab.link state=absent'
#查看
[root@ansible ~]# ansible all -a 'ls -l /data'
[root@ansible ~]# ansible all -m file -a 'dest=/data/ state=absent'
ps:挂在点的目录无法删除
#查看挂载点
[root@c7-48 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 1500376 0 1500376 0% /dev
tmpfs 1512380 0 1512380 0% /dev/shm
tmpfs 1512380 19616 1492764 2% /run
tmpfs 1512380 0 1512380 0% /sys/fs/cgroup
/dev/mapper/centos-root 28289540 2177492 26112048 8% /
/dev/sda1 1038336 139480 898856 14% /boot
tmpfs 302480 0 302480 0% /run/user/0
挂载点介绍
ansible all -m shell -a "rm -rf /data/*"
[root@ansible ~]# ansible 10.0.0.48 -m hostname -a "name=node1"
已经生效并改名
[root@c7-48 ~]# bash
[root@node1 ~]# ls
anaconda-ks.cfg f1.txt f2
[root@node1 ~]# cat /etc/hostname #centos-7的主机名的配置文件
node1
#centos6的主机名在
/etc/sysconfig/network
缺点:
#没有改 手动更改建议修改
[root@node1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 ==localhost4.localdomain4==
::1 localhost localhost.localdomain localhost6 ==localhost6.localdomain6==
==之间的部分 ==
支持时间: minute,hour,day,month,weekday
*/1 每1分钟
1-5 范围
1,3,6 隔开
#广播位置
[root@ansible ~]# which wall
/usr/bin/wall
[root@ansible ~]# ansible all -m cron -a 'minute=* weekday=1,7,6 job="/usr/bin/wall FBI warning" name=warningcron'
[root@ansible ~]# ansible all -m cron -a 'job="/usr/bin/wall FBI warning" name=warningcron state=absent'
[root@ansible ~]# ansible all -m cron -a 'disabled=true job="/usr/bin/wall FBI warning" name=warningcron'
[root@ansible ~]# ansible all -m cron -a 'disabled=no job="/usr/bin/wall FBI warning" name=warningcron'
[root@node1 ~]# contab -l
bash: contab: command not found
[root@node1 ~]# crontab -l
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
#Ansible: warningcron
#* * * * * /usr/bin/wall FBI warning #已注释
[root@ansible ~]# ansible all -m cron -a 'disabled=false job="/usr/bin/wall FBI warning" name=warningcron'
[root@ansible ~]# ansible all -m cron -a 'disabled=yes job="/usr/bin/wall FBI warning" name=warningcron'
[root@node1 ~]# crontab -l
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
#Ansible: warningcron
* * * * * /usr/bin/wall FBI warning
install (present' or
installed’,`latest’),
remove (absent' or
removed’)
[root@ansible ~]# ansible all -m yum -a 'name=vsftpd'
ansible all -m yum -a 'list=installed'
ansible all -m yum -a 'name=vsftpd state=removed'
#验证
ansible all -m shell -a 'rpm -q vsftpd'
ansible all -m yum -a 'name=vsftpd,memcached state=latest'
ansible all -m yum -a 'name=vsftpd,memcached state=absent'
#先copy到远程主机
[root@ansible data]# ansible 10.0.0.44,10.0.0.48 -m copy -a 'src=/data/jdk-8u131-linux-x64_.rpm dest=/root/'
#然后安装
[root@ansible data]# ansible 10.0.0.44,10.0.0.48 -m yum -a 'name=/root/jdk-8u131-linux-x64_.rpm state=installed'
#禁用gpg检查
[root@ansible data]# ansible 10.0.0.44,10.0.0.48 -m yum -a 'name=/root/jdk-8u131-linux-x64_.rpm state=installed disabled_gpg_check=yes'
#边清楚更新缓存 一边去装包
[root@ansible data]# ansible 10.0.0.44,10.0.0.48 -m yum -a 'name=dstat update_cache=yes'
gpg_check简介
[root@ansible ~]# ansible all -m service -a 'name=nginx state=started enabled=yes'
ansible all-m service -a 'name=httpd state=stopped'
ansible all -m service -a 'name=httpd state=started'
ansible all -m service -a 'name=httpd state=reloaded'
ansible all -m service -a 'name=httpd state=restarted'
#查看nginx账号有没有
getent passwd nginx
id nginx
[root@ansible ~]# ansible all -m user -a 'name=nginx shell=/sbin/nologin system=yes home=/var/system groups=root,bin uid=808 comment="system service"'
#system=yes 表示系统账号
# shell=/sbin/nologin 系统用户的shell类型
#comment 描述
#groups 附加组
[root@ansible ~]# ansible all -m user -a 'name=ylm shell=/sbin/nologin system=yes home=/var/ylm groups=root,bin uid=88 comment="nginx service"'
[root@ansible ~]# ansible all -m shell -a 'getent passwd ylm'
[root@ansible ~]# ansible all -m user -a 'name=ylm state=absent remove=yes'
#removes=yes 是确认删除家目录
[root@ansible ~]# ansible all -m shell -a 'getent passwd ylm'
[root@ansible ~]# ansible all -m group -a 'name=ylm system=yes gid=800'
#验证
[root@ansible ~]# ansible all -a 'getent group ylm'
[root@ansible ~]# ansible all -m group -a 'name=ylm state=absent'