自动化运维ansible常用模块用法

目录

前言

ansible默认提供了很多模块来供我们使用。
在Linux中:
ansible-doc -s 模块名    #查看ansible模块用法参数等帮助
ansible-doc -l  		#列出此版本的ansible支持哪些模块

常用模块

ping模块
- name: Try to  connect to host, verify a usable python and return 'pong' if successful.
#检查控制节点和指定的被控制节点机器是否能连通,如果能连通就返回"pong"

[ansible@master ansible]$ ansible test-hosts -m ping 
192.168.1.109 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
setup模块
- name: Gathers facts about remote hosts
#主要用于获取主机信息:架构信息,IP,时间,域名,网卡,MAC,主机名,CPU等信息。
#在playbooks里经常会用到的一个参数gather_facts就与该模块相关。
#setup模块下经常使用的一个参数是filter参数。

#查看指定的被控制节点机器内存信息
[ansible@master ansible]$ ansible test-hosts -m setup -a "filter=ansible_*_mb"
192.168.1.109 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 203, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 5026, 
                "used": 818
            }, 
            "real": {
                "free": 203, 
                "total": 5844, 
                "used": 5641
            }, 
            "swap": {
                "cached": 0, 
                "free": 4063, 
                "total": 4063, 
                "used": 0
            }
        }, 
        "ansible_memtotal_mb": 5844, 
        "ansible_swapfree_mb": 4063, 
        "ansible_swaptotal_mb": 4063
    }, 
    "changed": false
}

#查看指定的被控制节点机器内存信息并存到本地/tmp/facts/目录
[ansible@master ansible]$ ansible test-hosts -m setup -a "filter=ansible_*_mb" --tree /tmp/facts/
[ansible@master ansible]$ ll /tmp/facts/
总用量 4
-rw-r--r-- 1 root root 330 3月  27 14:23 192.168.1.109
[ansible@master ansible]$ cat /tmp/facts/192.168.1.109
command模块
  • chdir:在运行命令之前,切换到此目录。
- name: Executes a commandon the remote nodes.
#在指定被控制节点上运行指定命令

[ansible@master ansible]$ ansible test-hosts -m command -a 'hostname'
192.168.1.109 | SUCCESS | rc=0 >>
slave1
[ansible@master ansible]$ ansible test-hosts -m command -a 'date'
192.168.1.109 | SUCCESS | rc=0 >>
Tue Mar 26 16:36:15 CST 2019
shell模块

shell 和command 区别:
shell模块是通过shell程序执行的, 默认是由/bin/sh执行的, <, >, |, ;, & 都可用。但这样有潜在的 shell 注入风险,后面再详谈。
command 模块不通过shell程序执行,不能调用shell指令,没有bash的环境变量,也不能使用shell的一些操作,在遇到"<",">","|","&"将会终止。
所以,command 模块更安全,因为他不受用户环境的影响。 也很大的避免了潜在的 shell 注入风险。

  • chdir:在运行命令之前,切换到此目录。
  • executable:更改用于执行命令的shell(bash,sh)。 应该是可执行文件的绝对路径。
- name: Execute commands or scripts on the remote nodes.
#在指定被控制节点上运行指定命令或者控制节点的脚本。

