• Ansible 模块


    一、命令类型模块

    1.command 模块

    #默认模块,远程执行命令,可忽略-m选项
    [root@m01 ~]# ansible web01 -m command -a 'free -m'
    web01 | CHANGED | rc=0 >>
                  total        used        free      shared  buff/cache   available
    Mem:            972         128         479           7         364         658
    Swap:          1023           0        1023
    
    #不支持特殊字符
    [root@m01 ~]# ansible web01 -m command -a "ifconfig eth0 | awk 'NR==2 {print $2}'"
    web01 | FAILED | rc=1 >>
    |: Unknown host
    ifconfig: `--help' gives usage information.non-zero return code
    

    2.shell 万能模块

    [root@m01 ~]# ansible web01 -m shell -a 'free -m'
    web01 | CHANGED | rc=0 >>
                  total        used        free      shared  buff/cache   available
    Mem:            972         128         479           7         364         658
    Swap:          1023           0        1023
    
    #支持特殊字符
    [root@m01 ~]# ansible web01 -m shell -a "ifconfig eth0 | awk 'NR==2 {print $2}'"
    web01 | CHANGED | rc=0 >>
    192.168.15.7
    

    在节点上执行操作

    所以基本上在命令行可以执行的命令,用shell模块都可以执行

    注意:调用bash执行命令 类似 cat /tmp/test.md | awk -F'|' '{print $1,$2}' &> /tmp/example.txt 这些复杂命令,即使使用shell也可能会失败。

    解决办法:写到脚本时,copy到远程,执行,再把需要的结果拉回执行命令的机器。

    将shell模块代替command,设为默认模块

    [root@m01 ~]# vim /etc/ansible/ansible.cfg
    module_name = shell
    

    3.script 运行shell脚本 模块

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

    [root@m01 ~]# vim mkdir.sh     # 脚本必须放在ansible执行机器上面
    #!/bin/bash
    mkdir /dir
    
    #远程执行脚本
    [root@m01 ~]# ansible web_group -m script -a 'mkdir.sh'     # 最好用绝对路径
    
    #查看
    [root@m01 ~]# ansible web_group -m shell -a 'ls -ld /dir'
    web01 | CHANGED | rc=0 >>
    drwxr-xr-x. 2 root root 6 Dec 18 08:40 /dir
    web02 | CHANGED | rc=0 >>
    drwxr-xr-x 2 root root 6 Dec 18 08:40 /dir
    

    二、软件管理模块

    1.yum安装软件 和apt模块

    yum 管理软件包,使用非常简单,省去编写脚本。安装只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本。

    apt 模块管理 Debian 相关版本的软件包。

    参数 解释
    name 安装的服务的名字
    state 执行命令 present installed removed latest absent 其中installed and present等效 latest标志安装yum中最新版本,absent and removed 等效 表示删除安装包
    disable_gpg_check 用于禁用rmp包的公钥gpg验证,默认值no 表示不做验证
    enablerepo 用于指定安装软件包是临时启用的yum源
    disablerepo 用于指定安装软件包是临时禁止用的yum源
    [root@m01 ~]# ansible-doc yum
    EXAMPLES:
    - name: install the latest version of Apache
      yum:
        name: httpd,vsfpd
        state: latest
        
    name:
    	httpd					   #服务名字
    	file					   #软件包的名字
    	http://nginx.org/...		#软件包在网上的地址
    	/usr/local/src/nginx-release-centos-6.0.rpm #本地的rpm包
    state:
    	latest		#安装最新版本的包
    	absent		#卸载软件包
    	present		#安装软件包
    	
    #直接yum安装服务httpd
    [root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=present'
    相当于在远程机器上执行:yum install -y httpd
    
    #安装本地的rpm包(包一定先传到远程机器上)***最好用内部软件包安装。会少很多bug
    [root@m01 ~]# ansible web_group -m yum -a 'name=/tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm state=present'
    相当于在远程机器上执行:yum localinstall -y /tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm
    
    #使用网上软件包安装
    [root@m01 ~]# ansible web_group -m yum -a 'name=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm state=present'
    相当于在远程机器上执行:yum install -y https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm
    
    #卸载服务包 
    [root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=absent'
    

    2.yum_repository 配置yum仓库 模块

    [root@m01 ~]# ansible-doc yum_repository
    EXAMPLES:
    - name: Add repository
      yum_repository:
        description: EPEL YUM repo
        baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
        file: external_repos
        enabled: no
        state: absent
    #参数介绍
    name			
    description		#yum源中 name 部分的内容
    baseurl			#yum源中 yum 仓库的地址
    gpgcheck		#yum源中 gpgcheck,是否检查yum源
    enabled			#yum源中的 enabled,是否开启yum源
    file			#yum源的文件名字
    
    #添加yum源
    [root@m01 ~]# ansible web_group -m yum_repository -a 'name=nginx.repo description="nginx stable repo" baseurl="http://nginx.org/packages/centos/7/$basearch/" enabled=1 gpgcheck=1 file=nginx state=present'
    
    #查看添加的yum源
    [root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
    [nginx.repo]
    baseurl = http://nginx.org/packages/centos/7/$basearch/
    enabled = 1
    gpgcheck = 1
    name = nginx stable repo
    
    
    #注意:
    	1.file参数不修改,name参数也不修改的前提下,是修改yum源
    	2.file参数不修改,name参数修改的情况下,是添加yum源
    

    三、文件管理模块

    1.copy 批量分发文件 模块

    功能:从ansible服务器主控端复制文件到远程主机

    1)语法

    [root@m01 ~]# ansible-doc copy
    EXAMPLES:
    - name: Copy file with owner and permissions
      copy:
        src: /srv/myfiles/foo.conf        #文件的源路径(在控制端)
        dest: /etc/foo.conf          #目标地址或文件的路径(在受控端)
        owner: foo             #文件推过去之后属主
        group: foo             #文件推过去之后属组
        mode: '0644'           #文件推过去之后的权限
        backup: yes         #替换的文件是否备份
        content: '# This file was moved to /etc/other.conf' #直接将内容写入文件
        follow: yes          #是否识别软连接
    
    参数 解释
    src 用于指定需要copy的文件或目录。
    dest 用于指定文件将被拷贝到远程主机的哪个目录中,dest为必须参数。
    content 当不使用src指定拷贝的文件时,可以使用content直接指定文件内容,src与content两个参数必有其一,否则会报错。
    force 当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否强制覆盖,可选值有yes和no,默认值为yes,表示覆盖,如果设置为no,则不会执行覆盖拷贝操作,远程主机中的文件保持不变。
    backup 当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否对远程主机的文件进行备份,可选值有yes和no,当设置为yes时,会先备份远程主机中的文件,然后再将ansible主机中的文件拷贝到远程主机。
    owner 指定文件拷贝到远程主机后的属主,但是远程主机上必须有对应的用户,否则会报错。
    group 指定文件拷贝到远程主机后的属组,但是远程主机上必须有对应的组,否则会报错。
    mode 指定文件拷贝到远程主机后的权限,如果你想将权限设置为”rw-r--r--“,则可以使用mode=0644表示,如果你想要在user对应的权限位上添加执行权限,则可以使用mode=u+x表示。

    2)例子

    #推送yum源文件到远端
    [root@m01 ~]# ansible web_group -m copy -a 'src=/root/nginx.repo dest=/etc/yum.repos.d/'
    
    #推送rsync密码文件授权
    [root@m01 ~]# ansible web_group -m copy -a 'src=/root/1.txt dest=/etc/rsync.password mode=600'
    
    #推送站点文件并授权属主属组
    [root@m01 ~]# ansible web_group -m copy -a 'src=/root/index.html dest=/root/ owner=adm group=adm'
    
    #推送文件并备份
    [root@m01 ~]# ansible web_group -m copy -a 'src=/root/index.html dest=/root/ owner=adm group=adm backup=yes'
    注意:buckup参数不是用来回滚的,需要回滚的话要备份原来m01上推送的文件
    
    #直接将内容写入文件
    [root@m01 ~]# ansible web_group -m copy -a 'content="rsync_backup:123456" dest=/tmp/rsync.password mode=600'
    

    2.file 建立目录或文件 模块

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

    1)语法和参数

    [root@m01 ~]# ansible-doc file
    EXAMPLES:
    - name: Change file ownership, group and permissions
      file:
      	src: /file/to/link/to   #源文件(如果做软链接就是远程机器上的文件)
        path: /etc/foo.conf   #创建的文件或目录的地址
        owner: foo          #文件或目录的属主
        group: foo        #文件或目录的属组
        mode: '0644'    #文件或目录的权限
        state:      #可以批量管理受控端增删
        	   link         #创建软链接
        	   touch		#创建文件
    	       directory	#创建目录
    	       absent		#删除
        recurse: yes       #递归操作	
    
    参数 解释
    path 必须参数,用于指定要操作的文件或目录,在之前版本的ansible中,使用dest参数或者name参数指定要操作的文件或目录,为了兼容之前的版本,使用dest或name也可以。
    recurse 当要操作的文件为目录,将recurse设置为yes,可以递归的修改目录中文件的属性。
    state 此参数非常灵活,其对应的值需要根据情况设定。 directory:在远端创建目录 touch:在远端创建文件 link:创建链接文件 absent:表示删除文件或目录 mode:设置文件或目录权限 owner:设置文件或目录属主 group:设置文件或目录属组
    src 当state设置为link或者hard时,表示我们想要创建一个软链或者硬链,所以,我们必须指明软链或硬链链接的哪个文件,通过src参数即可指定链接源。
    force 当state=link的时候,可配合此参数强制创建链接文件,当force=yes时,表示强制创建链接文件。不过强制创建链接文件分为三种情况。情况一:当要创建的链接文件指向的源文件并不存在时,使用此参数,可以先强制创建出链接文件。情况二:当要创建链接文件的目录中已经存在与链接文件同名的文件时,将force设置为yes,会将同名文件覆盖为链接文件,相当于删除同名文件,创建链接文件。情况三:当要创建链接文件的目录中已经存在与链接文件同名的文件,并且链接文件指向的源文件也不存在,这时会强制替换同名文件为链接文件。
    owner 用于指定被操作文件的属主,属主对应的用户必须在远程主机中存在,否则会报错。
    group 用于指定被操作文件的属组,属组对应的组必须在远程主机中存在,否则会报错。
    mode 用于指定被操作文件的权限,比如,如果想要将文件权限设置为”rw-r-x---“,则可以使用mode=650进行设置,或者使用mode=0650,效果也是相同的。如果想要设置特殊权限,比如为二进制文件设置suid,则可以使用mode=4700。

    2)实例

    #单纯的创建目录
    [root@m01 ~]# ansible web01 -m file -a 'path=/code state=directory'
    相当于在远程机器上执行:mkdir /code
    
    #创建目录并授权
    [root@m01 ~]# ansible web01 -m file -a 'path=/code state=directory owner=nginx group=nginx mode=755'
    相当于在远程机器上执行:mkdir /code && chown nginx.nginx /code && chmod 755 /code
    
    #递归创建目录,不需要加任何参数
    [root@m01 ~]# ansible web01 -m file -a 'path=/code/wordpress/wp-content/pic state=directory'
    
    #递归授权目录
    [root@m01 ~]# ansible web01 -m file -a 'path=/code/ state=directory owner=nginx group=nginx mode=755 recurse=yes'
    
    #以利用模块删除数据信息
    [root@m01 ~]# ansible web01 -m file -a "dest=/dan/dan.txt state=absent"
    
    #注意:
    	1.当创建的目录都不存在时,递归创建会递归授权
    	2.当创建的上层目录已经存在,递归授权只授权最后一层目录下的内容
    	3.递归授权需加参数会授权齐全recurse=yes
    	
    #创建文件。创建文件时,上层目录必须存在
    [root@m01 ~]# ansible web01 -m file -a 'path=/tmp/1.txt state=touch'
    
    #创建文件并授权
    [root@m01 ~]# ansible web01 -m file -a 'path=/tmp/1.txt state=touch owner=nginx group=nginx'
    
    #创建软链接
    [root@m01 ~]# ansible web01 -m file -a 'src=/tmp/1.txt  state=link'
    
    #删除文件
    [root@m01 ~]# ansible web01 -m file -a 'path=/tmp/1.txt state=absent'
    

    3. get_url 下载软件 模块

    1)语法和参数

    [root@m01 ~]# ansible-doc get_url
    EXAMPLES:
    - name: Download foo.conf
      get_url:   
        url: http://example.com/path/file.conf  #文件下载地址
        dest: /etc/foo.conf   #文件存放路径
        mode: '0440'  	#文件下载后授权
        checksum: sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c #验证文件 : 加密方式
    
    参数 解释
    url 下载的URL
    mode 权限
    dest 下载的路径
    checksum md5值
    timeout 下载文件的超时时间
    url_password URL密码
    url_username URL用户

    2)实例

    #下载网站上的文件
    [root@m01 ~]# ansible web01 -m get_url -a 'url=https://repo.huaweicloud.com/etcd/v3.4.0-rc.4/etcd-v3.4.0-rc.4-linux-arm64.tar.gz mode=777  dest=/tmp'
    
    #下载时验证文件
    [root@m01 ~]# ansible web01 -m get_url -a 'url=http://192.168.15.7/1.txt dest=/tmp checksum=md5:d8e8fca2dc0f896fd7cb4cb0031ba249'
    

    4.fetch 批量拉取数据 模块

    功能:从远程主机提取文件至ansible的主控端,copy相反,目前不支持目录

    参数 解释
    src (必需)要获取的远程系统上的文件。
    dest (必需)要将文件保存到的目录。
    fail_on_missing 当设置为 Trueyes,如果由于不明因素导致从远程主机中不能读取到该文件,则会显示 fail
    validate_checksum 当文件fetch之后进行md5检查 **
    Flat 允许覆盖默认行为从hostname/path到/file的,如果dest以/结尾,它将使用源文件的基础名称
    示例1
    [root@m01 ~]# ansible public -m fetch -a "src=/tmp/push_ssh_key.sh dest=shanhe.sh "
    
    [root@m01 ~]# tree shanhe.sh
    /root/shanhe.sh/
    └── 106.13.81.75
        └── tmp
            └── push_ssh_key.sh
    
    2 directories, 1 file
    
    示例2
    [root@m01 ~]# ansible public -m fetch -a "src=/tmp/push_ssh_key.sh dest=/opt validate_checksum=yes"
    

    四、Ansible 服务管理模块

    1.service 启动与停止服务 模块

    [root@m01 ~]# ansible-doc service
    EXAMPLES:
    - name: Start service httpd, if not started
      service:
        name: httpd
        state: started
        
    name: nginx			#服务名字
    state:
    	started			#启动服务
    	stopped			#停止服务
    	restarted		#重启服务
    	reloaded		#重载服务
    enabled: yes		#开机自启
    
    示例
    #1.启动crond服务,并加入开机自启
    [root@m01 ~]# ansible webservers -m service -a "name=crond state=started enabled=yes"
    #2.停止crond服务,并删除开机自启
    [root@m01 ~]# ansible webservers -m service -a "name=crond state=stopped enabled=no"
    #3.重启crond服务
    [root@m01 ~]# ansible webservers -m service -a "name=crond state=restarted"
    #4.重载crond服务
    [root@m01 ~]# ansible webservers -m service -a "name=crond state=reloaded"
    

    2.systemd 启动与停止服务 模块

    1)语法和参数

    [root@m01 ~]# ansible-doc systemd
    EXAMPLES:
    - name: Make sure a service is running
      systemd:
        state: started
        name: httpd
    
    name: nginx			#服务名字
    state:
    	started			#启动服务
    	stopped			#停止服务
    	restarted		#重启服务
    	reloaded		#重载服务
    enabled: yes		#开机自启
    

    2)实例

    #关闭服务
    [root@m01 ~]# ansible web01 -m systemd -a 'name=nginx state=stopped'
    
    #启动服务
    [root@m01 ~]# ansible web01 -m systemd -a 'name=nginx state=started'
    

    3.hostname 管理主机名模块

    参数 解释
    name 主机名
    示例
    [root@m01 ~]# ansible public -m hostname -a 'name=kubernetes'
    [root@m01 ~]# bash
    [root@kubernetes ~]# 
    

    五、用户管理模块

    1.group 管理组 模块

    1)语法和参数

    [root@m01 ~]# ansible-doc group
    EXAMPLES:
    - name: Ensure group "somegroup" exists
      group:
        name: somegroup			#组名字
        state: 
        	present				#创建组
            absent				#删除组
        gid: 666				#指定组id
    

    2)实例

    #创建用户组
    [root@m01 ~]# ansible web01 -m group -a 'name=www gid=666 state=present'
    
    #修改用户组
    [root@m01 ~]# ansible web01 -m group -a 'name=www gid=666 state=absent'
    

    2.user 管理用户 模块

    1)语法和参数

    [root@m01 ~]# ansible-doc user
    EXAMPLES:
    - name: Add the user 'johnd' with a specific uid and a primary group of 'admin'
      user:
        name: johnd  #用户名字
        comment: John Doe   #用户备注
        uid: 1040    #用户id
        group: admin   #用户所在的组名字
        shell: /bin/bash  #用户可以登录
       		   /sbin/nologin	#用户不需要登录
       	state: absent  #删除用户
       		   present		#创建用户
        remove: yes   #移除家目录
        create_home: false  #不创建家目录
        		     true	#创建家目录	
    
    参数 解释
    uid 指定用户的uid
    group 指定用户组名称
    groups 指定附加组名称
    password 给用户添加密码(记得单引号)
    shell 指定用户登录shell
    create_home 是否创建家目录
    state 用户状态:absent(删除)
    home 家目录路径
    system 是否是系统用户
    remove 是否删除家目录数据:yes(删除)

    2)实例

    #创建用户,不需要登录,有家目录
    [root@m01 ~]# ansible web01 -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=true'
    
    #删除用户并移除家目录
    [root@m01 ~]# ansible web01 -m user -a 'name=www state=absent remove=yes'
    
    #注意:
    	1.当组的名字与用户名字相同时,删除用户组也会被删除
    	2.当组的名字与用户名字相同时,而组下面还有其他用户,则删除用户时不会删除同名组
    

    六、系统管理模块

    1.cron 定时任务模块

    1)语法和参数

    [root@m01 ~]# ansible-doc cron
    EXAMPLES:
    - name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /dev/null"
      cron:
        name: "check dirs"   #定时任务的备注
        minute: "0"     #分钟
        hour: "5,2"     #小时
        day: "*"   	    #日
        month: "*"    	#月
        weekday: "*"    	 #周
        job: "ls -alh > /dev/null"    	#指定的定时任务内容
        state:
          present	  #新建定时任务
          absent        #删除定时任务
        disabled: 
          yes   	#注释定时任务
          	no		#取消注释
    

    2)实例

    #添加定时任务,每五分钟执行一次时间同步(只配置分钟,其他不配置默认是 * )
    [root@m01 ~]# ansible web01 -m cron -a 'name="测试ansible配置定时任务" minute=*/5 job="ntpdate time1.aliyun.com"'
    #查看配置
    [root@web01 html]# crontab -l
    #Ansible: 测试ansible配置定时任务
    */5 * * * * ntpdate time1.aliyun.com
    
    #注释定时任务
    [root@m01 ~]# ansible web01 -m cron -a 'name="测试ansible配置定时任务" minute=*/5 job="ntpdate time1.aliyun.com" disabled=yes'
    
    #删除定时任务
    [root@m01 ~]# ansible web01 -m cron -a 'name="测试ansible配置定时任务" minute=*/5 job="ntpdate time1.aliyun.com" state=absent'
    
    #注意:
    	1.定时任务添加,是通过名字来区分是否一样的,如果不加name参数,则会添加重复的定时任务
    	2.定时任务注释是通过 name 参数来注释的,所以一定要加 name 参数
    	3.定时任务删除是通过 name 参数来删除的,所以一定要加 name 参数
    

    2.mount 挂载模块

    • 参数
    参数 解释
    src 挂载IP及目录
    path 挂载的路径
    fstype 挂载的类型
    opts
    state 挂载的状态
    • state参数
    参数 解释
    present 开机挂载,仅将挂载配置写入/etc/fstab
    mounted 挂载设备,并将配置写入/etc/fstab
    unmounted 卸载设备,不会清除/etc/fstab写入的配置
    absent 卸载设备,会清理/etc/fstab写入的配置

    1)安装NFS

    1.安装nfs
    [root@m01 ~]# ansible nfs_server -m yum -a 'name=nfs-utils state=present'
    
    2.配置nfs
    [root@m01 ~]# ansible nfs_server -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash)" dest=/etc/exports'
    
    3.创建目录
    [root@m01 ~]# ansible nfs_server -m file -a 'path=/data state=directory owner=nfsnobody group=nfsnobody'
    
    4.启动服务
    [root@m01 ~]# ansible nfs_server -m systemd -a 'name=nfs state=started'
    

    2)挂载模块语法和参数

    [root@m01 ~]# ansible-doc mount
    EXAMPLES:
    - name: Mount DVD read-only
      mount:
        path: /mnt/dvd
        src: /dev/sr0
        fstype: iso9660
        opts: ro,noauto
        state: present
        
    path		#本机准备挂载的目录
    src			#远端挂载点
    fstype		#指定挂载类型
    opts		#挂载参数(/etc/fstab中的内容)
    state
    	present		#配置开机挂载,将配置写入自动挂载文件,并没有直接挂载
    	unmounted	#取消挂载,但是没有删除自动挂载配置
    	#常用配置
    	mounted		#配置开机挂载,并且直接挂载上
    	absent		#取消挂载,并且删除自动挂载配置
    

    3)实例

    #配置开机挂载,将配置写入自动挂载文件,并没有直接挂载
    [root@m01 ~]# ansible web01 -m mount -a 'path=/data src=172.16.1.31:/data fstype=nfs opts=defaults state=present'
    
    #配置开机挂载,并且直接挂载上
    [root@m01 ~]# ansible web01 -m mount -a 'path=/data src=172.16.1.31:/data fstype=nfs opts=defaults state=mounted'
    
    #取消挂载,但是没有删除自动挂载配置
    [root@m01 ~]# ansible web01 -m mount -a 'path=/data src=172.16.1.31:/data fstype=nfs opts=defaults state=unmounted'
    
    #取消挂载,并且删除自动挂载配置
    [root@m01 ~]# ansible web01 -m mount -a 'path=/data src=172.16.1.31:/data fstype=nfs opts=defaults state=absent
    

    3.selinux 模块

    - name: Disable SELinux
      selinux:
        state: disabled
    
    #关闭selinux
    [root@m01 ~]# ansible web02 -m selinux -a 'state=disabled'
    web02 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "configfile": "/etc/selinux/config", 
        "msg": "", 
        "policy": "targeted", 
        "reboot_required": false, 
        "state": "disabled"
    }
    

    4.firewalld 防火墙 模块

    1)语法和参数

    [root@m01 ~]# ansible-doc firewalld
    EXAMPLES:
    - firewalld:
        service: https	#指定服务
        permanent: 				#是否永久生效
        	yes					#永久生效
        	no					#临时生效
        state: 
        	enabled			    #允许通过
        port: 
        	8081/tcp			#指定端口
        zone: dmz				#指定区域
        rich_rule: rule service name="ftp" audit limit value="1/m" accept		#配置富规则
        source: 192.0.2.0/24	 #指定网段
        interface: eth2			#帮定网卡
        masquerade: yes			#开启IP伪装
    

    2)实例

    #允许访问http服务,永久生效
    [root@m01 ~]# ansible web01 -m firewalld -a 'service=http permanent=yes state=enabled'
    [root@m01 ~]# ansible web01 -m firewalld -a 'service=http state=enabled'
    
    #允许访问80端口,临时生效
    [root@m01 ~]# ansible web01 -m firewalld -a 'port=80/tcp state=enabled'
    
    #配置允许10.0.0.0网段访问22端口
    [root@m01 ~]# ansible web01 -m firewalld -a 'rich_rule=rule family=ipv4 source address=10.0.0.0/24 port port=22 protocol=tcp accept" state=enabled'
    
    #配置网段白名单
    [root@m01 ~]# ansible web01 -m firewalld -a 'source=10.0.0.0/24 zone=trusted state=enabled permanent=yes'
    [root@m01 ~]# ansible web01 -m firewalld -a 'source=10.0.0.0/24 zone=trusted state=enabled permanent=no'
    
    示例1
    #解压包到受控端,包在控制端上
    [root@m01 ~]# ansible all -m unarchive -a "src=/root/test.tar.gz dest=/opt "
    
    [root@m01 ~]# ll /opt/
    total 1448
    -rw-------   1 root  root      808 May 19 15:09 hello.yaml
    
    #在受控端解压包,包在受控端
    [root@m01 ~]# ansible web01 -m unarchive -a 'src=/tmp/php.tar.gz dest=/tmp remote_src=yes'
    
    示例2
    [root@m01 ~]# ansible public -m unarchive -a "src=https://repo.huaweicloud.com/etcd/v3.2.24/etcd-v3.2.24-linux-arm64.tar.gz dest=/opt copy=no owner=shanhe group=yang"
    

    5.unarchive 解压缩 模块

    实现有两种用法:

    1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes

    2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no

    参数 解释
    copy 默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上,如果设置为no,则是在远程主机上直接解压文件
    remote_src 和copy功能一样且互斥,yes表示在远程主机,不在ansible主机,no表示文件在ansible主机上
    src 源路径,可以是ansible主机上的路径,也可以是远程主机(被管理端或者第三方主机)上的路径,如果是远程主机上的路径,则需要设copy=no
    dest 远程主机上的目标路径
    mode 设置解压缩后的文件权限
    示例1
    [root@m01 ~]# ansible all -m unarchive -a "src=/root/test.tar.gz dest=/opt "
    
    [root@m01 ~]# ll /opt/
    total 1448
    -rw-------   1 root  root      808 May 19 15:09 hello.yaml
    
    示例2
    [root@m01 ~]# ansible public -m unarchive -a "src=https://repo.huaweicloud.com/etcd/v3.2.24/etcd-v3.2.24-linux-arm64.tar.gz dest=/opt copy=no owner=shanhe group=yang"
    

    6.archive 压缩模块

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

    参数 解释
    path 要压缩的文件或目录
    dest 压缩后的文件
    format 指定打包的类型
    mode 权限
    group 属组
    owner 属主
    [root@m01 ~]# ansible-doc archive
    EXAMPLES:
    - name: Compress directory /path/to/foo/ into /path/to/foo.tgz
      archive:
        path: /path/to/foo				#要打包的内容
        dest: /path/to/foo.tgz			#打好的包与存放位置
        format:gz					  #打包的类型 bz2, gz, tar, xz, zip
    
    #打包实例
    [root@m01 ~]# ansible web01 -m archive -a 'path=/tmp dest=/opt/php.tar.gz format=gz'
    

    7.setup 模块

    功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机较多,会影响执行速度,可以使用 gather_facts: no 来禁止 Ansible 收集 facts 信息。

    ansible all -m setup -a "filter=ansible_nodename"
    ansible all -m setup -a "filter=ansible_hostname"
    ansible all -m setup -a "filter=ansible_domain"
    ansible all -m setup -a "filter=ansible_memtotal_mb"
    ansible all -m setup -a "filter=ansible_memory_mb"
    ansible all -m setup -a "filter=ansible_memfree_mb"
    ansible all -m setup -a "filter=ansible_os_family"
    ansible all -m setup -a "filter=ansible_distribution_major_version"
    ansible all -m setup -a "filter=ansible_distribution_version"
    ansible all -m setup -a "filter=ansible_processor_vcpus"
    ansible all -m setup -a "filter=ansible_all_ipv4_addresses"
    ansible all -m setup -a "filter=ansible_architecture"
    ansible all -m setup -a "filter=ansible_processor*"
    
    #1.获取web01主机所有信息
    [root@m01 ~]# ansible web01 -m setup
    
    #2.获取主机IP
    [root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_default_ipv4'
    
    #3.获取主机名
    [root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_fqdn'
    web01 | SUCCESS => {
        "ansible_facts": {
            "ansible_fqdn": "www.baidu.com", 
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false
    }
    
    #4.获取内存信息
    [root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_memory_mb'
    web01 | SUCCESS => {
        "ansible_facts": {
            "ansible_memory_mb": {
                "nocache": {
                    "free": 720, 
                    "used": 252
                }, 
                "real": {
                    "free": 276, 
                    "total": 972, 
                    "used": 696
                }, 
                "swap": {
                    "cached": 0, 
                    "free": 1023, 
                    "total": 1023, 
                    "used": 0
                }
            }, 
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false
    }
    
    #5.常用参数
    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_fqdn:仅显示主机名。
    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个数(只显示总的个数)。
    

    8.mysql模块

    #1.mysql_db 模块
    - name: Create a new database with name 'bobdata'
      mysql_db:
        name: bobdata			#库的名字
        state: 
        	present				#创建库
        	import				#导入数据库
        	dump				#导出数据库
        target: /tmp/dump.sql	  #导入或导出的数据库文件
        
    #2.mysql_user 模块
    - name: Create database user with name 'bob' and password '12345' with all database privileges
      mysql_user:
        name: bob			#用户名
        host: 172.16.1.%	#用户连接的主机
        password: 12345		#用户密码
        priv: '*.*:ALL'		#用户权限
        state: 
        	present			#创建用户
        	absent			#删除用户
        
    grant all privileges on *.* to bob@'172.16.1.%' identified by '12345'
    

    9.debug模块(调试)

    参数 msg:显示ansible模块执行后的结果

    七、使用模块加ad-hoc搭建y页面

    1.准备文件

    1)准备httpd配置文件

    [root@m01 ~]# yum install -y httpd
    [root@m01 ~]# vim /etc/httpd/conf/httpd.conf
    User www
    Group www
    

    2)准备php安装包

    [root@m01 ~]# ll
    -rw-r--r--  1 root root 19889622 Nov 22 15:52 php.tar.gz
    

    3)准备PHP配置文件

    [root@m01 ~]# tar xf php.tar.gz -C /tmp/
    [root@m01 tmp]# yum localinstall -y *.rpm
    [root@m01 tmp]# vim /etc/php-fpm.d/www.conf
    user = www
    group = www
    [root@m01 tmp]# vim /etc/php.ini
    upload_max_filesize = 200M
    post_max_size = 200M
    

    4)准备代码文件

    [root@m01 ~]# ll kaoshi.zip 
    -rw-r--r-- 1 root root 26995 Nov 22 16:47 kaoshi.zip
    

    2.编写ansible命令

    #1.安装httpd
    ansible web_group -m yum -a 'name=httpd state=present' &&
    #2.创建www用户组
    ansible web_group,nfs -m group -a 'name=www gid=666 state=present' &&
    #3.创建www用户
    ansible web_group,nfs -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=false' &&
    #4.配置httpd
    ansible web_group -m copy -a 'src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/' &&
    #5.解压php安装包到web服务器
    ansible web_group -m unarchive -a 'src=/root/php.tar.gz dest=/tmp/' &&
    #6.安装php
    ansible web_group -m shell -a 'yum localinstall -y /tmp/*.rpm' &&
    #7.配置php
    ansible web_group -m copy -a 'src=/etc/php-fpm.d/www.conf dest=/etc/php-fpm.d/' &&
    ansible web_group -m copy -a 'src=/etc/php.ini dest=/etc/' &&
    #8.启动php
    ansible web_group -m systemd -a 'name=php-fpm state=started enabled=yes' &&
    #9.启动httpd
    ansible web_group -m systemd -a 'name=httpd state=started enabled=yes' &&
    #10.解压代码
    ansible web_group -m unarchive -a 'src=/root/kaoshi.zip dest=/var/www/html/ owner=www group=www' &&
    #11.站点目录授权
    ansible web_group -m file -a 'path=/var/www/ state=directory owner=www group=www recurse=yes' &&
    #12.安装NFS
    ansible nfs -m yum -a 'name=nfs-utils state=present' &&
    #13.安装rpcbind
    ansible web_group,nfs -m yum -a 'name=rpcbind state=present' &&
    #14.配置nfs
    ansible nfs -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports' &&
    #15.创建挂载目录
    ansible nfs -m file -a 'path=/data state=directory owner=www group=www' &&
    #16.启动nfs
    ansible nfs -m systemd -a 'name=nfs state=started' &&
    #17.启动rpcbind
    ansible nfs -m systemd -a 'name=rpcbind state=started' &&
    #18.创建web端挂载的目录
    ansible web_group -m file -a 'path=/var/www/html/upload state=directory owner=www group=www' &&
    #19.挂载
    ansible web_group -m mount -a 'src=172.16.1.31:/data path=/var/www/html/upload ftype=nfs opts=defaults state=mounted'
    
  • 相关阅读:
    【学习笔记】QT下载地址
    【学习笔记】激活码方式注册的实现原理述
    【学习笔记】Inputs on an Embedded Linux Device
    【学习笔记】TSlib校准原理
    【学习笔记】13.5 使用者的特殊 shell 与 PAM 模块
    【学习笔记】Linux GDM
    【学习笔记】MC.exe 生成H和RC文件
    【工具】时间差计算器
    【学习笔记】Android 学习(一)
    【学习笔记】tensor flow 2.4 moist 数据下载错误
  • 原文地址:https://www.cnblogs.com/caodan01/p/14859808.html
Copyright © 2020-2023  润新知