二:Ansible常用模块

二:Ansible常用模块

一:Ansible命令模块

1.1 command

# 默认模块, 执行命令 [root@m01 ~]# ansible web_group -a "hostname" 

1.2 shell

# 如果需要一些管道操作,则使用shell [root@m01 ~]# ansible web_group -m shell -a "ps -ef|grep nginx" -f 50 

注意:command不识别管道符之类的操作

1.3 script

# 编写脚本

[root@m01 ~]# vim /root/yum.sh
#!/usr/bin/bash
yum install -y vsftpd

#在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
[root@m01 ~]# ansible web_group -m script -a "/root/yum.sh"

二:Ansible 软件管理模块

1.1 yum

#查看使用方法
[root@m01 ~]# ansible-doc yum
state:
present  安装软件包   默认是present,可以不写
absent   移除软件包
latest   安装最新软件包

name                            
     (包名httpd)     #本地yum源
    file://   #指定本地安装路径(yum localinstall 本地rpm包)
    http://   #指定yum源(从远程仓库获取rpm包)

[root@m01 ~]# ansible web_group -m yum -a "name=httpd state=present"

1.2 cron:定时任务

ansible web_group -m cron -a 'name="aaaaaaa" minute=0 hour=5,2 day=2 month=1 weekday=1-7 state=present job="/bin/sh /root/a.sh > /dev/null"'

name:注释
state:
    present
    absent
job:crontab要执行的命令


删除:
ansible web_group -m cron -a 'name="aaaaaaa state=absent"

分时日月周不写就直接*号

1.3 yum_repository:建YUM仓

[root@m01 ~]# ansible 'web_group' -m yum_repository -a "name=nginx description='zls_nginx' baseurl=http://nginx.org/packages/centos/7/$basearch/"


    name: 指定repo文件名
    description:描述(repo文件中name的值)
    baseurl: 指定yum源
    file:如果定义了file那么文件名以file为准,如果没有定义file文件名以name为准
    state:
        present  #创建(默认)
        absent   #删除

[root@m01 ~]# ansible 'web_group' -m yum_repository -a "name=nginx_new file='zls_nginx_new' description='zls_nginx' baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=no"

#删除
[root@m01 ~]# ansible 'web_group' -m yum_repository -a "name=nginx state=absent"
[root@m01 ~]# ansible 'web_group' -m yum_repository -a "name=nginx_new file=zls_nginx_new state=absent"
[root@m01 ~]# ansible 'web_group' -m yum_repository -a "name=nginx123 file=zls_nginx_new state=absent"

# 在已有的仓库文件中添加一个仓库
[root@m01 ~]# ansible 'web_group' -m yum_repository -a "name=nginx123 file='zls_nginx_new' description='zls_nginx' baseurl=http://download.driverzeng.com gpgcheck=no"

三:Ansible文件管理模块

1.1 file

[root@m01 ~]# ansible web_group -m file -a 'path=/tmp/zls state=directory owner=root group=root mode=0644'
    path: 指定创建的路径
    state:
        touch:创建文件
        directory:创建目录
        file:修改文件属性(默认)
        link:软连接
        absent:删除指定的文件或目录
    owner:指定属主
    group:指定属组
    mode:指定权限

1.2 get_url:下载

[root@m01 ~]# ansible web01 -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.0-1.el7.x86_64.rpm dest=/tmp mode=0644'

[root@m01 ~]# ansible web01 -m get_url -a 'url=http://test.driverzeng.com/Zabbix_File/percona-release-0.1-3.noarch.rpm dest=/tmp mode=0644 checksum=md5:ea13c36cf79e131bded48bac4f7e88c2'

url:指定软件包地址
dest:指定下载的路径
mode:指定文件的权限
checksum:
    md5:
    sha256:

1.3 unarchive:解压缩

[root@m01 ~]# ansible web02 -m unarchive -a 'src=/root/wordpress-5.0.3-zh_CN.tar.gz dest=/tmp'

src:指定压缩包的路径
dest:指定解压的位置
remote_src:
    yes:
    no:(默认)

四:Ansible服务管理模块

systemd

