企业运维----自动化运维工具-Ansible(二)Ansible中的常用模块


1.进行主机连通性测试

[devops@westos_student41 ansible]$ ansible all -m ping
172.25.41.3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
172.25.41.1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
172.25.41.2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

主机是连通状态

2.Command模块

常用命令

chdir       # 在执行命令之前,先切换到该目录
executable  # 切换shell来执行命令,需要使用命令的绝对路径
free_form   # 要执行的Linux指令,一般使用Ansible的-a参数代替。
creates     # 一个文件名,当这个文件存在,则该命令不执行,可以用来做判断
removes     # 一个文件名,这个文件不存在,则该命令不执行

实验

[devops@westos_student41 ansible]$ ansible westos -m command -a "touch /mnt/westosfile"
[WARNING]: Consider using the file module with state=touch rather than running
'touch'.  If you need to use command because file is insufficient you can add 'warn:
false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid
of this message.
172.25.41.1 | CHANGED | rc=0 >>

[root@westosa mnt]# ls
westosfile
[devops@westos_student41 ansible]$ ansible westos -m command -a "rm -fr  /mnt/westosfile"
[WARNING]: Consider using the file module with state=absent rather than running 'rm'.
If you need to use command because file is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of this
message.
172.25.41.1 | CHANGED | rc=0 >>

[root@westosa mnt]# ls
[root@westosa mnt]# 

chdir 进入目录执行操作

[devops@westos_student41 ansible]$ ansible westos -m command -a "chdir=/mnt/ touch westosfile"
[WARNING]: Consider using the file module with state=touch rather than running
'touch'.  If you need to use command because file is insufficient you can add 'warn:
false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid
of this message.
172.25.41.1 | CHANGED | rc=0 >>

creates file存在 后面的不执行,removes file存在 后面的执行

[devops@westos_student41 ansible]$ ansible westos -m command -a "creates=/mnt/westosfile touch /mnt/linuxfile"
172.25.41.1 | SUCCESS | rc=0 >>
skipped, since /mnt/westosfile exists
[devops@westos_student41 ansible]$ ansible westos -m command -a "removes=/mnt/westosfile touch /mnt/linuxfile"
[WARNING]: Consider using the file module with state=touch rather than running
'touch'.  If you need to use command because file is insufficient you can add 'warn:
false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid
of this message.
172.25.41.1 | CHANGED | rc=0 >>

[root@westosa mnt]# ls
linuxfile  westosfile

颜色代表信息:

绿色: 执行成功但未对远程主机做任何更改
黄色: 执行成功并对远程主机作改变
红色: 执行失败

3.Shell模块

shell模块可以在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等。

executable=bash 指定shell运行

[devops@westos_student41 ansible]$ ansible westos -m shell -a "ps ax |grep $$"
172.25.41.1 | CHANGED | rc=0 >>
   5108 pts/1    S+     0:00 /bin/sh -c ps ax |grep 4322
   5110 pts/1    R+     0:00 grep 4322
[devops@westos_student41 ansible]$ ansible westos -m shell -a "executable=bash ps ax |grep $$"
172.25.41.1 | CHANGED | rc=0 >>
   5293 pts/1    S+     0:00 bash -c ps ax |grep 4322
   5295 pts/1    R+     0:00 grep 4322

脚本运行

[devops@westos_student41 ansible]$ cat hostname.sh 
echo hello westos
[devops@westos_student41 ansible]$ ansible westos -m script -a './hostname.sh'
172.25.41.1 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 172.25.41.1 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 172.25.41.1 closed."
    ],
    "stdout": "hello westos\r\n",
    "stdout_lines": [
        "hello westos"
    ]
}

4.Copy模块

这个模块用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限等。

    src    #被复制到远程主机的本地文件。可以是绝对路径,也可以是相对路径。如果路径是一个目录,则会递归复制,用法类似于"rsync"
    content   #用于替换"src",可以直接指定文件的值
    dest    #必选项,将源文件复制到的远程主机的绝对路径
    backup   #当文件内容发生改变后,在覆盖之前把源文件备份,备份文件包含时间信息
    directory_mode    #递归设定目录的权限,默认为系统默认权限
    force    #当目标主机包含该文件,但内容不同时,设为"yes",表示强制覆盖;设为"no",表示目标主机的目标位置不存在该文件才复制。默认为"yes"
    others    #所有的 file 模块中的选项可以在这里使用

