Ansible(三)——模块

所有模块可参考ansible-doc介绍 Index of all Modules — Ansible Documentation

ansible-doc 模块名 可查看某个模块的帮助文档

以下测试基于主控 Ubuntu2004、ansible 2.9.6

创建测试项目的目录,ansible.cfg,hosts

[root@wenzi ~]#mkdir /data
[root@wenzi ~]#cd /data/
[root@wenzi data]#cp /etc/ansible/ansible.cfg /data/
[root@wenzi data]#cp /etc/ansible/hosts /data/
[root@wenzi data]#vim ansible.cfg
inventory      = /data/hosts
host_key_checking = False
log_path = /var/log/ansible.log
...

[root@wenzi data]#cat hosts
[web]
192.168.28.30
192.168.28.31

[client]
192.168.28.40
192.168.28.41

测试前实现主控和被控之间基于秘钥的验证。

Command模块

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

注意:此模块不具有幂等性;不支持 $VARNAME < > | ; & 等,可用shell模块实现

常见选项

chdir=dir        #执行命令前,先切换至目录dir
creates=file     #当file不存在时才会执行
removes=file     #当file存在时才会执行
[root@wenzi data]#ansible 192.168.28.30 -a "chdir=/etc cat os-release"
192.168.28.30 | CHANGED | rc=0 >>
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
...

Shell模块

功能:和command相似,用shell执行命令,支持各种符号,比如:*,$, > , 相当于增强版的command模块。

注意:此模块不具有幂等性,不建议使用,推荐使用各功能的专用模块

常见选项

chdir=dir      #执行命令前,先切换至目录dir
creates=file   #当file不存在时才会执行
removes=file   #当file存在时才会执行
[root@wenzi data]#ansible web -m shell -a "echo $HOSTNAME"

shell模块默认使用/bin/sh,若要改为/bin/bash,如下

    - name: check jdk env
      shell: |
        source /etc/profile
        java -version
      args:
        executable: /bin/bash
      register: jdk_res
      ignore_errors: yes

Script模块

功能:在远程主机上运行ansible服务器上的脚本(无需执行权限)

注意:此模块不具有幂等性

常见选项

chdir=dir        #执行命令前,先切换至目录dir
cmd              #指定ansible主机的命令
creates=file     #当file不存在时才会执行
removes=file     #当file存在时才会执行
[root@wenzi data]#cat cs.sh
#!/bin/bash
#********************************************************************
#FileName:     cs.sh
#Version:      1.0
#Date:         2024-02-07
#Author:       wenzi
#Description: This is description
#********************************************************************
echo "This is ansible master"

[root@wenzi data]#ansible 192.168.28.40 -m script -a "/data/cs.sh"
192.168.28.40 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.28.40 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.28.40 closed."
    ],
    "stdout": "This is ansible master\r\n",
    "stdout_lines": [
        "This is ansible master"
    ]
}

Copy模块

功能:复制ansible主控端的文件到远程主机

注意:src=file 如果是没指明路径,则为当前目录或当前目录下的files目录下的file文件。

常见选项

src            #控制端的源文件路径
dest           #被控端的文件路径
owner          #属主
group          #属组
mode           #权限
content        #指定dest的文件内容
backup         #是否备份
validate       #验证成功才会执行copy
remote_src     #no是默认值,表示src文件在ansible主机,yes表示src文件在远程主机
如目标存在,默认覆盖,此处指定覆盖前先备份
ansible webservers -m copy -a "src=/root/test1.sh dest=/tmp/test2.sh owner=wang mode=600 backup=yes"

指定内容,直接生成目标文件    
ansible webservers -m copy -a "content='123' dest=/etc/rsync.pas owner=root group=root mode=0600"

复制/etc目录自身,注意/etc/后面没有/
ansible webservers -m copy -a "src=/etc dest=/backup"

复制/etc/下的文件,不包括/etc/目录自身,注意/etc/后面有/
ansible webservers -m copy -a "src=/etc/ dest=/backup"

Get_url模块

功能:将文件从http、https或ftp下载到被管理机节点上

常见选项

