ansible从入门到入土:常用模块简介---->apt模块

26 篇文章 1 订阅
5 篇文章 0 订阅

在ansible中我并没有找到自动判断对端系统包管理器的模块,所以想要在Debian或者Ubuntu等系统安装软件,就需要使用apt模块,下面演示一下基本操作

安装ansible

关于如何安装和使用ansible请看这篇:自动化ansible工具:带你从安装到实现“hello world“

apt 模块使用

1、安装软件(mariadb-server)

命令如下:

ansible hello -m apt -a "name=mariadb-server state=present"

实现效果(删减部分内容):

root@debian:~# ansible hello -m apt -a "name=mariadb-server state=present"
server1 | CHANGED => {
    "cache_update_time": 1612347402,
    "cache_updated": false,
    "changed": true,
    "stderr": "",
    "stderr_lines": [],
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following additional packages will be installed:\n  galera-3 gawk libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl\n  libdbd-mysql-perl libdbi-perl libencode-locale-perl libfcgi-perl\n  libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl\n  libhttp-date-perl libhttp-message-perl libio-html-perl\n  liblwp-mediatypes-perl libmariadb3 libmpfr6 libsigsegv2 libsnappy1v5\n  libterm-readkey-perl libtimedate-perl liburi-perl mariadb-client-10.3\n  mariadb-client-core-10.3 mariadb-common mariadb-server-10.3\n  mariadb-server-core-10.3 mysql-common psmisc rsync socat\nSuggested packages:\n  gawk-doc libclone-perl libmldbm-perl libnet-daemon-perl\n  libsql-statement-perl libdata-dump-perl libipc-sharedcache-perl libwww-perl\n  mailx mariadb-test netcat-openbsd tinyca\nThe following NEW packages will be installed:\n  galera-3 gawk libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl\n  libdbd-mysql-perl libdbi-perl libencode-locale-perl libfcgi-perl\n  libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl\n  libhttp-date-perl libhttp-message-perl libio-html-perl\n  liblwp-mediatypes-perl libmariadb3 libmpfr6 libsigsegv2 libsnappy1v5\n  libterm-readkey-perl libtimedate-perl liburi-perl mariadb-client-10.3\n  mariadb-client-core-10.3 mariadb-common mariadb-server mariadb-server-10.3\n  mariadb-server-core-10.3 mysql-common psmisc rsync socat\n0 upgraded, 33 newly installed, 0 to remove and 7 not upgraded.\nNeed to get 22.3 MB of archives.\nAfter this operation, 170 MB of additional disk space will be used.\nGet:1 http://mirrors.163.com/debian buster/main amd64 libmpfr6 amd64 4.0.2-1 [775 kB]\nGet:2 http://mirrors.163.com/debian buster/main amd64 libsigsegv2 amd64 2.12-2 [32.8 kB]\nGet:3 http://mirrors.163.com/debian buster/main amd64 gawk amd64 1:4.2.1+dfsg-1 [660 kB]\nGet:4 http://mirrors.163.com/debian 
        "Setting up libio-html-perl (1.001-1) ...",
        "Setting up libmariadb3:amd64 (1:10.3.27-0+deb10u1) ...",
        "Setting up galera-3 (25.3.25-2) ...",
        "Setting up libtimedate-perl (2.3000-2+deb10u1) ...",
        "Setting up mariadb-client-core-10.3 (1:10.3.27-0+deb10u1) ...",
        "Setting up libfcgi-perl (0.78-2+b3) ...",
        "Setting up libterm-readkey-perl (2.38-1) ...",
        "Setting up liburi-perl (1.76-1) ...",
        "Setting up libdbi-perl:amd64 (1.642-1+deb10u1) ...",
        "Setting up rsync (3.1.3-6) ...",
        "Created symlink /etc/systemd/system/multi-user.target.wants/rsync.service -> /lib/systemd/system/rsync.service.",
        "Setting up libhttp-date-perl (6.02-1) ...",
        "Setting up mariadb-client-10.3 (1:10.3.27-0+deb10u1) ...",
        "Setting up libdbd-mysql-perl:amd64 (4.050-2) ...",
        "Setting up libhtml-parser-perl (3.72-3+b3) ...",
        "Setting up mariadb-server-10.3 (1:10.3.27-0+deb10u1) ...",
        "Created symlink /etc/systemd/system/mysql.service -> /lib/systemd/system/mariadb.service.",
        "Created symlink /etc/systemd/system/mysqld.service -> /lib/systemd/system/mariadb.service.",
        "Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service -> /lib/systemd/system/mariadb.service.",
        "Setting up libhttp-message-perl (6.18-1) ...",
        "Setting up libcgi-pm-perl (4.40-1) ...",
        "Setting up libhtml-template-perl (2.97-1) ...",
        "Setting up mariadb-server (1:10.3.27-0+deb10u1) ...",
        "Setting up libcgi-fast-perl (1:2.13-1) ...",
        "Processing triggers for systemd (241-7~deb10u5) ...",
        "Processing triggers for man-db (2.8.5-2) ...",
        "Processing triggers for libc-bin (2.28-10) ..."
    ]
}

