Ansible常用模块
Command 模块
功能:在远程主机执行命令,此为默认模块,可忽略-m选项
注意:此命令不支持 $VARNAME < > | ; & 等,用shell模块实现
范例 :
chdir参数表示执行命令之前,会先进入到指定的目录中,然后执行 输出 hello world
[root@ansible-1 etc]# ansible webservers -a 'chdir=/etc echo hello world' 192.168.10.203 | CHANGED | rc=0 >> hello world 192.168.10.202 | CHANGED | rc=0 >> hello world
creates参数表示,如果 /data/f1.txt 这个文件存在,就不执行后面的打印输出命令,如果不存在,才执行后面的命令。(10.203 存在 /data/f1.txt 文件,10.202不存在这个文件)
remove参数表示,如果 /data/f1.txt 这个文件不存在,就不执行后面的命令,如果文件存在,才执行后面的命令。
[root@ansible-1 etc]# ansible webservers -m command -a 'chdir=/etc creates=/data/f1.txt echo 111' 192.168.10.203 | SUCCESS | rc=0 >> skipped, since /data/f1.txt exists 192.168.10.202 | CHANGED | rc=0 >> 111 [root@ansible-1 etc]# ansible webservers -m command -a 'chdir=/etc removes=/data/f1.txt echo 111' 192.168.10.203 | CHANGED | rc=0 >> 111 192.168.10.202 | SUCCESS | rc=0 >> skipped, since /data/f1.txt does not exist
以下命令需要用 shell模块执行,默认的command模块不行,
ansible websrvs -m command -a 'service vsftpd start' ansible websrvs -m command -a 'echo 123456 |passwd --stdin meng' ansible websrvs -m command -a 'rm -rf /data/' ansible websrvs -m command -a 'echo hello > /data/1.log' ansible websrvs -m command -a "echo $HOSTNAME"
shell模块
功能:和command相似,用shell执行命令
范例:
[root@ansible-1 etc]# ansible webservers -m shell -a 'echo $HOSTNAME' 192.168.10.203 | CHANGED | rc=0 >> ansible-3 192.168.10.202 | CHANGED | rc=0 >> ansible-2 You have new mail in /var/spool/mail/root [root@ansible-1 etc]# ansible webservers -m shell -a 'echo 123456 |passwd --stdin meng' 192.168.10.203 | CHANGED | rc=0 >> Changing password for user meng. passwd: all authentication tokens updated successfully. 192.168.10.202 | FAILED | rc=252 >> passwd: Unknown user name 'meng'.non-zero return code [root@ansible-1 etc]# ansible webservers -m shell -a 'ls -lh /etc/yum.conf' 192.168.10.202 | CHANGED | rc=0 >> -rw-r--r--. 1 root root 970 Dec 3 2015 /etc/yum.conf 192.168.10.203 | CHANGED | rc=0 >> -rw-r--r--. 1 root root 970 Dec 3 2015 /etc/yum.conf You have new mail in /var/spool/mail/root [root@ansible-1 etc]# ansible webservers -m shell -a 'echo 111 > /data/111.log' 192.168.10.203 | CHANGED | rc=0 >> 192.168.10.202 | CHANGED | rc=0 >> [root@ansible-1 etc]#
注意:调用bash执行命令 类似 cat /tmp/test.md | awk -F'|' '{print $1,$2}' &> /tmp/example.txt 这些
复杂命令,即使使用shell也可能会失败,解决办法:写到脚本时,copy到远程,执行,再把需要的结
果拉回执行命令的机器
范例:将shell模块代替command,设为模块
[root@ansible ~]#vim /etc/ansible/ansible.cfg
#修改下面一行
module_name = shel
Script 模块
功能:在远程主机上运行ansible服务器上的脚本(无需执行权限)
范例 :
[root@ansible-1 ~]# cat sayhello.sh #!/bin/bash echo "$HOSTNAME hello" [root@ansible-1 ~]# ansible webservers -m script -a '/root/sayhello.sh' 192.168.10.203 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.10.203 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.10.203 closed." ], "stdout": "ansible-3 hello\r\n", "stdout_lines": [ "ansible-3 hello" ] } 192.168.10.202 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.10.202 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.10.202 closed." ], "stdout": "ansible-2 hello\r\n", "stdout_lines": [ "ansible-2 hello" ] }
Copy 模块
功能:从ansible服务器主控端复制文件到远程主机
#如目标存在,默认覆盖,此处指定先备份 [root@ansible-1 ~]# ansible webservers -m copy -a "src=/root/sayhello.sh dest=/tmp/sayhello2.sh owner=root mode=777 backup=yes" #复制/etc目录自身,注意/etc/后面没有/ [root@ansible-1 ~]# ansible websrvs -m copy -a "src=/etc dest=/backup" #复制/etc/下的文件,不包括/etc/目录自身,注意/etc/后面有/ [root@ansible-1 ~]# ansible websrvs -m copy -a "src=/etc/ dest=/backup"
Fetch 模块
将所有webservers分组下的被控机 /etc/redhat-release 拉取到主控机下的 /tmp/123目录下
[root@ansible-1 123]# ansible webservers -m fetch -a "src=/etc/redhat-release dest=/tmp/123" [root@ansible-1 123]# tree /tmp/123/ /tmp/123/ ├── 192.168.10.202 │ └── etc │ └── redhat-release └── 192.168.10.203 └── etc └── redhat-release 4 directories, 2 files [root@ansible-1 123]# ll total 0 drwxr-xr-x 3 root root 16 Feb 7 15:55 192.168.10.202 drwxr-xr-x 3 root root 16 Feb 7 15:55 192.168.10.203
File 模块
#创建名称为mysql的文件夹 [root@ansible-1 log]# ansible webservers -m file -a 'path=/data/mysql state=directory owner=mysql group=mysql' #创建文件 [root@ansible-1 log]# ansible webservers -m file -a 'path=/data/123.txt state=touch' #递归删除文件。链接被取消 [root@ansible-1 log]# ansible webservers -m file -a 'path=/data/123.txt state=absent'
#创建软链接,将/data/aomo 文件链接到 /data/testfile-link 下,
[root@ansible-1 log]# ansible webservers -m file -a 'src=/data/aomo dest=/data/testfile-link state=link'
unarchive 模块
功能:解包解压缩
实现有两种用法:
1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes
2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no
常见参数:
copy:默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上,如果设置为
copy=no,会在远程主机上寻找src源文件
remote_src:和copy功能一样且互斥,yes表示在远程主机,不在ansible主机,no表示文件在
ansible主机上
src:源路径,可以是ansible主机上的路径,也可以是远程主机(被管理端或者第三方主机)上的路
径,如果是远程主机上的路径,则需要设置copy=no
dest:远程主机上的目标路径
mode:设置解压缩后的文件权限
范例:
#将192.168.10.202 上的 /data/bak.tgz 压缩包 拷贝到 202的 /root 目录下并解压。 [root@ansible-1 log]# ansible '192.168.10.202' -m unarchive -a 'src=/data/bak.tgz dest=/root owner=root group=root copy=no'
#将主机上 /tmp/music.zip 压缩包拷贝到远程主机202上的 root目录下解压。默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上。
[root@ansible-1 tmp]# ansible '192.168.10.202' -m unarchive -a 'src=/tmp/music.zip dest=/root owner=root group=root'
Archive 模块
功能:打包压缩保存在被管理节点
范例:
#将远程机202 上的 /var/log/ 下的所有文件 打包放到 /data下,压缩包格式 gz [root@ansible-1 tmp]# ansible '192.168.10.202' -m archive -a 'path=/var/log/ dest=/data/log.tar.gz format=gz owner=root group=root'
Hostname 模块
功能:管理主机名
范例:
[root@ansible-1 tmp]# ansible node1 -m hostname -a "name=APP" [root@ansible-1 tmp]# ansible 192.168.10.203 -m hostname -a 'name=node2.123.com'
Cron 模块
功能:计划任务
支持时间:minute,hour,day,month,weekday
范例
#创建任务 [root@ansible-1 ~]# ansible webservers -m cron -a 'hour=1 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh' [root@ansible-1 ~]# ansible webservers -m cron -a 'hour=1 minute=30 weekday=1-5 name="test" job="echo hello"'
[root@ansible-1 ~]# ansible no-ntptime-key -m cron -a "minute=*/10 job='nohup /usr/sbin/ntpdate 172.30.1.80 >/dev/null 2>&1 &' name='synctime'" >> /root/no-crontab-key202111151115.log
#被控机器上查看已经生成定时任务 [root@ansible-3 aomo]# crontab -l */5 * * * * ntpdate time3.aliyun.com && hwclock -w #Ansible: backup mysql 30 1 * * 1-5 /root/mysql_backup.sh #Ansible: test 30 1 * * 1-5 echo hello #禁用计划任务 [root@ansible-1 ~]# ansible webservers -m cron -a 'hour=1 minute=30 weekday=1-5 name="test" job="echo hello" disabled=yes' #启用计划任务 [root@ansible-1 ~]# ansible webservers -m cron -a 'hour=1 minute=30 weekday=1-5 name="test" job="echo hello" disabled=no' #删除计划任务 [root@ansible-1 ~]# ansible webservers -m cron -a 'name="backup mysql" state=absent' [root@ansible-1 ~]# ansible webservers -m cron -a 'state=absent name=test'
Yum 模块
功能:管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本
范例:
#安装 [root@ansible-1 ~]# ansible webservers -m yum -a 'name=httpd state=present' #删除 [root@ansible-1 ~]# ansible webservers -m yum -a 'name=httpd state=absent' #一次安装多个 [root@ansible-1 ~]# ansible webservers -m yum -a 'name=iotop,cowsay'
Service 模块
功能:管理服务
范例:
[root@ansible-1 ~]# ansible webservers -m service -a 'name=httpd state=started enabled=yes' [root@ansible-1 ~]# ansible webservers -m service -a 'name=httpd state=stopped' [root@ansible-1 ~]# ansible webservers -m service -a 'name=httpd state=reloaded' [root@ansible-1 ~]# ansible webservers -m shell -a "sed -i 's/^Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf" [root@ansible-1 ~]# ansible webservers -m service -a 'name=httpd state=restarted'
User 模块
功能:管理用户
范例:
#创建用户 [root@ansible-1 ~]# ansible webservers -m user -a 'name=user1 comment="test user" uid=2048 home=/app/user1 group=root' [root@ansible-1 ~]# ansible webservers -m user -a 'name=nginx comment="nginx" uid=88 group=nginx groups="root,daemon" shell=/sbi create_home=no home=/data/nginx non_unique=yes' #remove=yes表示删除用户及家目录等数据,默认remove=no [root@ansible-1 ~]# ansible webservers -m user -a 'name=nginx state=absent remove=yes'
Group 模块
功能:管理组
范例
#创建组 [root@ansible-1 ~]# ansible webservers -m group -a 'name=nginx gid=88 system=yes' #删除组 [root@ansible-1 ~]# ansible webservers -m group -a 'name=nginx state=absent'
Lineinfile 模块
ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,
存在问题,无法正常进行替换 。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可
以方便的进行替换
功能:相当于sed,可以修改文件内容
范例
[root@ansible-1 ~]# ansible webservers -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 80'" [root@ansible-1 ~]# ansible webservers -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'" [root@ansible-1 ~]# ansible webservers -m lineinfile -a "dest=/etc/fstab state=absent regexp="^#""
root@ansible-master:/etc/ansible/playbook# ansible 192.168.20.11 -m lineinfile -a "path=/etc/ssh/sshd_config regexp='^#Port' line='Port 9689'"
[root@ansible-1 ~]#ansible "~cac-web(09|05|03|04|06|08)" -m lineinfile -a "path=/etc/pam.d/system-auth regexp='password requisite pam_cracklib.so try_first_pass retry=3 type=' line='password requisite pam_cracklib.so try_first_pass retry=3 type=A+b+4+! difok=3 minlen=8 dcredit=-1 ucredit=-1 lcredit=-1 ocredit=-1'"
root@ansible-master:/etc/pam.d# ansible 192.168.20.10 -m lineinfile -a "path=/etc/login.defs regexp='PASS_MAX_DAYS.*99999' line='PASS_MAX_DAYS 90'"
Replace 模块
该模块有点类似于sed命令,主要也是基于正则进行匹配和替换,建议使用
范例
[root@ansible-1 ~]# ansible webservers -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#\1'" [root@ansible-1 ~]# ansible webservers -m replace -a "path=/etc/fstab regexp='^#(.*)' replace='\1'" 效果如下:先注销掉,再放开注释 [root@ansible-3 sbin]# cat /etc/fstab /dev/mapper/centos-root / xfs defaults 0 0 #UUID=c9c8192e-c943-4b1f-aa57-2deb0c30913b /boot xfs defaults 0 0 You have new mail in /var/spool/mail/root [root@ansible-3 sbin]# cat /etc/fstab /dev/mapper/centos-root / xfs defaults 0 0 UUID=c9c8192e-c943-4b1f-aa57-2deb0c30913b /boot xfs defaults 0 0 [root@ansible-3 sbin]#
Setup 模块
功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机
较多,会影响执行速度,可以使用 gather_facts: no 来禁止 Ansible 收集 facts 信息
范例:
[root@ansible-1 ~]# ansible all -m setup [root@ansible-1 ~]# ansible all -m setup -a "filter=ansible_nodename" [root@ansible-1 ~]# ansible all -m setup -a "filter=ansible_hostname" [root@ansible-1 ~]# ansible all -m setup -a "filter=ansible_domain" [root@ansible-1 ~]# ansible all -m setup -a "filter=ansible_memtotal_mb" [root@ansible-1 ~]# ansible all -m setup -a "filter=ansible_memory_mb" [root@ansible-1 ~]# ansible all -m setup -a "filter=ansible_memfree_mb" [root@ansible-1 ~]# ansible all -m setup -a "filter=ansible_os_family" [root@ansible-1 ~]# ansible all -m setup -a "filter=ansible_distribution_major_version" [root@ansible-1 ~]# ansible all -m setup -a "filter=ansible_distribution_version" [root@ansible-1 ~]# ansible all -m setup -a "filter=ansible_processor_vcpus" [root@ansible-1 ~]# ansible all -m setup -a "filter=ansible_all_ipv4_addresses"