url       #下载文件的URL,支持HTTP,HTTPS或FTP协议
dest      #下载到目标路径(绝对路径),如果目标是一个目录,就用原文件名,如果目标设置了名称就用目标
设置的名称
owner     #指定属主
group     #指定属组
mode      #指定权限
force     #如果yes,dest不是目录,将每次下载文件,如果内容改变则替换文件。如果no,则只有在目标不存
在时才会下载,默认no
checksum  #对目标文件在下载后计算摘要,以确保其完整性
          #示例: checksum="sha256:D98291AC[...]B6DC7B97",
                 checksum="sha256:http://example.com/path/sha256sum.txt"
url_username     #用于HTTP基本认证的用户名。对于允许空密码的站点,此参数可以不使用`url_password'
url_password     #用于HTTP基本认证的密码。如果未指定`url_username'参数,则不会使用url_password'参数
validate_certs   #如果“no”,SSL证书将不会被验证。适用于自签名证书在私有网站上使用
timeout   #URL请求的超时时间,秒为单位
[root@wenzi ~]#ansible web -m get_url -a 'url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/usr/local/src/'

Fetch 模块

功能:从被控端提取文件至ansible的主控端

常见选项

src      #被控制端的源文件路径,只支持文件
dest     #ansible控制端的目录路径
[root@wenzi ~]#ansible web -m fetch -a 'src=/etc/os-release dest=/data/os'
[root@wenzi ~]#tree /data/
/data/
├── ansible.cfg
├── hosts
└── os
    ├── 192.168.28.30
    │   └── etc
    │       └── os-release
    ├── 192.168.28.31
    │   └── etc
    │       └── os-release
    ├── 192.168.28.40
    │   └── etc
    │       └── os-release
    └── 192.168.28.41
        └── etc
            └── os-release

9 directories, 6 files

File模块

功能:设置文件属性,创建文件,目录和软链接等

常见选项

path		#在被控端创建的路径
src			#源路径,创建软连接时用
dest		#目标路径,创建软连接时用
owner		#属主
group		#属组
mode		#权限
state		#状态 
	=touch    	#创建文件  
	=directory  #创建目录
	=link		#软链接
	=hard     	#硬链接
	=absent		#文件、目录将被删除;链接文件将被删除,源文件不受影响。
recurse     	#默认为no;若为yes表示递归授权
创建文件
[root@wenzi ~]#ansible web -m file -a 'path=/root/test.txt state=touch'

创建目录
[root@wenzi ~]#ansible web -m file -a 'path=/root/bbb state=directory owner=mysql group=mysql'

创建软链接
[root@wenzi ~]#ansible web -m file -a 'src=/root/test.txt dest=/root/test_link state=link'

删除文件
[root@wenzi ~]#ansible web -m file -a 'path=/root/test.txt state=absent'

递归修改目录属性,但不递归至子目录
[root@wenzi ~]#ansible web -m file -a "path=/data/mysql state=directory owner=mysql group=mysql"

递归修改目录及子目录的属性
[root@wenzi ~]#ansible web -m file -a "path=/data/mysql state=directory owner=mysql group=mysql recurse=yes"

Stat模块

功能:检查文件或文件系统的状态

注意:对于Windows目标,请改用win_stat模块

常见选项

path #文件/对象的完整路径(必须)

常用的返回值判断

使用 register 将模块的结果保存到变量中后,可以访问以下返回值:
    exists: 文件或目录是否存在。
    isdir: 如果目标是一个目录,则为 True。
    isfile: 如果目标是一个普通文件,则为 True。
    islnk: 如果目标是一个符号链接,则为 True。
[root@wenzi ~]#ansible 127.0.0.1 -m stat -a 'path=/etc/passwd'
127.0.0.1 | SUCCESS => {
    "changed": false,
    "stat": {
        "atime": 1707379390.1438441,
        "attr_flags": "e",
        "attributes": [
            "extents"
        ],
        "block_size": 4096,
        "blocks": 8,
        "charset": "us-ascii",
        "checksum": "e39ffcf24172e2ba3a002ce7ab78cef3e9f59544",
        "ctime": 1706881710.4931915,
        "dev": 64768,
        "device_type": 0,
        "executable": false,
        "exists": true,
        "gid": 0,
        "gr_name": "root",
        "inode": 925168,
        "isblk": false,
        "ischr": false,
        "isdir": false,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": true,
        "issock": false,
        "isuid": false,
        "mimetype": "text/plain",
        "mode": "0644",
        "mtime": 1706881710.4931915,
        "nlink": 1,
        "path": "/etc/passwd",
        "pw_name": "root",
        "readable": true,
        "rgrp": true,
        "roth": true,
        "rusr": true,
        "size": 2045,
        "uid": 0,
        "version": "1602992175",
        "wgrp": false,
        "woth": false,
        "writeable": true,
        "wusr": true,
        "xgrp": false,
        "xoth": false,
        "xusr": false
    }
}