复制本地文件到远程主机/mnt下 指定权限为1777

[devops@westos_student41 ansible]$ ls
ansible.cfg  inventory  test.yaml
[devops@westos_student41 ansible]$ ansible westos -m copy -a 'src=test.yaml dest=/mnt/test.yaml mode=1777'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "d89c19a70cb3e0ea095b90f149bdadc46ebc13cd",
    "dest": "/mnt/test.yaml",
    "gid": 0,
    "group": "root",
    "md5sum": "ad85738e99a3c6e5ec3970c6a4cfac26",
    "mode": "01777",
    "owner": "root",
    "secontext": "system_u:object_r:mnt_t:s0",
    "size": 1150,
    "src": "/home/devops/.ansible/tmp/ansible-tmp-1629959076.6076407-11254-122232893347708/source",
    "state": "file",
    "uid": 0
}

[root@westosa mnt]# ll
total 4
-rw-r--r--. 1 root root    0 Aug 26 14:10 linuxfile
-rwxrwxrwt. 1 root root 1150 Aug 26 14:24 test.yaml
-rw-r--r--. 1 root root    0 Aug 26 14:04 westosfile

backup=yes 修改文件之后可以再copy会将之前的文件备份

[devops@westos_student41 ansible]$ vim test.yaml 
[devops@westos_student41 ansible]$ ansible westos -m copy -a 'src=test.yaml dest=/mnt/test.yaml mode=1777 owner=westos backup=yes'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup_file": "/mnt/test.yaml.6753.2021-08-26@14:32:16~",
    "changed": true,
    "checksum": "cea0f065fbb21d3660ed12749f26c3dc1eb00438",
    "dest": "/mnt/test.yaml",
    "gid": 0,
    "group": "root",
    "md5sum": "6bbcf9b0e80d72cd6a77466961a43fea",
    "mode": "01777",
    "owner": "westos",
    "secontext": "system_u:object_r:mnt_t:s0",
    "size": 1156,
    "src": "/home/devops/.ansible/tmp/ansible-tmp-1629959533.6960654-11557-170862689266381/source",
    "state": "file",
    "uid": 1000
}

[root@westosa mnt]# ll
total 8
-rw-r--r--. 1 root   root    0 Aug 26 14:10 linuxfile
-rwxrwxrwt. 1 westos root 1156 Aug 26 14:32 test.yaml
-rwxrwxrwt. 1 westos root 1150 Aug 26 14:24 test.yaml.6753.2021-08-26@14:32:16~
-rw-r--r--. 1 root   root    0 Aug 26 14:04 westosfile
[devops@westos_student41 ansible]$ ansible westos -m copy -a 'dest=/mnt/westos  mode=1777 owner=westos backup=yes content="hello westos"'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "dest": "/mnt/westos",
    "gid": 0,
    "group": "root",
    "md5sum": "dde5c4df2e17f6693afdd6769dcf4e09",
    "mode": "01777",
    "owner": "westos",
    "secontext": "system_u:object_r:mnt_t:s0",
    "size": 12,
    "src": "/home/devops/.ansible/tmp/ansible-tmp-1629959697.664989-11721-204976592233107/source",
    "state": "file",
    "uid": 1000
}

[root@westosa mnt]# cat westos
hello westos[root@westosa mnt]# 

5.Fetch模块

该模块用于从远程某主机获取(复制)文件到本地。
 
有两个选项:

 dest:用来存放文件的目录
 src:在远程拉取的文件,并且必须是一个file,不能是目录

将远程主机的文件复制到ansible主机中

[devops@westos_student41 ansible]$ ansible westos -m fetch -a "src=/mnt/westos dest=~/.ansible"
172.25.41.1 | CHANGED => {
    "changed": true,
    "checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "dest": "/home/devops/.ansible/172.25.41.1/mnt/westos",
    "md5sum": "dde5c4df2e17f6693afdd6769dcf4e09",
    "remote_checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "remote_md5sum": null
}
[devops@westos_student41 ansible]$ cd ..
[devops@westos_student41 ~]$ cd .ansible/
[devops@westos_student41 .ansible]$ ls
172.25.41.1  cp  tmp
[devops@westos_student41 .ansible]$ cd 172.25.41.1/mnt/
[devops@westos_student41 mnt]$ ls
westos
[devops@westos_student41 mnt]$ cat westos 
hello westos[devops@westos_student41 mnt]$ 