2、卸载软件

也就是把state的值改成absent即可

ansible hello -m apt -a "name=mariadb-server state=absent"

实现效果:

root@debian:~#  ansible hello -m apt -a "name=mariadb-server state=absent"
server1 | CHANGED => {
    "changed": true,
    "stderr": "",
    "stderr_lines": [],
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following packages were automatically installed and are no longer required:\n  galera-3 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl\n  libdbd-mysql-perl libdbi-perl libencode-locale-perl libfcgi-perl\n  libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl\n  libhttp-date-perl libhttp-message-perl libio-html-perl\n  liblwp-mediatypes-perl libmariadb3 libsnappy1v5 libterm-readkey-perl\n  libtimedate-perl liburi-perl mariadb-client-10.3 mariadb-client-core-10.3\n  mariadb-common mariadb-server-10.3 mariadb-server-core-10.3 mysql-common\n  rsync socat\nUse 'apt autoremove' to remove them.\nThe following packages will be REMOVED:\n  mariadb-server\n0 upgraded, 0 newly installed, 1 to remove and 7 not upgraded.\nAfter this operation, 67.6 kB disk space will be freed.\n(Reading database ... \r(Reading database ... 5%\r(Reading database ... 10%\r(Reading database ... 15%\r(Reading database ... 20%\r(Reading database ... 25%\r(Reading database ... 30%\r(Reading database ... 35%\r(Reading database ... 40%\r(Reading database ... 45%\r(Reading database ... 50%\r(Reading database ... 55%\r(Reading database ... 60%\r(Reading database ... 65%\r(Reading database ... 70%\r(Reading database ... 75%\r(Reading database ... 80%\r(Reading database ... 85%\r(Reading database ... 90%\r(Reading database ... 95%\r(Reading database ... 100%\r(Reading database ... 46049 files and directories currently installed.)\r\nRemoving mariadb-server (1:10.3.27-0+deb10u1) ...\r\n",
    "stdout_lines": [
        "Reading package lists...",
        "Building dependency tree...",
        "Reading state information...",
        "The following packages were automatically installed and are no longer required:",
        "  galera-3 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl",
        "  libdbd-mysql-perl libdbi-perl libencode-locale-perl libfcgi-perl",
        "  libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl",
        "  libhttp-date-perl libhttp-message-perl libio-html-perl",
        "  liblwp-mediatypes-perl libmariadb3 libsnappy1v5 libterm-readkey-perl",
        "  libtimedate-perl liburi-perl mariadb-client-10.3 mariadb-client-core-10.3",
        "  mariadb-common mariadb-server-10.3 mariadb-server-core-10.3 mysql-common",
        "  rsync socat",
        "Use 'apt autoremove' to remove them.",
        "The following packages will be REMOVED:",
        "  mariadb-server",
        "0 upgraded, 0 newly installed, 1 to remove and 7 not upgraded.",
        "After this operation, 67.6 kB disk space will be freed.",
        "(Reading database ... ",
        "(Reading database ... 5%",
        "(Reading database ... 10%",
        "(Reading database ... 15%",
        "(Reading database ... 20%",
        "(Reading database ... 25%",
        "(Reading database ... 30%",
        "(Reading database ... 35%",
        "(Reading database ... 40%",
        "(Reading database ... 45%",
        "(Reading database ... 50%",
        "(Reading database ... 55%",
        "(Reading database ... 60%",
        "(Reading database ... 65%",
        "(Reading database ... 70%",
        "(Reading database ... 75%",
        "(Reading database ... 80%",
        "(Reading database ... 85%",
        "(Reading database ... 90%",
        "(Reading database ... 95%",
        "(Reading database ... 100%",
        "(Reading database ... 46049 files and directories currently installed.)",
        "Removing mariadb-server (1:10.3.27-0+deb10u1) ..."
    ]
}
root@debian:~# 

3、其他语法及参数

3.1、更新软件

ansible hello -m apt -a "name=nginx state=latest default_release=squeeze-backports update_cache=yes"

参数对照表

参数作用
name=nginx指定软件名:nginx
state=latest最新版本
default_release使用的首选源:squeeze-backports
update_cache是否在安装之前更新源,也就是apt update

相关文章:

ansible从入门到入土:常用模块---->apt模块

ansible从入门到入土:教你如何编写ansible剧本实现yum安装软件

ansible从入门到入土:对剧本进行语法检查及真/伪执行剧本

ansible从入门到入土:带你从安装到实现“hello world

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坐公交也用券

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值