- name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards together but bash does)
  shell: cat < /tmp/*txt
  args:
  		executable: /bin/bash

raw模块

执行原始的命令,而不是通过模块子系统。在任何情况下,使用shell或命令模块是合适的。给定原始的参数直接通过配置的远程shell运行。可返回标准输出、错误输出和返回代码。此模块没有变更处理程序支持。
这个模块不需要远程系统上的Python,就像脚本模块一样。此模块也支持Windows目标。

- name: Executes a low-down and dirty SSH command

[ansible@master ansible]$ ansible test-hosts -m raw -a 'hostname|tee'
192.168.1.109 | SUCCESS | rc=0 >>
slave1
copy模块

copy 模块的作用就是拷贝文件,它与之前介绍过的 fetch 模块类似,不过,fetch 模块是从远程主机中拉取文件到 ansible 管理主机,而 copy 模块是将 ansible 管理主机上的文件拷贝到远程主机中。

  • src:用于指定需要copy的文件或目录。
  • dest:用于指定文件将被拷贝到远程主机的哪个目录中,dest为必须参数。
  • content:当不使用src指定拷贝的文件时,可以使用content直接指定文件内容,src与content两个参数必有其一,否则会报错。
  • force: 当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否强制覆盖,可选值有yes和no,默认值为yes,表示复盖,如果设置为no,则不会执行覆盖拷贝操作,远程主机中的文件保持不变。
  • backup: 当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否对远程主机的文件进行备份,可选值有yes和no,当设置为yes时,会先备份远程主机中的文件,然后再将ansible主机中的文件拷贝到远程主机。
  • owner: 指定文件拷贝到远程主机后的属主,但是远程主机上必须有对应的用户,否则会报错。
  • group: 指定文件拷贝到远程主机后的属组,但是远程主机上必须有对应的组,否则会报错。
  • mode: 指定文件拷贝到远程主机后的权限,如果你想将权限设置为”rw-r–r--“,则可以使用mode=0644表示,如果你想要在user对应的权限位上添加执行权限,则可以使用mode=u+x表示。
- name: Copies local files to remote locations
#把主控端指定目录下的xxx文件拷贝到到指定节点机器上

[ansible@master ansible]$ pwd
/etc/ansible
[ansible@master ansible]$ ll
总用量 36
-rw-r--r-- 1 root root 19557 3月  14 11:35 ansible.cfg
-rw-r--r-- 1 root root  1211 3月  26 16:44 hosts
drwxr-xr-x 2 root root  4096 2月   9 01:50 roles
-rw-r--r-- 1 ansible ansible    32 3月  26 17:04 test.log
drwxr-xr-x 2 root root  4096 3月  22 17:01 yml_dir

[ansible@master ansible]$ ansible test-hosts  -m copy -a 'src=/etc/ansible/test.log  dest=//var/lib/ansible/'
192.168.1.109 | SUCCESS => {
    "changed": true, 
    "checksum": "c5ffc2801ba0cf17c3438fd0960a93597033f834", 
    "dest": "/var/lib/ansible/test.log", 
    "gid": 0, 
    "group": "ansible", 
    "md5sum": "2186f9799db682df586599249ecd8002", 
    "mode": "0644", 
    "owner": "ansible", 
    "secontext": "unconfined_u:object_r:user_home_dir_t:s0", 
    "size": 32, 
    "src": "/etc/ansible/test.log", 
    "state": "file", 
    "uid": 0
}

fetch模块

fetch 模块是从远程主机中拉取文件到 ansible 管理主机。

  • src:远程系统上要获取的文件。 这必须是一个文件,而不是一个目录。 后续版本可能会支持递归提取。
  • dest:保存文件的目录。 例如,如果dest目录是/backup,在主机host.example.com上命名为/etc/profile的src文件将被保存到/backup/host.example.com/etc/profile。
  • flat:允许您覆盖将目标文件添加到主机名/ path / to / file的默认行为。
# 将文件存储到/tmp/fetched/host.example.com/tmp/somefile中
- fetch:
    src: /tmp/somefile
    dest: /tmp/fetched

# 直接指定路径
- fetch:
    src: /tmp/somefile
    dest: /tmp/prefix-{{ inventory_hostname }}
    flat: yes

# 指定目标路径
- fetch:
    src: /tmp/uniquefile
    dest: /tmp/special/
    flat: yes
file模块

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

  • force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
  • group:定义文件/目录的属组
  • mode:定义文件/目录的权限
  • owner:定义文件/目录的属主
  • path:必选项,定义文件/目录的路径
  • recurse:递归的设置文件的属性,只对目录有效
  • src:要被链接的源文件的路径,只应用于state=link的情况
  • dest:被链接到的路径,只应用于state=link的情况
  • state:
    ?? directory:如果目录不存在,创建目录
    ?? file:即使文件不存在,也不会被创建
    ?? link:创建软链接
    ?? hard:创建硬链接
    ?? touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
    ?? absent:删除目录、文件或者取消链接文件
- name: Creates or Deletes and Sets attributes of files

#在指定节点上创建目录并更改目录的各属性。
[ansible@master ansible]$ ansible test-hosts  -m file -a "dest=/var/lib/ansible/dir1/  mode=755 owner=ansible group=ansible state=directory"

#在指定节点上删除目录。
[ansible@master ansible]$ ansible test-hosts  -m file -a "dest=/var/lib/ansible/dir1/  state=absent"

#在指定节点上创建文件并更改文件的各属性。
[ansible@master ansible]$ ansible test-hosts  -m file -a "dest=/var/lib/ansible/test_touch.log  mode=755 owner=ansible group=ansible state=touch"

#在指定节点上删除文件。
[ansible@master ansible]$ ansible test-hosts  -m file -a "dest=/var/lib/ansible/test_touch.log  state=absent"

group模块
  • gid:指定用的gid;
  • name:指定用户组名称;
  • state:是创建还是删除。(present表创建;absent表删除)
  • system:如果是yes,则表示创建的组是系统组。
- name: Add or remove groups

#在指定被控制节点上创建一个组
[ansible@master ansible]$ ansible test-hosts -m command -a "tail -5 /etc/group"
192.168.1.109 | SUCCESS | rc=0 >>
zabbix:x:201:
redis:x:492:
mailnull:x:47:
smmsp:x:51:
nagios:x:501:
#[ansible@master ansible]$  ansible test-hosts -m command -a "groupadd newgroup"
#192.168.1.109 | SUCCESS | rc=0 >>
[ansible@master ansible]$  ansible test-hosts -m group -a "name=newgroup"
192.168.1.109 | SUCCESS | rc=0 >>
redis:x:492:
mailnull:x:47:
smmsp:x:51:
nagios:x:501:
newgroup:x:502:

[ansible@master ansible]$ ansible test-hosts -m command -a "tail -5 /etc/group"
192.168.1.109 | SUCCESS | rc=0 >>
redis:x:492:
mailnull:x:47:
smmsp:x:51:
nagios:x:501:
newgroup:x:502: 《==



#在指定被控制节点上删除一个组
#[ansible@master ansible]$  ansible test-hosts -m command -a "groupdel newgroup"
#192.168.1.109 | SUCCESS | rc=0 >>
[ansible@master ansible]$  ansible test-hosts -m group -a "name=newgroup state=absent"
192.168.1.109 | SUCCESS => {
    "changed": true, 
    "name": "newgroup", 
    "state": "absent"
}

[ansible@master ansible]$ ansible test-hosts -m command -a "tail -5 /etc/group"
192.168.1.109 | SUCCESS | rc=0 >>
zabbix:x:201:
redis:x:492:
mailnull:x:47:
smmsp:x:51:
nagios:x:501:
user模块
  • name:指定用户名;
  • password:设定用户密码,password参数需要接受md5加密后的值;
  • state:用户状态,默认为present(present:表示添加用户; absent:表示删除用户);
  • update_password:修改用户密码;
  • always:新密码和旧密码不同时进行修改;
  • on_create:为新创建的用户指定密码;
  • createhome:创建家目录(yes:默认项,即创建用户默认是有家目录的; no:创建用户时不创建家目录);
  • remove:(yes:删除用户家目录,需要指定此参数 no:默认项,删除用户时默认不删除用户的家目录);
  • system:(yes:默认创建为普通用户,而非系统用户);
  • 如果不指定默认生成的选项有:home:创建家目录; shell:创建默认的shell为/bin/bash; system:默认创建为普通用户,而非系统用户,指定是用yes;
- name: Manage user accounts

#在指定被控制节点上创建一个用户。
[ansible@master ansible]$ ansible test-hosts -m command -a "id yuki"
192.168.1.109 | FAILED | rc=1 >>
id: yuki:无此用户non-zero return code
[ansible@master ansible]$ echo "123456" |openssl passwd -1 -stdin
$1$Ww2727.S$ObFL4sV4vdUfS9jR5GK2e1
[ansible@master ansible]$ ansible test-hosts -m user -a "name=yuki password=$1$Ww2727.S$ObFL4sV4vdUfS9jR5GK2e1"
192.168.1.109 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 503, 
    "home": "/home/yuki", 
    "name": "yuki", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 503
}
[ansible@master ansible]$ ansible test-hosts -m command -a "id yuki"
192.168.1.109 | SUCCESS | rc=0 >>
uid=503(yuki) gid=503(yuki) 组=503(yuki)

#在指定被控制节点上更改一个用户的属性。
[ansible@master ansible]$ ansible test-hosts -m user -a "name=yuki update_password=always password=$1$N08JURA8$2hzp7nhUzpQUKrAr7Ja/h0"
192.168.1.109 | SUCCESS => {
    "append": false, 
    "changed": true, 
    "comment": "", 
    "group": 503, 
    "home": "/home/yuki", 
    "move_home": false, 
    "name": "yuki", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 503
}

#在指定被控制节点上删除一个用户。
[ansible@master ansible]$ ansible test-hosts -m user -a "name=yuki remove=yes  state=absent"
192.168.1.109 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "yuki", 
    "remove": true, 
    "state": "absent"
}
[ansible@master ansible]$  ansible test-hosts -m command -a "id yuki"
192.168.1.109 | FAILED | rc=1 >>
id: yuki:无此用户non-zero return code
yum模块
  • config_file:yum的配置文件;
  • disable_gpg_check:关闭gpg_check;
  • disablerepo:不启用某个源;
  • enablerepo:启用某个源;
  • name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径;
  • state:状态(present:默认的,表示为安装;lastest:安装为最新的版本;absent:表示删除)
- name: Manages packages with the `yum' package manager

#安装最新版本的apache
[ansible@master ansible]$ ansible test-hosts -m yum -a "name=httpd state=latest"

#移除apache
[ansible@master ansible]$ ansible test-hosts -m yum -a "name=httpd state=absent"

#安装指定版本
[ansible@master ansible]$ ansible test-hosts -m yum -a "name=httpd-2.2.29-1.4.amzn1 state=present"

#安装整个Development tools相关的软件包
[ansible@master ansible]$ ansible test-hosts -m yum -a "name='@Development tools' state=present"

#从一个远程yum仓库安装nginx
[ansible@master ansible]$ ansible test-hosts -m yum -a "name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present"
service模块
  • arguments:给命令行提供一些选项,aliases: args;
  • enabled:是否开机启动 yes,否则是no;
  • name: 必须填写,服务名称;
  • pattern:定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行。
  • runlevel:运行级别;
  • sleep:如果执行了restarted,在则stop和start之间沉睡几秒钟;
  • state:对当前服务执行(started启动,stopped停止,restarted重启,reloaded重新加载等操作);
  • use:服务模块实际上使用系统特定的模块,通常是自动检测,这个设置可以强制一个特定的模块。通常情况下,使用ansible_service_mgr 的值,并在找不到匹配的情况下推到旧的服务模块;
- name: Manage services

#启动nginx服务,没有运行则启动
[ansible@master ansible]$ ansible test-hosts -m service -a "name=nginx state=started enabled=yes"

#停止nginx服务
[ansible@master ansible]$ ansible test-hosts -m service -a "name=nginx state=stopped"

#重启nginx 服务
[ansible@master ansible]$ ansible test-hosts -m service -a "name=nginx state=restarted"

#重新加载
[ansible@master ansible]$ ansible test-hosts -m service -a "name=nginx state=reloaded"

#启动服务foo
[ansible@master ansible]$ ansible test-hosts -m service -a "name=foo pattern=/usr/bin/foo state=started"

#重启eth0 网卡
[ansible@master ansible]$ ansible test-hosts -m service -a "name=network  state=restarted args=eth0"
script模块
- name: Runs a local script on a remote node after transferring it

#在指定的被控制节点上执行控制节点上的脚本
[ansible@master ansible]$ ansible t3 -m script -a "/home/script/test.sh"
cron模块
  • state:(present:创建任务; absent:删除任务);
  • backup:对远程主机上的原任务计划内容修改之前做备份;
  • job:要执行的任务;
  • name:该任务的描述(必须项);
  • user:以哪个用户的身份运行;
  • minute:分钟(0-59),默认为*;
  • hour:小时(0-23),默认为*;
  • day:日(1-31),默认为*;
  • month:月(1-12),默认为*;
  • weekday:周(0-7),默认为*;
[ansible@master ansible]$ ansible test-hosts -m cron -a "name='sync time from ntpserver' minute=*/10 job='/usr/sbin/ntpdate 3.cn.pool.ntp.org'"