playbook如下形式
- name: 检查文件是否存在
  stat:
    path: /etc/hosts
  register: hosts_file

- name: 文件存在时输出消息
  debug:
    msg: "文件存在"
  when: hosts_file.stat.exists

- name: 文件不存在时输出消息
  debug:
    msg: "文件不存在"
  when: not hosts_file.stat.exists

Fail模块

功能:使任务失败,并可以提供一个自定义的错误消息。通常和when一起使用

常见选项

msg: (必需) 你想要显示的错误信息字符串。
- name: 任务名称
  fail:
    msg: "你的自定义错误信息"
  when: 条件表达式

一、希望某个任务总是失败,可省略when
- name: 无条件使任务失败
  fail:
    msg: "这个任务总是会失败"
    
二、有条件失败
- name: 无条件使任务失败
  fail:
    msg: "这个任务总是会失败"
    
三、结合 stat 模块,我们可以创建一个任务来检查文件是否存在,并在文件不存在时使任务失败
- name: 检查关键文件是否存在
  stat:
    path: /path/to/critical_file
  register: critical_file_stat

- name: 如果文件不存在则停止执行
  fail:
    msg: "关键文件 /path/to/critical_file 不存在,停止执行"
  when: not critical_file_stat.stat.exists

四、根据命令的输出结果来决定是否失败,如服务是否运行
- name: 检查服务状态
  shell: systemctl is-active --quiet httpd
  register: httpd_status
  ignore_errors: yes  # 忽略错误,允许继续执行后续任务

- name: 如果服务未运行则失败
  fail:
    msg: "HTTPD服务没有运行"
  when: httpd_status.rc != 0

Unarchive模块

功能:解包解压缩

常见选项

remote_src		#yes表示源文件在远程被控主机 或 第三方主机上;no表示文件在ansible主控上,默认值为no, 此选项代替copy选项
copy			#默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上,如果设置为copy=no,会在远程主机上寻找src源文件,此选项已废弃
src				#源路径。ansible主控上的路径 或 远程被控主机的路径 或 第三方的路径,如果是远程主机上的路径,则需要设置remote_src=yes
dest			#远程主机上的目标路径
owner			#默认递归
group			#默认递归
mode			#设置解压缩后的文件权限,默认递归
creates=/path/file		#当绝对路径/path/file不存在时才会执行
从本机解压至web
[root@wenzi ~]#ansible web -m unarchive -a 'src=/data/foo.tgz dest=/var/lib/foo owner=wang group=bin'

从远程主机解压至web
[root@wenzi ~]#ansible web -m unarchive -a 'src=https://nginx.org/download/nginx-1.20.2.tar.gz dest=/data remote_src=yes owner=wang group=wang'

Archive模块

功能:打包压缩保存在被管理节点

常见选项

path   #压缩的文件或目录
dest   #压缩后的文件
format #压缩格式,支持gz,bz2,xz,tar,zip
owner  #属主
group  #属组
mode   #权限
[root@wenzi ~]#ansible web -m archive -a 'path=/var/log/ dest=/data/log.tar.bz2 format=bz2 owner=wang mode=0600'

Hostname模块

功能:管理主机名

常见选项

name  #修改后的主机名称
[root@wenzi ~]#ansible web -m hostname -a 'name=web'

Cron模块

功能:计划任务

支持时间:minute,hour,day,month,weekday

常见选项

name	#描述,必写,禁用、启用某个计划任务使用name区分
minute  #分钟
hour 	#小时
weekday #周
user	#任务由哪个用户运行;默认root
job		#任务
state	#默认present,可选absent
创建计划任务
[root@wenzi ~]#ansible web -m cron -a "name=test minute=*/5 job='ls'"

禁用计划任务
[root@wenzi ~]#ansible web -m cron -a "name=test minute=*/5 job='ls' disabled=yes"

启用计划任务
[root@wenzi ~]#ansible web -m cron -a "name=test minute=*/5 job='ls' disabled=no"

