Ansible--Module

原创
小哥 3年前 (2022-11-02) 阅读数 42 #大杂烩

Ansible--Module

Ansible Module #

Ansible Ad-hoc模式公共模块 #

ansible-doc 常用命令

ansible-doc -h

Usage: ansible-doc [-l|-F|-s] [options] [-t ] [plugin] -j 以jsonFormat显示所有模块信息。 -l 列出所有模块 -s 查看常用模块参数

直接跟随模块名称,显示模块的所有信息

[root@ansible ~]# ansible-doc -j [root@ansible ~]# ansible-doc -l [root@ansible ~]# ansible-doc -l |wc -l #统计所有模块的数量,ansible2.8共计2834个模块 2834

与命令相关的模块 #

command #

ansible 默认模块,执行命令,注意:shell中的 "<" , ">" , "|" , ";" , "&" , "$" 特殊字符,如 command 在模块中使用,如果需要, shell 模块

查看模块参数

[root@ansible ~]# ansible-doc -s command

在192.168.1.31在服务器上方执行ls命令,则默认设置在当前用户的主目录中。/root

[root@ansible ~]# ansible 192.168.1.31 -a ls

chdir 首先切换工作目录,然后执行以下命令,这些命令通常在编译时使用。

[root@ansible ~]# ansible 192.168.1.31 -a chdir=/tmp pwd 192.168.1.31 | CHANGED | rc=0 >> /tmp

creates 如果creates如果文件为

[root@ansible ~]# ansible 192.168.1.31 -a creates=/tmp ls /etc/passwd #tmp如果该目录存在,请执行以下操作ls命令 192.168.1.31 | SUCCESS | rc=0 >> skipped, since /tmp exists [root@ansible ~]# ansible 192.168.1.31 -a creates=/tmp11 ls /etc/passwd # tmp11如果该文件不存在,请执行以下操作ls命令 192.168.1.31 | CHANGED | rc=0 >> /etc/passwd

removes 和creates相反,如果removes在执行后一个操作之前,该文件已存在。

[root@ansible ~]# ansible 192.168.1.31 -a removes=/tmp ls /etc/passwd #tmp文件存在,然后是后者ls命令 192.168.1.31 | CHANGED | rc=0 >> /etc/passwd [root@ansible ~]# ansible 192.168.1.31 -a removes=/tmp11 ls /etc/passwd #tmp11如果该文件不存在,请执行以下操作ls命令 192.168.1.31 | SUCCESS | rc=0 >> skipped, since /tmp11 does not exist

shell #

专门用来执行 shell 命令的模块,以及 command 模块是一样的,参数基本是一样的,有 chdir,creates,removes 等参数

查看模块参数

[root@ansible ~]# ansible-doc -s shell

[root@ansible ~]# ansible 192.168.1.31 -m shell -a mkdir /tmp/test [root@ansible ~]# ansible 192.168.1.31 -m shell -a ls /tmp

执行以下命令,每次更新文件的时间戳。

[root@ansible ~]# ansible 192.168.1.31 -m shell -a cd /tmp/test && touch 1.txt && ls 192.168.1.31 | CHANGED | rc=0 >> 1.txt

因为有时您不想更新文件的创建时间戳,所以如果文件存在,也不会执行。creates

[root@ansible ~]# ansible 192.168.1.31 -m shell -a creates=/tmp/test/1.txt cd /tmp/test && touch 1.txt && ls 192.168.1.31 | SUCCESS | rc=0 >> skipped, since /tmp/test/1.txt exists

script #

用于在托管计算机上执行。 shell 脚本模块、脚本不需要存在于托管计算机上。

查看模块参数

[root@ansible ~]# ansible-doc -s script

编写shell脚本

[root@ansible ~]# vim ansible_test.sh

!/bin/bash

echo hostname

在所有托管计算机上执行该脚本。

[root@ansible ~]# ansible all -m script -a /root/ansible_test.sh 192.168.1.32 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.1.32 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.1.32 closed." ], "stdout": "linux.node02.com\r\n", "stdout_lines": [ "linux.node02.com" ] } ......

与文件相关的模块 #

file #

用于文件处理、创建、删除、权限控制等。

查看模块参数

[root@ansible ~]# ansible-doc -s file path #要管理的文件路径 recurse #递归 state: directory #如果目标不存在,则创建目录及其子目录。 touch #创建一个文件,如果该文件存在,则修改该文件。 属性

 absent     #删除文件或目录
 mode       #设置文件或目录权限
 owner      #设置文件或目录所有者信息。
 group      #设置文件或目录组信息。
 link       #创建需要和的软连接。src配合使用
 hard       #创建需要和的硬连接。src配合使用

创建目录