flat=yes 只负责负责文件,但需要给定文件名称

[devops@westos_student41 ansible]$ ansible westos -m fetch -a "src=/mnt/westos dest=~/.ansible/westos flat=yes"
172.25.41.1 | CHANGED => {
    "changed": true,
    "checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "dest": "/home/devops/.ansible/westos",
    "md5sum": "dde5c4df2e17f6693afdd6769dcf4e09",
    "remote_checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "remote_md5sum": null
}
[devops@westos_student41 ansible]$ cd ..
[devops@westos_student41 ~]$ cd .ansible/
[devops@westos_student41 .ansible]$ ls
172.25.41.1  cp  tmp  westos
[devops@westos_student41 .ansible]$ cat westos 
hello westos[devops@westos_student41 .ansible]$ 

清理一下westosa的mnt目录

[root@westosa mnt]# rm -fr *

6.File模块

该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等。
常见的命令:

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

创建文件

[devops@westos_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westos state=touch'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt/westos",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}

[root@westosa mnt]# ls
westos

创建目录

[devops@westos_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westosdir state=directory'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/mnt/westosdir",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 6,
    "state": "directory",
    "uid": 0
}
[root@westosa mnt]# ls
westos  westosdir

创建软连接

[devops@westos_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westos.link state=link src=/mnt/westos'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt/westos.link",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 11,
    "src": "/mnt/westos",
    "state": "link",
    "uid": 0
}
[root@westosa mnt]# ll
total 0
-rw-r--r--. 1 root root  0 Aug 26 14:54 westos
drwxr-xr-x. 2 root root  6 Aug 26 14:56 westosdir
lrwxrwxrwx. 1 root root 11 Aug 26 14:57 westos.link -> /mnt/westos

创建硬链接

[devops@westos_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westos.hard state=hard src=/mnt/westos'

172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt/westos.hard",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 0,
    "src": "/mnt/westos",
    "state": "hard",
    "uid": 0
}
[root@westosa mnt]# ll
total 0
-rw-r--r--. 2 root root  0 Aug 26 14:54 westos
drwxr-xr-x. 2 root root  6 Aug 26 14:56 westosdir
-rw-r--r--. 2 root root  0 Aug 26 14:54 westos.hard
lrwxrwxrwx. 1 root root 11 Aug 26 14:57 westos.link -> /mnt/westos

修改目录权限

[devops@westos_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westosdir/test state=touch'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt/westosdir/test",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}
[devops@westos_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westosdir mode=777'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "path": "/mnt/westosdir",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 18,
    "state": "directory",
    "uid": 0
}
[root@westosa mnt]# ll
total 0
-rw-r--r--. 2 root root  0 Aug 26 14:54 westos
drwxrwxrwx. 2 root root 18 Aug 26 15:04 westosdir
-rw-r--r--. 2 root root  0 Aug 26 14:54 westos.hard
lrwxrwxrwx. 1 root root 11 Aug 26 14:57 westos.link -> /mnt/westos
[root@westosa mnt]# cd westosdir/
[root@westosa westosdir]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 26 15:04 test

recurse=yes 递归修改目录权限和目录中子文件的权限

[devops@westos_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westosdir mode=777 recurse=yes'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "path": "/mnt/westosdir",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 18,
    "state": "directory",
    "uid": 0
}
[root@westosa westosdir]# ll
total 0
-rwxrwxrwx. 1 root root 0 Aug 26 15:04 test
[root@westosa mnt]# rm -fr *

7.Archive与unarchive模块

archive压缩

[devops@westos_student41 ansible]$ ansible westos -m archive -a 'path=/etc dest=/mnt/etc.tar.gz format=gz owner=westos mode=777'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "archived": [
        "/etc/mtab",
        "/etc/fstab",
        "/etc/crypttab",
        ......
        "/etc/smartmontools/smartd.conf",
        "/etc/smartmontools/smartd_warning.sh"
    ],
    "arcroot": "//",
    "changed": true,
    "dest": "/mnt/etc.tar.gz",
    "expanded_exclude_paths": [],
    "expanded_paths": [
        "/etc"
    ],
    "gid": 0,
    "group": "root",
    "missing": [],
    "mode": "0777",
    "owner": "westos",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 6795808,
    "state": "file",
    "uid": 1000
}
[root@westosa mnt]# ll
total 6640
-rwxrwxrwx. 1 westos root 6795808 Aug 26 15:13 etc.tar.gz

unarchive解压

[devops@westos_student41 ansible]$ ansible westos -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt/ copy=no'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt/",
    "extract_results": {
        "cmd": [
            "/bin/gtar",
            "--extract",
            "-C",
            "/mnt/",
            "-z",
            "-f",
            "/mnt/etc.tar.gz"
        ],
        "err": "",
        "out": "",
        "rc": 0
    },
    "gid": 0,
    "group": "root",
    "handler": "TgzArchive",
    "mode": "0755",
    "owner": "root",
    "secontext": "system_u:object_r:mnt_t:s0",
    "size": 35,
    "src": "/mnt/etc.tar.gz",
    "state": "directory",
    "uid": 0
}
[root@westosa mnt]# ll
total 6652
drwxr-xr-x. 131 root   root    8192 Aug 26 15:18 etc
-rwxrwxrwx.   1 westos root 6795808 Aug 26 15:13 etc.tar.gz
[root@westosa mnt]# rm -fr *