删除计划任务
[root@wenzi ~]#ansible web -m cron -a "name=test state=absent"

Yum模块

功能:管理软件包

只支持RHEL系列

常见选项

name	#软件包名称,多个软件包用逗号,隔开
state	#状态  
	=present	#安装,此为默认值
	=absent		#删除
 	=latest		#最新版
list    #列出指定包
enablerepo		#启用哪个仓库安装
disablerepo		#不使用哪些仓库的包
exclude			#排除指定的包
validate        #是否检验,默认为yes
列出httpd软件包
[root@wenzi ~]#ansible web -m yum -a 'list=httpd'

安装httpd
[root@wenzi ~]#ansible web -m yum -a 'name=httpd state=present'

升级除除kernel和foo开头以外的所有包
[root@wenzi ~]#ansible webservers -m yum -a 'name=* state=lastest exclude=kernel*,foo*'

删除
[root@wenzi ~]#ansible web -m yum -a 'name=httpd state=absent'

Yum_repository模块

功能:yum的仓库配置管理

常见选项

name		#仓库id,对应配置文件中的[xxx]
description #仓库描述名称,对应配置文件中的name=
baseurl		#仓库的地址
gpgcheck	#是否开启验证,yes 或 no
gpgkey      #仓库公钥路径
state		#默认present,可选absent
enabled		#若为yes,表示启用;若为no,表示禁用
[root@wenzi ~]#ansible web -m yum_repository -a 'name=zabbix description="zabbix repo" baseurl="https://mirrors.aliyun.com/zabbix/zabbix/6.0/rhel/$releasever/$basearch" gpgcheck=no enabled=yes'

Service模块

功能:管理服务

常见选项

name	#服务名称
state	#服务状态
	=started	#启动
	=stopped	#停止
	=restarted	#重启
	=reloaded	#重载
enabled		#开启自启动
daemon_reload   #加载新的配置文件,适用于systemd模块
[root@wenzi ~]#ansible web -m service -a 'name=httpd state=started enabled=yes'

User模块

功能:管理用户

常见选项

name	#创建的名称
uid		#指定uid
group	#指定基本组
shell	#登录shell类型默认/bin/bash
create_home		#是否创建家目录,默认会创建家目录,no不创建
home			#指定家目录,可配合create_home=no使用
password		#设定对应的密码,必须是加密后的字符串才行,否则不生效
system			#yes表示系统用户
groups          #附加组
append			#追加附加组使用,yes表示增加新的附加组
state   		#absen删除
remove			#yes表示删除用户时将家目录一起删除
generate_ssh_key #是否创建私钥,yes 或 no
ssh_keyu_bits    #私钥位数
ssh_key_file     #私钥文件路径
[root@wenzi ~]#ansible web -m user -a 'name=user1 comment="test user" uid=2000  group=root create_home=no home=/data/user1'
[root@wenzi ~]#ansible web -m user -a 'name=user1 state=absent remove=yes'

Group模块

功能:管理组

常见选项

name	#指定组名称
gid     #指定gid
state   #默认present,可选absent
system  #是否是系统组,默认no
[root@wenzi ~]#ansible web -m group -a 'name=nginx gid=88 system=yes'
[root@wenzi ~]#ansible web -m group -a 'name=nginx state=absent'

Lineinfile模块

功能:相当于sed,主要用于修改某一行的文件内容

常见选项

path	#被控端文件的路径
regexp	#可选字段,正则匹配语法格式,表示被替换的内容
line	#必要字段,替换为的内容,
state	#absent表示删除
insertbefore    #正则表达式,插入到匹配内容前面,如和regexp同时存在,只在没找到与regexp匹配时才使用insertbefore
insertafter     #正则表达式,插入到匹配内容后面,如和regexp同时存在,只在没找到与regexp匹配时才使用insertafter
backrefs        #支持后面引用,yes和no
backup          #修改前先备份
create          #如果文件不存在,则创建,默认不存在会出错
mode            #指定权限
owner           #指定用户
group           #指定组

#注意
regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被
匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,这么这些行都会被删除。
修改httpd监听端口
[root@wenzi ~]#ansible web -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 8080'"

删除网关
[root@wenzi ~]#ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 regexp="^GATEWAY" state=absent'