[root@ansible ~]# ansible 192.168.1.31 -m file -a path=/tmp/test1 state=directory

创建文件

[root@ansible ~]# ansible 192.168.1.31 -m file -a path=/tmp/test2 state=touch

建立软链接(src表示源文件,path表示目标文件)

[root@ansible ~]# ansible 192.168.1.31 -m file -a src=/tmp/test1 path=/tmp/test3 state=link

删除文件

[root@ansible ~]# ansible 192.168.1.31 -m file -a path=/tmp/test2 state=absent

创建文件时,请同时设置权限和其他信息

[root@ansible ~]# ansible 192.168.1.31 -m file -a path=/tmp/test4 state=directory mode=775 owner=root group=root

copy #

用于管理端将文件复制到远程主机,可以设置权限、属于组、属于主机等。

查看模块参数

[root@ansible ~]# ansible-doc -s copy src #需要copy文件的源路径。 dest #需要copy文件的目标路径。 backup #对copy的文件 content #将内容直接添加到远程主机管理的文件将覆盖原始文件内容 mode #对copy设置远程文件的权限 owner #对copy远端的文件设置属于主服务器 group #对copy到属于该组的远程文件设置。

将文件复制到远程主机并重命名

[root@ansible ~]# ansible 192.168.1.31 -m copy -a dest=/tmp/a.sh src=/root/ansible_test.sh

将文件复制到远程主机并备份远程文件。,安装时间信息备份文件(当文件内容更新时,重新-copy时用到)

[root@ansible ~]# ansible 192.168.1.31 -m copy -a dest=/tmp/a.sh src=/root/ansible_test.sh backup=yes

直接在远程主机上a.sh添加内容

[root@ansible ~]# ansible 192.168.1.31 -m copy -a dest=/tmp/a.sh content="#!/bin/bash\n echo uptime"

将文件复制到远程主机并设置权限以及所有者和所有者组

[root@ansible ~]# ansible 192.168.1.31 -m copy -a dest=/tmp/passwd src=/etc/passwd mode=700 owner=root group=root

fetch #

它用于从托管计算机拉取文件。提取的内容将保留目录结构。一般用于采集被管理机器的日志文件等。

查看模块参数

[root@ansible ~]# ansible-doc -s fetch src #指定需要从远程计算机拉出的文件路径。 dest #指定从远程计算机拉取的文件存储路径

从托管计算机拉入cron默认情况下,日志文件将以托管节点地址创建目录,并存储在

[root@ansible ~]# ansible 192.168.1.31 -m fetch -a dest=/tmp src=/var/log/cron

[root@ansible ~]# tree /tmp/192.168.1.31/ /tmp/192.168.1.31/ └── var └── log └── cron

2 directories, 1 file

与用户相关的模块 #

user #

用于管理系统用户、用户创建、删除、主目录、组设置等。

查看模块参数

[root@ansible ~]# ansible-doc -s user name #指定用户名 home #指定用户的主目录 uid #指定用户的uid group #指定用户的用户组 groups #为用户指定其他组 password #指定用户的密码 shell #指定用户的登录名shell create_home #是否创建用户主目录,默认为yes remove #删除用户时,指定是否删除主目录。 state: absent #删除用户

创建用户名要指定主目录,请指定。uid及组

[root@ansible ~]# ansible 192.168.1.31 -m user -a name=mysql home=/opt/mysql uid=1002 group=root [root@ansible ~]# ansible 192.168.1.31 -m shell -a id mysql && ls -l /opt 192.168.1.31 | CHANGED | rc=0 >> uid=1002(mysql) gid=0(root) 组=0(root) 总用量 0 drwx------ 3 mysql root 78 5月 27 18:13 mysql

创建用户,不创建主目录,并且无法登录

[root@ansible ~]# ansible 192.168.1.31 -m user -a name=apache shell=/bin/nologin uid=1003 create_home=no [root@ansible ~]# ansible 192.168.1.31 -m shell -a id apache && tail -1 /etc/passwd 192.168.1.31 | CHANGED | rc=0 >> uid=1003(apache) gid=1003(apache) 组=1003(apache) apache:x:1003:1003::/home/apache:/bin/nologin

删除用户

[root@ansible ~]# ansible 192.168.1.31 -m user -a name=apache state=absent

删除用户和删除主目录

[root@ansible ~]# ansible 192.168.1.31 -m user -a name=mysql state=absent remove=yes

group #

用于创建组,创建用户时如果需要指定组,则可以传递不存在的组。 group 先创建组

查看模块参数

[root@ansible ~]# ansible-doc -s group name #指定组的名称 gid #指定组的gid state: absent #删除组 present #创建组(默认状态)

创建组