8.hostname、Cron模块

hostname模块 修改主机名称
[devops@westos_student41 ansible]$ ansible westos -m hostname -a 'name=www.westos.com'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "ansible_domain": "westos.com",
        "ansible_fqdn": "www.westos.com",
        "ansible_hostname": "www",
        "ansible_nodename": "www.westos.com",
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "www.westos.com"
}
[devops@westos_student41 ansible]$ ansible westos -m shell -a 'hostname'
172.25.41.1 | CHANGED | rc=0 >>
www.westos.com
[devops@westos_student41 ansible]$ ansible westos -m hostname -a 'name=westosa'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "ansible_domain": "",
        "ansible_fqdn": "westosa",
        "ansible_hostname": "westosa",
        "ansible_nodename": "westosa",
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "westosa"
}
[devops@westos_student41 ansible]$ ansible westos -m shell -a 'hostname'
172.25.41.1 | CHANGED | rc=0 >>
westosa
cron模块 设定定时任务

该模块适用于管理cron计划任务的。
其使用的语法跟我们的crontab文件中的语法一致。
常用命令:

    day= #日应该运行的工作( 1-31, *, */2, )
    hour= # 小时 ( 0-23, *, */2, )
    minute= #分钟( 0-59, *, */2, )
    month= # 月( 1-12, *, /2, )
    weekday= # 周 ( 0-6 for Sunday-Saturday,, )
    job= #指明运行的命令是什么
    name= #定时任务描述
    reboot # 任务在重启时运行,不建议使用,建议使用special_time
    special_time #特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
    state #指定状态,present表示添加定时任务,也是默认设置,absent表示删除定时任务
    user # 以哪个用户的身份执行

建立定时任务,每周五执行一次

[devops@westos_student41 ansible]$ ansible westos -m cron -a "job='echo hello westos' name=westoscron weekday=5"
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "westoscron"
    ]
}
[devops@westos_student41 ansible]$ ansible westos -m shell -a 'cat /var/spool/cron/root'
172.25.41.1 | CHANGED | rc=0 >>
#Ansible: westoscron
* * * * 5 echo hello westos

删除定时任务 (注释任务)

[devops@westos_student41 ansible]$ ansible westos -m cron -a "job='echo hello westos' name=westoscron weekday=5 disabled=yes"
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "westoscron"
    ]
}
[devops@westos_student41 ansible]$ ansible westos -m shell -a 'cat /var/spool/cron/root'
172.25.41.1 | CHANGED | rc=0 >>
#Ansible: westoscron
#* * * * 5 echo hello westos

9.yum_repository模块

该模块用于软件仓库的配置。

实验:配置软件仓库文件 /etc/yum.repos.d/westos.repo

删除之前存在的配置文件