[ansible@master ansible]$ ansible test-hosts -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'

unarchive模块
  • copy: 在解压文件之前,是否先将文件复制到远程主机,默认为yes,no则要求目标主机上压缩包必须存在;
  • creates:指定一个文件名,当改文件存在时,则解压指令不执行;
  • dest: 远程主机上的一个路径,即文件解压的路径;
  • src:如果copy为yes ,则需要指定压缩文件的源路径;
  • group:解压后的目录或文件的属组;
  • mode: 解压后文件的权限;
  • owner: 解压后文件或目录的属主;
  • list_files: yes,则会列出压缩包里的文件,默认为no;
- name: Unpacks an archive after (optionally) copying it from the local machine.

#解压控制节点机器上的指定文件到指定的被控制节点机器上的指定路径下   
[ansible@master ansible]$  ansible test-hosts -m unarchive -a "src=foo.tgz  dest=/var/lib/foo"

#在指定的被控制节点机器上解压指定文件到指定路径下   
[ansible@master ansible]$  ansible test-hosts -m unarchive -a "src=/tmp/foo.zip  dest=/usr/local/bin remote_src: yes"


#在指定的被控制节点机器上解压需要先下载的文件到指定路径下 (added in 2.0version)  
[ansible@master ansible]$  ansible test-hosts -m unarchive -a "src=https://example.com/example.zip  dest=/usr/local/bin remote_src: yes"
get_url模块