[root@ansible ~]# ansible 192.168.1.31 -m group -a name=www

创建一个组并指定gid

[root@ansible ~]# ansible 192.168.1.31 -m group -a name=www1 gid=1005

删除组

[root@ansible ~]# ansible 192.168.1.31 -m group -a name=www1 state=absent

与包相关的模块 #

yum #

用于软件包管理、下载、安装、卸载、升级等。

查看模块参数

[root@ansible ~]# ansible-doc -s yum name #指定要对其执行操作的包的名称 download_dir #指定需要匹配的下载软件包的存储路径。download_only一起使用 download_only #只下载程序包,而不是安装和。yum --downloadonly一样 list: installed #列出所有已安装的程序包 updates #列出所有可以更新的包 repos #全部列出yum仓库 state:
installed, present #安装程序包(两者都可以选择) removed, absent #卸载程序包 latest #安装最新的程序包

列出所有已安装的程序包

[root@ansible ~]# ansible 192.168.1.31 -m yum -a list=installed

列出所有可更新的包

[root@ansible ~]# ansible 192.168.1.31 -m yum -a list=updates

全部列出yum仓库

[root@ansible ~]# ansible 192.168.1.31 -m yum -a list=repos

只需下载该程序包并转到指定目录。

[root@ansible ~]# ansible 192.168.1.31 -m yum -a name=httpd download_only=yes download_dir=/tmp

安装程序包

[root@ansible ~]# ansible 192.168.1.31 -m yum -a name=httpd state=installed

卸载程序包

[root@ansible ~]# ansible 192.168.1.31 -m yum -a name=httpd state=removed

安装软件包组,类似yum groupinstall Development Tools

[root@ansible ~]# ansible 192.168.1.31 -m yum -a name="@Development Tools" state=installed

pip #

用于安装python中的包

查看模块参数

[root@ansible ~]# ansible-doc -s pip

使用pip当您需要确保托管计算机具有python-pip软件包

[root@ansible ~]# ansible 192.168.1.31 -m yum -a name=python-pip

安装pip包

[root@ansible ~]# ansible 192.168.1.31 -m pip -a name=flask

service #

服务模块,用于管理服务、服务的启动、关闭、启动等。

查看模块参数

[root@ansible ~]# ansible-doc -s service name #指定需要管理的服务的名称。 enabled #指定是否从电源开始 state: #指定服务状态 started #启动服务 stopped #停止服务 restarted #重启服务 reloaded #重载服务

启动该服务并将引导设置为自启动。

[root@ansible ~]# ansible 192.168.1.31 -m service -a name=crond state=started enabled=yes

规划与任务相关的模块 #

cron #

用于指定计划任务,以及 crontab -e 一样

查看模块参数

[root@ansible ~]# ansible-doc -s cron job #指定要执行的任务 minute #分钟 hour #小时 day #天 month #月 weekday #周 name #描述计划任务 state: absetn #删除计划任务

创建计划任务并描述使用该任务的原因。

[root@ansible ~]# ansible 192.168.1.31 -m cron -a "name=这是一项有计划的测试任务 minute= hour= day= month= weekday=* job=/bin/bash /root/test.sh" [root@ansible ~]# ansible 192.168.1.31 -m shell -a crontab -l 192.168.1.31 | CHANGED | rc=0 >>

Ansible: 这是一项有计划的测试任务

          • /bin/bash /root/test.sh

创建不带说明的计划任务

[root@ansible ~]# ansible 192.168.1.31 -m cron -a "job=/bin/sh /root/test.sh"

删除计划任务

[root@ansible ~]# ansible 192.168.1.31 -m cron -a "name=None job=/bin/sh /root/test.sh state=absent"

系统信息相关模块 #

setup #

获取系统信息的模块。

查看模块参数

[root@ansible ~]# ansible-doc -s setup

查看所有系统信息

[root@ansible ~]# ansible 192.168.1.31 -m setup

filter 过滤系统信息

[root@ansible ~]# ansible 192.168.1.31 -m setup -a filter=ansible_all_ipv4_addresses

常见过滤选项

ansible_all_ipv4_addresses 所有的ipv4地址 ansible_all_ipv6_addresses 所有的ipv6地址 ansible_architecture 系统的体系结构 ansible_date_time 系统时间 ansible_default_ipv4 系统的默认设置ipv4地址 ansible_distribution 系统名称 ansible_distribution_file_variety 系统系列 ansible_distribution_major_version 系统的版本 ansible_domain 系统所在的域 ansible_fqdn 系统的主机名 ansible_hostname 系统的主机名,简写 ansible_os_family 系统系列 ansible_processor_cores cpu的核数 ansible_processor_count cpu的颗数 ansible_processor_vcpus cpu的个数

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门