自动化运维工具ansible 2常用模块


常用模块基本的命令格式:
ansible 匹配主机 -m 模块名 -a 指定参数
常用模块的参数查看方法
ansible-doc -s 模块名
node1(192.168.225.101)控制的主机
node2(192.168.225.102)被控制
node3(192.168.225.103)被控制

shell和command模块(两个都差不多)

1.我们在哪听过shell?shell不就是我们输入命令的地方吗,我们再来linux中输入的命令就是shell解释器翻译给内核,让内核执行的呀。
2.使用方法(默认使用就是command)
直接给参数就行了
3.缺点:
(1)command,有些复杂的命令执行起来会报错,例如管道符,重定向符号会报错。shell模块可以解决这个问题。
(2)麻烦:例如我想给一个用户设置密码不能直接用下面的方法,因为这种方法在执行命令后需要往标准流里输入指定密码。然而ansible只能把命令的标准输出返回,不能再次在标准流里输入。

passwd 用户名

因此,要在输入命令时就在标准流里指定密码(例如下面),好烦。因此有了其他模块的出现,帮助我们更好更方便地控制远程的主机。

[root@localhost ~]# ansible node2 -m shell -a "echo passwd test --stdin 123456  "
192.168.225.102 | SUCCESS | rc=0 >>
passwd test --stdin 123456

ping模块,测试ping通模块

1.可用参数

[root@node1 ~]# ansible-doc -s ping
- name: Try to connect to host, verify a usable python and return `pong' on success
  ping:
      data:                  #修改返回的字符串

2.实例:使得ping node2返回的信息是hello

[root@node1 ~]# ansible node2 -m ping -a "data='hello'"
192.168.225.102 | SUCCESS => {
    "changed": false, 
    "ping": "hello"
}

user模块,用户创建模块

1.可用参数

[root@node1 ~]# ansible-doc -s user
- name: Manage user accounts
  user:
    comment    # 用户的描述信息
    createhom  # 是否创建家目录,默认创建。如果不创建,则指定为no
    force      # 在使用`state=absent'是, 行为与`userdel --force'一致.
    group      # 指定基本组,指定方式可以是指定组的名字或者指定组的gid
    groups     # 指定附加组,如果指定为('groups=')表示删除所有组
    home       # 指定用户家目录
    name           # 指定用户名
    password        # 指定用户密码
    remove          # 在使用 `state=absent'时, 行为是与 `userdel --remove'一致.
    shell           # 指定默认shell
    state           #设置帐号状态,不指定为创建,指定值为absent表示删除
    system          # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户。
    uid             #指定用户的uid
    update_password  # 更新用户密码
    expires         #指明密码的过期时间

2.实例:
(1)创建一个test用户,为他指定组,指定家目录,指定uid,指定登陆shell指定密码。
(这里要注意,这里只允许使用加密后的密码,用openssl加密,设置后,才能登陆)。
(2)删除这个用户

[root@node1 ~]# openssl passwd
Password: 
Verifying - Password: 
eJwumU5AEuk6w
eJwumU5AEuk6w
[root@node1 ~]# ansible node2 -m user -a "name=hzy shell=/bin/bash 
[root@node1 ~]# ansible node2 -m user -a "name=test state=absent"

group模块,组创模块

1.可用参数

[root@node1 ~]# ansible-doc -s group
- name: 添加或删除组
  action: group
    gid       # 设置组的GID号
    name=     # 管理组的名称
    state     # 指定组状态,默认为创建,设置值为absent为删除
    system    # 设置值为yes,表示为创建系统组

2.实例
往node2创建一个组指定gid,组号,指定为系统组。并且查看

[root@node1 ~]# ansible node2 -m group -a "gid=5000 name=test_group system=yes"
[root@node1 ~]# ansible node2 -a "cat /etc/group| tail -n 1"

结果第二条报错了,是我们命令记错了吗?cat /etc/group| tail -n 1在node2本机上就是最后一个创建组,正确的呀,怎么回事???这就是上面说的默认使用的command的缺点,不能使用重定向符号。解决方法看下一个模块。

script模块,脚本执行模块。

1.可用参数

root@node1 test]# ansible-doc -s script
- name: Runs a local script on a remote node after transferring it
  script:
      chdir:                 # 在执行脚本之前,先修改所在节点的路径(默认在被控节点的家目录里)
      creates:               # 远程节点上的文件名,如果已存在,则不会运行此步骤。
      free_form:             # 本地脚本文件的路径,后跟可选参数。通常直接调用就行,不要写这个容易出bug
      removes:               #如果远程节点上没有同名文件,就不会执行

2.实例
在node1上写一个脚本,使得在node2的tmp目录下有一个名字为指定名字文件

[root@node1 ~]# chmod 777 1.sh 
[root@node1 ~]# cat 1.sh 
#!/bin/bash
touch /tmp/helloworld

[root@node1 ~]# ansible node2 -m script -a "1.sh"

copy模块,文件复制模块

1.可用参数

[root@node1 ~]# ansible-doc -s copy
- name: Copies files to remote locations
  copy:
    backup:在覆盖之前,将源文件备份,备份文件包含时间信息。
    content:用于替代“src”,可以直接设定指定文件的值
    dest:必选项。要将源文件复制到的远程主机的绝对路径
    directory_mode:递归设定目录的权限,默认为系统默认权限
    force:强制覆盖目的文件内容,默认为yes
    others:所有的file模块里的选项都可以在这里使用
    src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。

2.实例
在node1和node2上都有test_dir文件夹,把node1的这个文件夹复制到node2上,并且把语原本node2上的文件夹备份

要注意:目的地址必须是绝对路径,并且要有备份。(注意千万不要有多余的空格,否则会很难受)