添加网关
[root@wenzi ~]#ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 line="GATEWAY=1.1.1.1"'

在NAME=xxx行下面新增网关
[root@wenzi ~]#aansible webservers  -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfgeth0 insertafter="^NAME=" line="GATEWAY=2.2.2.2"'

删除/etc/fstab中注释行
[root@wenzi ~]#aansible all -m lineinfile  -a 'dest=/etc/fstab state=absent regexp="^#"'

Replace模块

功能: 多行修改替换

常见选项

path	#被控端文件的路径
regexp	#正则匹配语法格式,表示被替换的内容
replace #替换为的内容
after           #插入到替换内容前面,
before          #插入到替换内容后面
backup          #修改前先备份
mode            #指定权限
owner           #指定用户
group           #指定组
注释UUID开头的行。此处使用了(.*)分组,\1表示匹配到的第一个组
[root@wenzi ~]#ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#\1'"  

取消注释UUID开头的行。此处使用了(.*)分组,\1表示匹配到的第一个组
[root@wenzi ~]#ansible all -m replace -a "path=/etc/fstab regexp='^#(UUID.*)' replace='\1'"

SELinux模块

功能: 该模块管理 SELInux 策略

常见选项

policy     #指定SELINUXTYPE=targeted
state      #指定SELinux模式。disabled 或 permissive 或 enforcing
[root@wenzi ~]#ansible web -m selinux -a 'state=disabled'

Reboot模块

功能: 重启

常见选项

msg               #重启提示
pre_reboot_delay  #重启前延迟时间的秒数
post_reboot_delay #重启后延迟时间的秒数后,再验证系统正常启动
reboot_timeout    #重启后延迟时间再执行测试成功与否的命令
test_command      #执行测试成功与否的命令
[root@wenzi ~]#ansible webservers -m reboot -a 'msg="host will be reboot"'

Mount模块

功能:挂载和卸载文件系统

常见选项

src    #源设备路径,或网络地址
path   #挂载至本地哪个路径下
fstype #设备类型,ext4/xfs/nfs
opts   #挂载的选项,defaults/ro/rw
state  #挂载还是卸载
  =present #只更新/etc/fstab 文件,但不执行mount命令
  =unmounted #不更新/etc/fstab文件,只执行unmount命令
  =absent #删除/etc/fstab文件中条目,删除挂载点,执行unmount命令
  =mounted #更新/etc/fstab文件,创建挂载点, 执行mount命令
永久挂载,并立即生效
[root@wenzi ~]#ansible webservers -m mount -a 'src=10.0.0.8:/data/wordpress path=/var/www/html/wp-content/uploads opts="_netdev" state=mounted'

永久卸载,并立即生效
[root@wenzi ~]#ansible webservers -m mount -a 'src=10.0.0.8:/data/wordpress path=/var/www/html/wp-content/uploads fstype=nfs state=absent'

Setup模块

功能:setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用

常见选项

filter #指定过滤条件

Debug模块

功能:此模块可以用于输出信息,并且通过 msg 定制输出的信息内容

常见选项

msg       #指定命令输出的信息
var       #指定变量名,和msg互斥
verbosity #详细度
默认情况
[root@wenzi ~]#ansible web -m debug
192.168.28.30 | SUCCESS => {
    "msg": "Hello world!"
}
192.168.28.31 | SUCCESS => {
    "msg": "Hello world!"
}

Sysctl模块

功能:修改内核参数

常见选项

name  #内核参数
value #指定值
state #是否保存在sysctl.conf文件中,默认present
sysctl_set #使用sysctl -w 验证值生效
[root@wenzi ~]#ansible webservers -m sysctl -a 'name=net.ipv4.ip_forward value=1 state=present'

Pam_limits模块

功能::管理资源限制

[root@wenzi ~]#ansible web -m pam_limits -a "domain=joe limit_type=soft limit_item=nofile value=64000"

current_container_facts

模块意义:返回有关模块是否在 Docker 容器中运行的事实。
常用参数:无

- name: Get facts about the container
  current_container_facts:

docker_compose

模块意义:通过 docker-compose 管理多个容器。
常用参数:
   project_name: 项目名称 (默认: 当前目录名)
   path: Compose 文件路径 (默认: 当前目录)
   file: Compose 文件名 (默认: docker-compose.yml)
   command: 执行的命令
   service_names: 服务名称列表
        
