目录
安装Ansible
查看Ansible版本
定义Ansible组
测试Ansible Command&shell
Group 模块
User 模块
复制模块(copy/fetch)
File模块
References
Ansible是一种自动化的运维工具,基于Python开发,它集合了众多运维工具(比如puppet、chef、func等)的优点,能够实现批量操作。但其实Ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是Ansible所运行的模块,Ansible只是提供一种框架。
安装Ansible
yum install epel-release
yum repolist# 若安装失败 则依次安装依赖
yum install -y ansible
查看Ansible版本
ansible --versionansible 2.9.10config file = /etc/ansible/ansible.cfgconfigured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python2.7/site-packages/ansibleexecutable location = /usr/bin/ansiblepython version = 2.7.5 (default, Apr 2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
定义Ansible组
修改 /etc/ansible/hosts 文件中内容 ,添加组,如下:
vi /etc/ansible/hosts
# 接下来定义Ansible模块# ansible_ssh_port 连接目标远程主机的端口
# ansible_ssh_user 连接目标远程主机默认用户
# ansible_ssh_pass 连接目标远程主机默认用户密码# 定义组 - bdp[bdp]
192.168.254.121 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
192.168.254.122 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
192.168.254.123 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
192.168.254.124 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
192.168.254.125 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
192.168.254.126 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
# 修改该文件中 host_key_checking = False (避免SSH_KEY问题)vi /etc/ansible/ansible.cfg
测试Ansible Command&shell
ansible bdp -m command -a "free -m"
ansible bdp -m command -a "cat /etc/sysconfig/network-scripts/ifcfg-ens33"ansible bdp -m shell -a "cat /etc/sysconfig/network-scripts/ifcfg-ens33"
接下来学习下基于Ansible批量创建用户与用户组
Group 模块
Group 模块主要用来添加或者删除组。
ansible-doc -s group- name: Add or remove groupsgroup:gid: # Optional `GID' to set for the group.local: # Forces the use of "local" command alternatives on platforms that implement it. This is useful in environments that use centralized authentication when you want to manipulate the local groups. (e.g. it uses `lgroupadd' instead of`groupadd'). This requires that these commands exist on the targeted host, otherwise it will be a fatal error.name: # (required) Name of the group to manage.non_unique: # This option allows to change the group ID to a non-unique value. Requires `gid'. Not supported on macOS or BusyBox distributions.state: # Whether the group should be present or not on the remote host.system: # If `yes', indicates that the group created is a system group.# name 设置group组名,string类型,必填项
# gid 设定用户组gid,int类型,默认值为空
# state state用于指定用户组在远程主机上是否被更改或删除,string类型。有两个选项:absent,present。默认值为present添加组,absent为删除组。
# system 指定创建的用户组是否为系统组,布尔类型,可用选项false,true,默认为false。
# 测试示例
[root@node1 local]# ansible bdp -m group -a 'name=hadoop gid=666 state=present system=yes'
192.168.254.125 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 666, "name": "hadoop", "state": "present", "system": true
}
192.168.254.123 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 666, "name": "hadoop", "state": "present", "system": true
}
......
# 查看新建的组信息
ansible bdp -m shell -a 'cat /etc/group'hadoop:x:666:
User 模块
该模块主要管理远程主机上的 用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作。
# 查看user模块具体参数
[root@node1 local]# ansible-doc -s user
- name: Manage user accountsuser:append: # If `yes', add the user to the groups specified in `groups'. If `no', user will only be added to the groups specified in `groups', removing them from all other groups. Mutually exclusive with `local'authorization: # Sets the authorization of the user. Does nothing when used with other platforms. Can set multiple authorizations using comma separation. To delete all authorizations, use `authorization='''. Currently supported on Illumos/Solaris.comment: # Optionally sets the description (aka `GECOS') of user account.create_home: # Unless set to `no', a home directory will be made for the user when the account is created or if the home directory does not exist. Changed from `createhome' to `create_home' in Ansible 2.5.expires: # An expiry time for the user in epoch, it will be ignored on platforms that do not support this. Currently supported on GNU/Linux, FreeBSD, and DragonFlyBSD. Since Ansible 2.6 you can remove the expiry time specify a negative value.Currently supported on GNU/Linux and FreeBSD.force: # This only affects `state=absent', it forces removal of the user and associated directories on supported platforms. The behavior is the same as `userdel --force', check the man page for `userdel' on your system for details and support.When used with `generate_ssh_key=yes' this forces an existing key to be overwritten....... // 其它的就不列举了 详细的查看以下解释name 用于指定操作的 user,必须项。
uid 用于指定 user 的 UID,默认为空。
non_unique 与uid参数一起使用,允许改变UID为非唯一值。
group 参数用于指定用户 主组。默认值为空,为空时创建的用户组名跟用户名一致。
groups 参数用于指定用户属组,可以在创建用户时指定用户属组,也可以管理已经存在的用户属组。append 跟groups参数一起使用管理用户属组,默认为false,如果 append='yes' ,则从groups参数中增加用户的属组;如果 append='no' ,则用户属组只设置为groups中的组,移除其他所有属组。state 参数用于指定用户是否存在于远程主机中。可选值有 present、absent,默认值为 present。
remove 参数在 state=absent 时使用,等价于 userdel --remove 布尔类型,默认值为 false。
force 参数在 state=absent 时使用,等价于 userdel --force,布尔类型,默认值为 false。
home 参数用于指定用户home目录,值为路径
create_home 在用户创建时或home目录不存在时为用户创建home目录,布尔类型,默认值为 true
move_home 如果设置为yes,结合home= 使用,临时迁移用户家目录到特定目录comment 参数用于指定用户注释信息
shell 参数用于指定用户默认shell
system 参数用于指定用户是否是系统用户
expires 参数用于指定用户过期时间,相当于设置 /etc/shadow 文件中的的 第8列
passwd 参数用于指定用户密码,但是这个密码不能是明文密码,而是一个对明文密码加密后的字符串,默认为空
password_lock 参数用于锁定指定用户,布尔类型,默认为空update_password 参数可选值有always 和 on_create,默认为always 。当设置为always时,password参数的值与 /etc/shadow 中密码字符串不一致时更新用户的密码;当设置为on_create时,password参数的值与 /etc/shadow 中密码字符串不一致时也不会更新用户的密码,但如果是新创建的用户,则此参数即使为on_create,也会更新用户密码。generate_ssh_key 参数用于指定是否生成ssh密钥对,布尔类型,默认为false。当设置为yes时,为用户生成 ssh 密钥对,默认在 ~/.ssh 目录中生成名为 id_rsa私钥 和 id_rsa.pub公钥,如果同名密钥已经存在,则不做任何操作。sssh_key_bits 当 generate_ssh_key=yes 时,指定生成的ssh key加密位数。
ssh_key_file 当 generate_ssh_key=yes 时,使用此参数指定ssh私钥的路径及名称,会在同路径下生成以私钥名开头以 .pub 结尾对应公钥。ssh_key_comment 当 generate_ssh_key=yes 时,在创建证书时,使用此参数设置公钥中的注释信息。如果同名密钥已经存在,则不做任何操作。当不指定此参数时,默认注释信息为"ansible-generated on $hostname”。ssh_key_passphrase 当 generate_ssh_key=yes 时,在创建证书时,使用此参数设置私钥密码。如果同名密钥已经存在,则不做任何操作。ssh_key_type 当 generate_ssh_key=yes 时,在创建证书时,使用此参数指定密钥对的类型。默认值为 rsa,如果同名密钥已经存在,则不做任何操作。
# 基于passlib 生成密文密码 假设加密后的值为 xxx
python -c 'import crypt; print (crypt.crypt("password","hadoop"))' # 添加用户到指定组
ansible bdp -m user -a 'name=hadoop password=xxx update_password=always shell=/bin/bash group=hadoop state=present system=yes createhome=yes home=/home/hadoop' # 删除用户
ansible bdp -m user -a "name=hadoop state=absent"
复制模块(copy/fetch)
#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表示。ansible node142 -m copy -a 'src=/usr/local/software/jdk1.8.0_202 dest=/usr/local/software/ owner=root group=root'
File模块
[hadoop@node1 ~]$ ansible-doc -s file
- name: Manage files and file propertiesfile:access_time: # This parameter indicates the time the file's access time should be set to. Should be `preserve' when no modification is required, `YYYYMMDDHHMM.SS' when using default time format, or `now'. Default is `None' meaning that `preserve' is thedefault for `state=[file,directory,link,hard]' and `now' is default for `state=touch'.
path 必须参数,用于指定要操作的文件或目录,在之前版本的ansible中,使用dest参数或者name参数指定要操作的文件或目录,为了兼容之前的版本,使用dest或name也可以。state 此参数非常灵活,其对应的值需要根据情况设定。比如,想要在远程主机上创建/testdir/a/b目录,那么则需要设置path=/testdir/a/b,但是无法从”/testdir/a/b“这个路径看出b是一个文件还是一个目录,ansible也同样无法单单从一个字符串就知道是要创建文件还是目录,所以需要通过state参数进行说明。想要创建的/testdir/a/b是一个目录时,需要将state的值设置为directory,当它与path结合,ansible就能知道我们要操作的目标是一个目录。同理,想要操作的/testdir/a/b是一个文件时,则需要将state的值设置为touch。当创建软链接文件时,需将state设置为link。想要创建硬链接文件时,需要将state设置为hard。当我们想要删除一个文件时(删除时不用区分目标是文件、目录、还是链接),则需要将state的值设置为absent,”absent”为缺席之意,表示想要删除目标。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。recurse 当要操作的文件为目录,将recurse设置为yes,可以递归的修改目录中文件的属性。
# 创建目录
ansible bdp -m file -a 'path=/app state=directory owner=hadoop group=hadoop mode=755'# 删除目录
ansible bdp -m file -a 'path=/app state=absent'
References
Ansible