[root@node1 ~]# ansible node2 -m copy  -a "src='test_dir' dest='/root/' backup=yes"

file模块,文件管理模块

只能对被控制的机器里的文件做操作。不能把本机上的文件传输到目标机器上。
1.可用参数

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

2.实例:
(1)在node2上创建一个test_src_file文件。
(2)在node2上创建一个指向test_src_file的软连接。
(3)删除链接文件和原文件。

[root@node1 ~]# ansible node2 -m file  -a "path='/root/test_src_file' state=touch"
[root@node1 ~]# ansible node2 -m file  -a "src='/root/test_src_file' dest='/root/link_file' state=link"
[root@node1 ~]# ansible node2 -m file  -a "path='/root/link_file' state=absent"
[root@node1 ~]# ansible node2 -m file  -a "path='/root/test_src_file' state=absent"

cron模块,计划任务模块

1.可用参数

root@node1 ~]# ansible-doc -s cron
- name: Manage cron.d and crontab entries.
  cron:
     name:任务计划名称
	cron_file:替换客户端该用户的任务计划的文件
	minute:分(0-59, * ,*/2)
	hour:时(0-23, * ,*/2)
	day:日(1-31, * ,*/2)
	month:月(1-12, * , */2)
	weekday:周(0-6或1-7, *)
	job:任何计划执行的命令,state要等于present
	backup:是否备份之前的任务计划
	user:新建任务计划的用户
	state:指定任务计划present、absent

2.实例:创建一个每分钟输出helloworld的计划任务,等一会删除它(必须给名字才能够删除)

[root@node1 ~]# ansible node2 -m cron -a "name=‘test1’ job='echo helloworld' minute=* hour=* day=* month=* weekday=*"
[root@node1 ~]# ansible node2 -m cron -a "name=‘test1’ state=absent"

yum模块,管理文件下载的模块

[root@node1 ~]# ansible-doc -s yum
- name: Manages packages with the yum package manager
  yum:
    conf_file         #设定远程yum安装时所依赖的配置文件。如配置文件没有在默认的位置。
    disable_gpg_check    #是否禁止GPG checking,只用于`present' or `latest'。
    disablerepo   #临时禁止使用yum库。 只用于安装或更新时。
    enablerepo    #临时使用的yum库。只用于安装或更新时。
    name=            #所安装的包的名称
    state               #present安装, latest安装最新的, absent 卸载软件。
    update_cache  #强制更新yum的缓存。

2.实例
在node2上下载httpd的最新模块

[root@node1 ~]# ansible node2 -m yum -a "name=httpd state='latest' "

service模块,服务管理模块

[root@node1 ~]# ansible-doc service
> SERVICE    (/usr/lib/python2.7/site-packages/ansible/modules/system/service.py)
        Controls services on remote hosts. Supported init systems include BSD init, OpenRC, SysV, Solaris
        SMF, systemd, upstart. For Windows targets, use the [win_service] module instead.
  * note: This module has a corresponding action plugin.
arguments         #命令行提供额外的参数
enabled           #设置开机启动。
name=             #服务名称
runlevel          #开机启动的级别,一般不用指定。
sleep             #在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。
state     #started启动服务, stopped停止服务, restarted重启服务, reloaded重载配置

2.启动httpd服务,并且设置为开机启动,检查(用curl -I localhost)发现403错误,是由于防火墙没有关闭。

[root@node1 ~]# ansible node2 -m service -a "name=httpd state=started enabled=yes"
[root@node1 ~]# ansible node2 -a "curl -I localhost"
 [WARNING]: Consider using get_url or uri module rather than running curl

firewalld模块,防火墙模块

1.可用参数

state:                 # 接受开放则为enable,否则为rejectrequired) Should this port accept(enabled) or reject(disabled) connections
[root@node1 ~]# ansible-doc -s firewalld
port:                  # 端口号/协议for port
service:               #服务名称

2.实例
开放80端口,使得httpd服务可以访问

[root@node1 ~]# ansible node2 -m firewalld -a "port=80/tcp state=enabled"
192.168.225.102 | SUCCESS => {
    "changed": true, 
    "msg": "Non-permanent operation, Changed port 80/tcp to enabled"
}
[root@node1 ~]# ansible node2 -a "curl -I localhost"
 [WARNING]: Consider using get_url or uri module rather than running curl

192.168.225.102 | SUCCESS | rc=0 >>
HTTP/1.1 200 OK

uri模块,get_url模块,检测web网站

1.可用参数
url 网站的域名或ip
dest get_url方法需要指定下载网页的目的地

2.分别使用这两种方法检测我们刚才的的网站服务

[root@node1 ~]# ansible node2  -m uri -a "  url=http://192.168.225.102"
ansible node2  -m get_url -a " dest=/root url=http://192.168.225.102"

setup模块,收集所有关于主机的信息

1.可用参数
模块用于收集远程主机的一些基本信息,时间会很长
filter参数:用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。

ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)

2.实例
(1)收集node2主机的所有信息
(2)获取其中的cpu信息

[root@node1 test]# ansible node2 -m setup "filter=ansible_processor"
[root@node1 test]# ansible node2 -m setup -a "filter=ansible_processor"

selinux模块,限制管理模块

1.可用参数

[root@node1 ~]# ansible-doc -s selinux
- name: Change policy and state of SELinux
  selinux:
      conf:                  # path to the SELinux configuration file, if non-standard
      policy:                # name of the SELinux policy to use
      state:                 # (required) The SELinux mode

2.实例:关闭selinux

[root@node1 ~]# ansible node2 -m selinux -a "state=disabled"

猜你喜欢

转载自blog.csdn.net/weixin_44055272/article/details/88765092