- name: Start services using docker-compose
  docker_compose:
    project_name: my_project
    path: /path/to/docker-compose.yml
    command: up -d
    service_names:
      - web
      - db

docker_config

模块意义:管理 Docker 配置。
常用参数:
   config_path: 配置文件路径
   data: 配置内容
   state: 状态 (present, absent)

- name: Create a new configuration file
  docker_config:
    config_path: /etc/docker/daemon.json
    data: |
      {
        "registry-mirrors": ["https://mirror.gcr.io"]
      }
    state: present

docker_container

模块意义:管理 Docker 容器。
常用参数:
    name: 容器名称
    image: 镜像名称
    command: 运行容器时执行的命令
    ports: 端口映射
    volumes: 卷挂载
    state: 状态 (present, started, stopped, absent)

- name: Start a container with port mapping and volume mount
  docker_container:
    name: my_container
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - /var/www/html:/usr/share/nginx/html
    state: started

docker_container_exec

模块意义:执行 docker exec 命令。
常用参数:
    name: 容器名称
    command: 在容器内执行的命令
    detach: 是否分离模式 (默认: False)
    interactive: 是否交互模式 (默认: False)

- name: Execute a command in an existing container
  docker_container_exec:
    name: my_container
    command: echo "Hello from inside the container!"

docker_container_info

模块意义:获取 Docker 容器的信息。
常用参数:
    name: 容器名称
    filter: 过滤器 (默认: None)

- name: Retrieve information about a specific container
  docker_container_info:
    name: my_container
  register: container_info

docker_host_info

模块意义:获取 Docker 主机和服务对象列表的信息。
常用参数:
    filter: 过滤器 (默认: None)

- name: Retrieve information about the Docker host and its objects
  docker_host_info:
  register: host_info

docker_image

模块意义:管理 Docker 容器镜像。
常用参数:
    name: 镜像名称
    repository: 镜像仓库
    tag: 镜像标签 (默认: latest)
    pull_policy: 拉取策略 (always, missing, never)
    state: 状态 (present, absent)

- name: Pull an image from a registry
  docker_image:
    name: nginx
    tag: latest
    pull_policy: always

docker_image_info

模块意义:检查 Docker 镜像。
常用参数:
    name: 镜像名称
    repository: 镜像仓库
    tag: 镜像标签 (默认: latest)

- name: Inspect a Docker image
  docker_image_info:
    name: nginx
    tag: latest
  register: image_info

docker_image_load

模块意义:从归档文件加载 Docker 镜像。
常用参数:
    image: 镜像名称
    src: 归档文件路径

- name: Load a Docker image from an archive
  docker_image_load:
    src: /path/to/image.tar.gz

docker_login

模块意义:登录到 Docker 注册表。
常用参数:
    username: 用户名
    password: 密码
    email: 邮件地址
    server_url: 注册表 URL (默认: https://index.docker.io/v1/)

- name: Login to a Docker registry
  docker_login:
    username: my_username
    password: my_password
    email: [email protected]

docker_network

模块意义:管理 Docker 网络。
常用参数:
    name: 网络名称
    driver: 网络驱动程序 (默认: bridge)
    state: 状态 (present, absent)

- name: Create a new network
  docker_network:
    name: my_network
    driver: bridge
    state: present

docker_network_info

模块意义:获取 Docker 网络信息。
常用参数:
    name: 网络名称
    filter: 过滤器 (默认: None)

- name: Retrieve information about a specific network
  docker_network_info:
    name: my_network
  register: network_info

docker_plugin

模块意义:管理 Docker 插件。
常用参数:
    name: 插件名称
    enabled: 是否启用插件 (默认: True)
    state: 状态 (present, absent)

- name: Install a plugin
  docker_plugin:
    name: vieux/ssh-agent
    enabled: yes
    state: present

docker_prune

模块意义:清理各种 Docker 对象。
常用参数:
    containers: 清理容器 (默认: no)
    images: 清理未使用的镜像 (默认: no)
    volumes: 清理卷 (默认: no)
    networks: 清理网络 (默认: no)
    force: 强制清理 (默认: no)

- name: Prune unused containers, images, and networks
  docker_prune:
    containers: yes
    images: yes
    networks: yes

你可能感兴趣的:(#,Ansible,运维工具,ansible,运维,linux)