ansible常用模块

常用模块帮助文档参考:

https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html

Command 模块

功能:在远程主机执行命令,此为默认模块,可忽略-m选项

注意:此命令不支持 $VARNAME < > | ; & 等,用shell模块实现

command模块使用详解:

chdir: 	 	执行命令前,切换到目录
creates:	当该文件存在时,不执行该步骤
executable:	切换shell来执行命令,需要使用命令的绝对路径
free_from:	需要执行的脚本,一般使用Ansible的-a参数代替。
removes:	当该文件不存在时,不执行该步骤
warn:		如果在ansible.cfg中存在告警,如果设定了false,不会告警该行

范例:

[root@ansible ~]# ansible all -m command -a 'chdir=/etc cat redhat-release'
192.168.18.222 | CHANGED | rc=0 >>
CentOS Linux release 7.4.1708 (Core) 
192.168.18.132 | CHANGED | rc=0 >>
CentOS Linux release 8.4.2105
192.168.18.130 | CHANGED | rc=0 >>
CentOS Linux release 8.4.2105

[root@ansible ~]# ansible all -m command -a 'hostname'
192.168.18.222 | CHANGED | rc=0 >>
Quhz.localdomain
192.168.18.132 | CHANGED | rc=0 >>
slave2
192.168.18.130 | CHANGED | rc=0 >>
slave1

 
Shell模块

功能: 在远程主机上运行命令,和command相似,用shell执行命令
    尤其是用到管道变量等功能的复杂命令
    # ansible all -m shell -a 'echo 123456 | passwd --stdin test'

范例:

[root@ansible ~]#ansible all -m shell -a 'echo 123456 | passwd --stdin test'
 

范例:将shell模块代替command,设为模块

[root@ansible ~]#vim /etc/ansible/ansible.cfg
#修改下面一行
module_name = shell
 
Script模块

功能:在远程主机上运行ansible服务器上的脚本

范例:

ansible all  -m script -a /data/test.sh
Copy模块

功能:从ansible服务器主控端复制文件到远程主机

#如目标存在,默认覆盖,此处指定先备份
[root@ansible ~]# ansible all -m copy -a 'src=/root/anaconda-ks.cfg dest=/tmp/anaconda-ks.cfg owner=quhz mode=600 backup=yes'
#指定内容,直接生成目标文件    
[root@ansible ~]# ansible all -m copy -a "content='this is test file ' dest=/tmp/test.txt"
 
Fetch模块

功能:从远程主机提取文件至ansible的主控端,copy相反,目前不支持目录

范例:

[root@ansible ~]# ansible all -m fetch -a 'src=/etc/hostname dest=/root/test/'
[root@ansible ~]# tree test/
test/
├── 192.168.18.130
│   └── etc
│       └── hostname
├── 192.168.18.132
│   └── etc
│       └── hostname
└── 192.168.18.222
    └── etc
        └── hostname

6 directories, 3 files

File模块

功能:

主要用于远程主机上的文件操作,file模块包含如下选项:

force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no

group:定义文件/目录的属组

mode:定义文件/目录的权限

owner:定义文件/目录的属主

path:必选项,定义文件/目录的路径

recurse:递归的设置文件的属性,只对目录有效

src:要被链接的源文件的路径,只应用于state=link的情况

dest:被链接到的路径,只应用于state=link的情况

state: directory:如果目录不存在,创建目录

file:即使文件不存在,也不会被创建

link:创建软链接

hard:创建硬链接

touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间

absent:删除目录、文件或者取消链接文件

 

范例:

#创建空文件
[root@ansible ~]# ansible all -m file -a 'path=/tmp/test1.txt state=touch'
#删除文件
[root@ansible ~]# ansible all -m file -a 'path=/tmp/test1.txt state=absent'
#设置权限
[root@ansible ~]# ansible all -m file -a 'path=/tmp/test.txt owner=bin mode=600'
#创建目录
[root@ansible ~]# ansible all -m file -a 'path=/root/test state=directory'
#创建软链接
[root@ansible ~]# ansible all -m file -a 'src=/tmp/test.txt dest=/root/test-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:设置解压缩后的文件权限

范例:

ansible all -m unarchive -a 'src=/data/foo.tgz dest=/var/lib/foo'
ansible all -m unarchive -a 'src=/tmp/foo.zip dest=/data copy=no mode=0777'
ansible all -m unarchive -a 'src=https://example.com/example.zip dest=/data copy=no'
 