[root@m01 ~]# ansible web01 -m systemd -a 'name=nginx state=started'
name:指定服务名
state:
    started
    stopped
    restarted
    reloaded
enabled:
    true
    false

五:Ansible用户管理模块

1.1 user

#创建用户指定uid和gid,不创建家目录也不允许登陆
[root@m01 ~]# ansible web_group -m user -a "name=qls uid=888 group=888 shell=/sbin/nologin create_home=false"

1.2 group

[root@m01 ~]# ansible web_group -m group -a 'name=qls state=present gid=250'
    name:指定组名
    state:
        present:创建
        absent:删除
    gid:指定组的id

六:Ansible磁盘挂载模块

mount:挂载

[root@m01 ~]# ansible web01 -m mount -a 'path=/data src=172.16.1.31:/data fstype=nfs state=mounted'
path:挂载的路径
src:挂载点
fstype:挂载类型
state:
    present      # 开机挂载,仅将挂载配置写入/etc/fstab
    absent       # 卸载,并清除/etc/fstab
    mounted      # 挂载并写入/etc/fstab
    unmounted    # 卸载,不清除 /etc/fstab

七:Ansible主机信息模块

setup

为什么要讲这个模块?

做过自动化的小伙伴会觉得这个模块非常实用

在公司中总会有一些需求

比如:
1.根据不同主机不同IP创建对应IP的目录
2.根据不同主机不同主机名创建对应主机名的目录
3.自动化运维平台需要自动获取到主机的IP地址,内存信息,磁盘信息,主机名...等
4.如果安装数据库,分配内存为物理内存的80%,此时有3台不同物理内存的机器2G、4G、16G
写一个playbook的情况下,我需要获取到对应主机的内存并作出计算,写判断。

1.获取IP地址

[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_default_ipv4'
web01 | SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "10.0.0.7",
            "alias": "eth0",
            "broadcast": "10.0.0.255",
            "gateway": "10.0.0.2",
            "interface": "eth0",
            "macaddress": "00:0c:29:f8:98:80",
            "mtu": 1500,
            "netmask": "255.255.255.0",
            "network": "10.0.0.0",
            "type": "ether"
        },
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}



2.获取主机名

[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_fqdn'
web01 | SUCCESS => {
    "ansible_facts": {
        "ansible_fqdn": "web01",
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}

3.获取内存信息

[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_memory_mb'
web01 | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 1622,
                "used": 360
            },
            "real": {
                "free": 1068,
                "total": 1982,
                "used": 914
            },
            "swap": {
                "cached": 0,
                "free": 1023,
                "total": 1023,
                "used": 0
            }
        },
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}


4.其他信息参数

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个数(只显示总的个数)。

八:Ansible防火墙模块

1.1 selinux

[root@m01 ~]# ansible web01 -m selinux -a 'state=disabled'

1.2 firewalld

[root@m01 ~]# ansible web01 -m firewalld -a 'port=80/tcp state=enabled'

九:Ansible 数据库模块

1.1 mysql_db:创建数据库

- name: Create a new database with name 'jiangwei'
  mysql_db:
    name: jiangwei
    state: present
此时,遇到一个问题,playbook建库时,需要先登陆到数据库,登陆到数据库这个动作必须要自动完成,此时引入:
- login_host
        Host running the database.
        [Default: localhost]
        type: str

- login_password
        The password used to authenticate with.
        [Default: (null)]
        type: str

- login_port
        Port of the MySQL server. Requires `login_host' be defined as
        other than localhost if login_port is used.
        [Default: 3306]
        type: int

- login_user
        The username used to authenticate with.
        [Default: (null)]
        type: str

EX:
- name: Create a new database with name 'jiangwei'
  mysql_db:
    login_user: root
    login_password: '123'
    login_host: localhost
    name: jiangwei
    state: present

1.2 mysql_user:创建数据库用户

- name: Create database user with password and all database privileges =
  mysql_user:
    name: bob
    password: 12345
    priv: '*.*:ALL'
    state: present

EX:
- name: Create MySQL User
       mysql_user:
         name: wordpress
         password: '123456'
         priv: '*.*:ALL'
         host: '%'
         state: present

猜你喜欢

转载自www.cnblogs.com/captain-jiang/p/12078537.html