该模块主要用于从http、ftp、https服务器上下载文件(类似于wget),主要有如下参数:

  • sha256sum:下载完成后进行sha256 check;
  • timeout:下载超时时间,默认10s;
  • url:下载的URL;
  • url_password、url_username:主要用于需要用户名密码进行验证的情况;
  • dest:将文件下载到哪里的绝对路径。如果dest是目录,则使用服务器提供的文件名,或者如果没有提供,将使用远程服务器上的URL的基本名称。
  • headers:以格式“key:value,key:value”为请求添加自定义HTTP标头;
- name: Downloads files from HTTP, HTTPS, or FTP to nodes

[ansible@master ansible]$ ansible test-hosts -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'

synchronize模块
  • archive: 归档,相当于同时开启recursive(递归)、links、perms、times、owner、group、-D选项都为yes,默认该项为开启;
  • checksum: 跳过检测sum值,默认关闭;
  • compress:是否开启压缩;
  • copy_links:复制链接文件,默认为no ,注意后面还有一个links参数;
  • delete: 删除不存在的文件,默认no;
  • src: 源路径;
  • dest:目的路径;
  • dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议;
  • dirs:传速目录不进行递归,默认为no,即进行目录递归;
  • rsync_opts:rsync参数部分;
  • set_remote_user:主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不同的情况;
  • mode: push或pull 模块,push模的话,一般用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件;
 - name: A wrapper around rsync to make common tasks in your playbooks quick and easy.
 #使用rsync同步文件
 
[ansible@master ansible]$ ansible test-hosts  -m synchronize -a   "src=/tmp/nginx-1.13.3 dest=/tmp/ rsync_opts='--exclude=configure,--exclude=LICENSE'"

[ansible@master ansible]$ ansible test-hosts  -m synchronize -a   "src=/tmp/nginx-1.13.3 dest=/tmp/  checksum=yes times=no links=yes  rsync_opts='--exclude=configure,--exclude=LICENSE'"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章