Archive模块

功能:打包压缩

范例:

ansible websrvs -m archive  -a 'path=/var/log/ dest=/data/log.tar.bz2 format=bz2  owner=wang mode=0600'
 
3.4.9 Hostname模块

功能:管理主机名

范例:

ansible 192.168.18.130 -m hostname -a 'name=node1'
 
Cron模块

功能:计划任务
支持时间:minute,hour,day,month,weekday

范例:

#备份数据库脚本
[root@centos8 ~]#cat mysql_backup.sh 
mysqldump -A -F --single-transaction --master-data=2 -q -uroot |gzip > /data/mysql_date +%F_%T.sql.gz
#创建任务
ansible 10.0.0.8 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh'
ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime"
#禁用计划任务
ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime disabled=yes"
#启用计划任务
ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime disabled=no"
#删除任务
ansible websrvs -m cron -a "name='backup mysql' state=absent"
ansible websrvs -m cron -a 'state=absent name=Synctime'
 
Yum模块

功能:管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本

范例:

ansible websrvs -m yum -a  'name=httpd state=present'  #安装
ansible websrvs -m yum -a  'name=httpd state=absent' #删除
 
Service模块

功能:管理服务

范例:

ansible all -m service -a 'name=httpd state=started enabled=yes'
ansible all -m service -a 'name=httpd state=stopped'
ansible all -m service -a 'name=httpd state=reloaded’
ansible all -m shell -a "sed -i 's/^Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf"
ansible all -m service -a 'name=httpd state=restarted' 
 
User模块

功能:管理用户

范例:

#创建用户
ansible all -m user -a 'name=user1 comment=“test user” uid=2048 home=/app/user1 group=root'

ansible all -m user -a 'name=nginx comment=nginx uid=88 group=nginx groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nginx non_unique=yes'

删除用户及家目录等数据

ansible all -m user -a 'name=nginx state=absent remove=yes'

 
Group模块

功能:管理组

范例:

#创建组
ansible websrvs -m group  -a 'name=nginx gid=88 system=yes'
#删除组
ansible websrvs -m group  -a 'name=nginx state=absent'
 
Lineinfile模块

ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,存在问题,无法正常进行替换 。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可以方便的进行替换

功能:相当于sed,可以修改文件内容

范例:

ansible all -m   lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=enforcing'"
ansible all -m lineinfile  -a 'dest=/etc/fstab state=absent regexp="^#"'
 
Replace模块

该模块有点类似于sed命令,主要也是基于正则进行匹配和替换

范例:

ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#\1'"  
ansible all -m replace -a "path=/etc/fstab regexp='^#(.*)' replace='\1'"
 
Setup模块

功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机较多,会影响执行速度,可以使用gather_facts: no来禁止 Ansible 收集 facts 信息

范例:

ansible all -m setup
ansible all -m setup -a "filter=ansible_nodename"
ansible all -m setup -a "filter=ansible_hostname"
ansible all -m setup -a "filter=ansible_domain"
ansible all -m setup -a "filter=ansible_memtotal_mb"
ansible all -m setup -a "filter=ansible_memory_mb"
ansible all -m setup -a "filter=ansible_memfree_mb"
ansible all -m setup -a "filter=ansible_os_family"
ansible all -m setup -a "filter=ansible_distribution_major_version"
ansible all -m setup -a "filter=ansible_distribution_version"
ansible all -m setup -a "filter=ansible_processor_vcpus"
ansible all -m setup -a "filter=ansible_all_ipv4_addresses"
ansible all -m setup -a "filter=ansible_architecture"
ansible all -m  setup  -a "filter=ansible_processor*"
 

范例:

[root@ansible ~]# ansible all  -m  setup -a 'filter=ansible_python_version'
192.168.18.222 | SUCCESS => {
    "ansible_facts": {
        "ansible_python_version": "3.7.0",
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
192.168.18.130 | SUCCESS => {
    "ansible_facts": {
        "ansible_python_version": "3.6.8",
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
192.168.18.132 | SUCCESS => {
    "ansible_facts": {
        "ansible_python_version": "3.6.8",
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}

 

posted @ 2021-07-17 22:14  屈宏志  阅读(66)  评论(0编辑  收藏  举报