[root@node1 ansible]# ansible nodes -m user -a "name=wadeson state=absent"
cron模块:
ansible-doc -s cron
[root@node1 ansible]# ansible all -m cron -a 'name="sync time" minute=2 user=root state=present job="/usr/sbin/ntpdate time.nist.gov &> /dev/null"'
192.168.223.146 | FAILED! => {
"changed": false,
"failed": true,
"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
}
解决办法:在节点主机上安装libselinux-python
yum -y install libselinux-python
再次在ansible服务器上执行:
[root@node1 ansible]# ansible all -m cron -a 'name="sync time" minute=*/2 user=root state=present job="/usr/sbin/ntpdate time.nist.gov &> /dev/null"'
192.168.223.146 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"sync time"
]
}
在节点上查看:
[root@wadeson ~]# crontab -l
#Ansible: sync time
2 * * * * /usr/sbin/ntpdate time.nist.gov &> /dev/null
删除cron任务:
ansible all -m cron -a 'name="sync time" state=absent‘
copy模块:
ansible-doc -s copy
ansible all -m copy -a "src=/etc/fstab dest=/tmp/fatab.bak mode=600"
节点上查看:
[root@wadeson ~]# ll /tmp/
总用量 8
-rw-------. 1 root root 899 7月 30 21:55 fatab.bak
[root@node1 ansible]# cat test_var.yaml
- hosts: nodes
remote_user: root
tasks:
- name: create a new file
copy: content={{ node_var }} dest=/tmp/ansible_var.txtshell
file模块:
ansible-doc -s file,建立文件,目录,连接文件
ansible all -m file -a 'path=/tmp/testdir state=directory'
ansible all -m file -a 'src=/tmp/fstab.bak dest=/root/fstab.bak state=link'
[root@wadeson ~]# ll
总用量 16
-rw-------. 1 root root 1104 7月 30 19:54 anaconda-ks.cfg
lrwxrwxrwx. 1 root root 14 7月 30 22:03 fstab.bak -> /tmp/fstab.bak
state:file|absent|directory|link|hard|touch
在远程节点上建立一个新文件:
ansible nodes -m file -a 'path=/tmp/ansible_agent.txt
state=touch'
path这里能够为dest或者name
yum模块:
ansible-doc -s yum
ansible all -m yum -a 'name="vim,wget" state=latest'
state:present|latest|absent
ansible all -m yum -a 'name=ntpdate state=latest'
按照ntpdate包,而后定义cron任务
service模块:
参数:
enabled:设置是否开机启动
state:started|stopped|restarted
ansible all -m yum -a 'name=httpd state=latest'
ansible all -m service -a 'name=httpd state=started enabled=no'
节点查看:
[root@wadeson ~]# rpm -qa httpd
httpd-2.2.15-60.el6.centos.4.x86_64
[root@wadeson ~]# chkconfig --list|grep httpd
httpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
shell模块:
-a “command”,没有额外的参数
命令中带有管道,变量等等
[root@node1 ~]# ansible nodes -m shell -a 'echo "redhat"|passwd --stdin testuser1'
192.168.223.146 | SUCCESS | rc=0 >>
更改用户 testuser1 的密码 。
passwd: 全部的身份验证令牌已经成功更新。
而command模块:
[root@node1 ~]# ansible nodes -m command -a 'echo "redhat"|passwd --stdin testuser1'
192.168.223.146 | SUCCESS | rc=0 >>
redhat|passwd --stdin testuser1
能够看出并无修改用户的密码,command模块只能执行简单的命令
script模块:
一、将脚本传到node上
二、并在node上执行该脚本
ansible服务器:
[root@node1 ~]# cat echo.sh
#!/bin/bash
echo "ansible is better" > /tmp/echo.text
ansible all -m script -a "/root/echo.sh"
节点查看:
[root@wadeson ~]# ll /tmp/
总用量 16
-rw-r--r--. 1 root root 18 7月 30 14:46 echo.text
setup模块:查看facts
收集node上的系统信息,能够看成变量进行调用
ansible all -m setup