Ansible 常用模块
Ping 模块 (测试主控端和远程主机是否能连接成功)
[root@localhost ~]# ansible all -m ping #all代表所有command 在远程主机上指定指定的命令 不能使用特殊的符号:| > >>
参数:chdir 切换目录
creates 当指定文件存在时,命令不执行 当指定文件不存在时,命令执行
removes 当指定文件存在时,命令指定 当指定文件不存在时,命令不执行
[root@localhost ~]# ansible all -m command -a "chdir=/home ls"
[root@localhost ~]# ansible all -m command -a "creates=/etc/fstab ls /home"
[root@localhost ~]# ansible all -m command -a "creates=/etc/fstabsss ls /home"
shell 在远程主机上执行命令 可以用特殊的字符 万能模块
[root@localhost ~]# ansible all -m shell -a "ls / | grep home"user 管理或者创建远程主机上面的用户
参数:name:指定用户名 如果用户不存在 则创建该用户
password 给用户添加密码
[root@localhost ~]# ansible all -m user -a "name=test"
[root@localhost ~]# ansible all -m shell -a "tail -1 /etc/passwd"
[root@localhost ~]# yum -y install openssl-devel
[root@localhost ~]# openssl passwd -1 123.com -1 md5的加密方式
[root@localhost~]#ansible all -m user -a 'name=test password=Ys0jWK$US5Waaudi'
group 创建和管理远程主机上的组
参数:name 指定组 如果不存在则创建
gid 修改指定的gid
state=absent 删除指定的组 如果这个组是用户的基本组 则不能删除 如果不是则可以删除
[root@localhost ~]# ansible all -m group -a "name=one state=absent" [root@localhost ~]# ansible all -m shell -a "tail -1 /etc/group"
script 在远程主机上执行主控端的脚本
参数:chdir 切换目录
creates 文件存在 脚本不执行
removes 文件存在 脚本执行
[root@localhost ~]# ansible all -m script -a "chdir=/root test.sh"
[root@localhost ~]# ansible all -m script -a "chdir=/root creates=/etc/fstab test.sh"
[root@localhost ~]# ansible all -m script -a "chdir=/root removes=/etc/fstab test.sh"
copy模块 将主控端的文件复制到远程主机上 主要针对文件 也可以复制目录
参数:src 要复制的文件的路径(源文件)
dest 将文件复制到的目标主机的位置
content 将指定的内容写入到远程主机的文件中 这个是覆盖
owner :指定文件的属主
group :指定文件的属组
mode:指定文件的权限
copy(没有任何参数的情况下) 当主控端拷贝的文件和目标主机一致,但内容不一致 则强制覆盖 默认force=yes
force=no 当主控端拷贝的文件和目标主机一致,但内容不一致 放弃拷贝
backup=yes 当主控端拷贝的文件和目标主机一致,但内容不一致 放弃拷贝 则对目标文件进行备份不覆盖
[root@localhost ~]# ansible all -m copy -a "src=/root/hehehe dest=/home"
[root@localhost ~]# ansible all -m shell -a "ls /home"
yum模块 在远程主机上使用yum安装软件 远程主机上要配置好yum
参数:name指定安装包的名称
state:installed 安装软件包
removed 卸载软件包
disable_gpg_check 取消密钥认证
[root@localhost ~]# ansible all -m yum -a "name=httpd state=installed"
[root@localhost ~]# ansible all -m yum -a "name=httpd state=removed"
service:管理远程主机上的服务
参数:name 服务名
state:started 开启 restarted 重启 reloaded 重新加载 stopped 停止
enabled=yes 开启自启
ansible all -m shell -a "systemctl is-enabled httpd" 查看是否在开机自启当中
ansible all -m service -a "name=httpd state=started"
ansible all -m shell -a "netstat -anput | grep httpd" 查看当前进程的信息 -a所有-n数字形式 -ppid -u udp -t tcp
ansible all -m service -a "name=httpd state=stopped"
ansible all -m shell -a "netstat -anput | grep httpd"
ansible all -m service -a "name=httpd state=restarted"
ansible all -m shell -a "netstat -anput | grep httpd"
ansible all -m service -a "name=httpd state=reloaded"
ansible all -m shell -a "netstat -anput | grep httpd"
ansible all -m service -a "name=httpd enabled=yes"
ansible all -m shell -a "systemctl is-enabled httpd" 查看是否在开机自启当中
setup模块 查看远程主机上面的信息
[root@localhost ~]# ansible all -m setupfile 创建或者管理远程主机上的文件或者目录
参数:path/dest 指定的文件 如果远程主机上没有该文件 则创建
state 创建类型 touch文件 directory 目录 link软链接 hard硬链接
state=absent 删除文件或者目录
ansible all -m file -a 'path=/home/heihei state=touch'
ansible all -m file -a 'path=/home/qq state=directory'
ansible all -m shell -a "ls /home"
软链接和硬链接 src(源文件) 要写绝对路径
ansible all -m file -a "state=link src=/home/qq path=/home/ee"
ansible all -m shell -a "ls /home"
ansible all -m shell -a "ls -l /home/ee"
ansible all -m file -a "state=hard src=/home/heihei path=/home/tttt"
ansible all -m shell -a "ls -l /home/tttt"
cron 在远程主机上添加计划任务
参数:minute 分钟
hour 小时
day 天
mouth 月
weekday 周
job 执行的命令
name 对计划任务的命名
special_time=hourly
[root@localhost ~]# ansible all -m cron -a "name=one hour=8 minute=10 job='echo xixi'"
[root@localhost ~]# ansible all -m shell -a "crontab -l" #列出当前的计划任务
[root@localhost ~]# ansible all -m shell -a "crontab -r" #删除当前计划任务
Lineinfile
参数:regexp 正则匹配 ^.... ....$
line 将匹配到的内容进行替换
如果直接使用line 是在文件的最后添加内容
insetberfore 在文件匹配到的前面天剑
insertafter 在匹配到的后面添加
在实验中两次添加的内容不能一致 否则会添加不进去
[root@localhost ~]# ansible all -m file -a "name=/oo state=touch"
ansible all -m file -a "name=/oo state=touch"
ansible all -m shell -a "echo -e '11111\n222222\n333333' > /oo "
ansible all -m shell -a "cat /oo"
**单独使用line 最后一行添加**
[root@localhost ~]# ansible all -m lineinfile -a "line='4444' path=/oo"
[root@localhost ~]# ansible all -m shell -a "cat /oo"
**替换**
[root@localhost ~]# ansible all -m lineinfile -a "regexp='^2' line='55555' path=/oo"
[root@localhost ~]# ansible all -m shell -a "cat /oo"
**前面添加**
ansible all -m lineinfile -a "insertbefore='^3' line='6666' path=/oo"
ansible all -m shell -a "cat /oo"
**后面添加**
ansible all -m lineinfile -a "insertafter='^4' line='77777' path=/oo"
ansible all -m shell -a "cat /oo"
                    
                
                
            
        
浙公网安备 33010602011771号