[devops@westos_student41 ansible]$ ansible westos -m shell -a 'rm -fr /etc/yum.repos.d/*.repo'
[WARNING]: Consider using the file module with state=absent rather than running
'rm'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
172.25.41.1 | CHANGED | rc=0 >>
[devops@westos_student41 ansible]$ ansible westos -m yum_repository -a 'name="AppStream" baseurl=http://172.25.41.250/rhel8.2/AppStream description=AppStream file=westos enabled=yes gpgcheck=no'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "AppStream",
    "state": "present"
}
[devops@westos_student41 ansible]$ ansible westos -m yum_repository -a 'name="BaseOS" baseurl=http://172.25.41.250/rhel8.2/BaseOS description=BaseOS file=westos enabled=yes gpgcheck=no'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "BaseOS",
    "state": "present"
}

[root@westosa yum.repos.d]# ls
westos.repo
[root@westosa yum.repos.d]# cat westos.repo 
[AppStream]
baseurl = http://172.25.41.250/rhel8.2/AppStream
enabled = 1
gpgcheck = 0
name = AppStream

[BaseOS]
baseurl = http://172.25.41.250/rhel8.2/BaseOS
enabled = 1
gpgcheck = 0
name = BaseOS

10.dnf模块

该模块主要用于软件的安装。
其命令如下:

    name=  #所安装的包的名称
    state=  #present--->安装, latest--->安装最新的, absent---> 卸载软件。
    update_cache  #强制更新yum的缓存
    conf_file  #指定远程yum安装时所依赖的配置文件(安装本地已有的包)。
    disable_pgp_check  #是否禁止GPG checking,只用于presentor latest。
    disablerepo  #临时禁止使用yum库。 只用于安装或更新时。
    enablerepo  #临时使用的yum库。只用于安装或更新时。

安装httpd

[devops@westos_student41 ansible]$ ansible westos -m dnf -a 'name=httpd state=present'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
        "Installed: httpd-filesystem-2.4.37-21.module+el8.2.0+5008+cca404a3.noarch",
        "Installed: apr-1.6.3-9.el8.x86_64",
        "Installed: apr-util-1.6.1-6.el8.x86_64",
        "Installed: redhat-logos-httpd-81.1-1.el8.noarch",
        "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
        "Installed: httpd-tools-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64",
        "Installed: mod_http2-1.11.3-3.module+el8.2.0+4377+dc421495.x86_64",
        "Installed: httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64"
    ]
}

卸载httpd

[devops@westos_student41 ansible]$ ansible westos -m dnf -a 'name=httpd state=absent'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Removed: mod_http2-1.11.3-3.module+el8.2.0+4377+dc421495.x86_64",
        "Removed: httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64"
    ]
}

11.service、firewalld模块

service模块

该模块用于服务程序的管理。
其常用命令如下:

    arguments #命令行提供额外的参数
    enabled #设置开机启动。
    name= #服务名称
    runlevel #开机启动的级别,一般不用指定。
    sleep #在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)
    state #有四种状态,分别为:started--->启动服务, stopped--->停止服务, restarted--->重启服务, reloaded--->重载配置

下载httpd服务

[devops@westos_student41 ansible]$ ansible westos -m dnf -a 'name=httpd state=present'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: mod_http2-1.11.3-3.module+el8.2.0+4377+dc421495.x86_64",
        "Installed: httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64"
    ]
}

开启服务

[devops@westos_student41 ansible]$ ansible westos -m service -a 'name=httpd state=started enabled=yes'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started",
    "status": {
        "ActiveEnterTimestampMonotonic": "0",
        "ActiveExitTimestampMonotonic": "0",
......
        "UnitFileState": "disabled",
        "UtmpMode": "init",
        "Wants": "httpd-init.service",
        "WatchdogTimestampMonotonic": "0",
        "WatchdogUSec": "0"
    }
}

开启火墙,immediate=yes 立即开启

[devops@westos_student41 ansible]$ ansible westos -m firewalld -a 'service=http permanent=yes state=enabled immediate=yes'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
}

测试:编辑默认发布目录

[devops@westos_student41 ansible]$ ansible westos -m copy -a 'dest=/var/www/html/index.html content="hello westos"'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "dest": "/var/www/html/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "dde5c4df2e17f6693afdd6769dcf4e09",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:httpd_sys_content_t:s0",
    "size": 12,
    "src": "/home/devops/.ansible/tmp/ansible-tmp-1629970235.793403-20791-168836060169032/source",
    "state": "file",
    "uid": 0
}

请添加图片描述

12.user、group 模块

13.lineinfile 模块

14.replace 模块

15.setup 模块

16.debug模块

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值