静态资产
1.1.1.1
2.2.2.2
3.3.3.[1:15]
test01.gyq.com
test03.gyq.com
test[05:09].gyq.com
[web_servers]
192.168.1.2
192.168.1.3
192.168.1.5
[db_servers]
192.168.2.2
192.168.2.3
192.168.1.5
[all_servers]
[all_servers:children]
db_servers
web_servers
ansible all -i inventory.ini ... //伪指令, 不可执行
ansible db_servers -i inventory.ini --list-hosts
ansible PATTERN -i inventory -m module -a argument
[root@GYQ-master ~]# ansible 1.1.1.1 -i inventory.ini --list-hosts
hosts (1):
1.1.1.1
[root@GYQ-master ~]# ansible web_servers -i inventory.ini --list-hosts
hosts (3):
192.168.1.2
192.168.1.3
192.168.1.5
[root@GYQ-master ~]# ansible 3.3.3.1* -i inventory.ini --list-hosts
hosts (7):
3.3.3.10
3.3.3.11
3.3.3.12
3.3.3.13
3.3.3.14
3.3.3.15
3.3.3.1
[root@GYQ-master ~]# ansible 'web_servers:db_servers' -i inventory.ini --list-hosts
hosts (5):
192.168.1.2
192.168.1.3
192.168.1.5
192.168.2.2
192.168.2.3
[root@GYQ-master ~]# ansible 'web_servers:&db_servers' -i inventory.ini --list-hosts
hosts (1):
192.168.1.5
[root@GYQ-master ~]# ansible 'web_servers:!db_servers' -i inventory.ini --list-hosts
hosts (2):
192.168.1.2
192.168.1.3
ansible pattern [-i inventory] -m module -a argument
ansible-doc -l
[dbservers]
192.168.40.137
[webservers]
192.168.40.138
[root@GYQ-master ~]# ansible all -i hosts -a "echo 'hello'"
192.168.40.137 | CHANGED | rc=0 >>
hello
192.168.40.138 | CHANGED | rc=0 >>
hello
[root@GYQ-master ~]# ansible all -i hosts -m shell -a "echo 'hello'"
192.168.40.138 | CHANGED | rc=0 >>
hello
192.168.40.137 | CHANGED | rc=0 >>
hello
[root@GYQ-master ~]#
shell 模块可以执行SHELL的内置命令和特性(比如管道符)。
command模块无法执行SHELL的内置命令和特性
#ansible webservers -i hosts -m script -a "/root/a.sh"
192.168.40.138 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.40.138 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.40.138 closed."
],
"stdout": "",
"stdout_lines": []
}
[root@GYQ-master ~]# ansible webservers -i hosts -m copy -a "src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo"
192.168.40.138 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "21b7750daa3e959a63aab1564d748e326d565d50",
"dest": "/etc/yum.repos.d/nginx.repo",
"gid": 0,
"group": "root",
"md5sum": "640b9f8a4cc8dc99cb1a0048f28afd4f",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:system_conf_t:s0",
"size": 357,
"src": "/root/.ansible/tmp/ansible-tmp-1678688946.53-7950-171811340470478/source",
"state": "file",
"uid": 0
}
[root@GYQ-master ~]# ansible webservers -i hosts -a "ls /etc/yum.repos.d"
192.168.40.138 | CHANGED | rc=0 >>
CentOS-Base.repo
CentOS-CR.repo
CentOS-Debuginfo.repo
CentOS-fasttrack.repo
CentOS-Media.repo
CentOS-Sources.repo
CentOS-Vault.repo
epel.repo
epel-testing.repo
nginx.repo
[root@GYQ-master ~]#
ansible webservers -i hosts -m copy -a "src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo backup=yes"
[root@GYQ-master ~]# ansible webservers -i hosts -m copy -a "src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo owner=nobody group=nobody"
192.168.40.138 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "21b7750daa3e959a63aab1564d748e326d565d50",
"dest": "/etc/yum.repos.d/nginx.repo",
"gid": 99,
"group": "nobody",
"mode": "0644",
"owner": "nobody",
"path": "/etc/yum.repos.d/nginx.repo",
"secontext": "system_u:object_r:system_conf_t:s0",
"size": 357,
"state": "file",
"uid": 99
}
[root@GYQ-master ~]# ansible webservers -i hosts -m copy -a "src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo mode=0755"
192.168.40.138 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "21b7750daa3e959a63aab1564d748e326d565d50",
"dest": "/etc/yum.repos.d/nginx.repo",
"gid": 99,
"group": "nobody",
"mode": "0755",
"owner": "nobody",
"path": "/etc/yum.repos.d/nginx.repo",
"secontext": "system_u:object_r:system_conf_t:s0",
"size": 357,
"state": "file",
"uid": 99
}
name 仓库名称,就是仓库文件中第一行的中括号中名称,必须的参数。
description 仓库描述信息,添加时必须的参数
baseurl yum存储库"repodata"目录所在目录的URL,添加时必须的参数。它也可以是多个URL的列表。
file 仓库文件保存到被管理节点的文件名,不包含.repo。默认是name的值。
state preset确认添加仓库文件,absent 确认删除仓库文件。
gpgcheck 是否检查GPG yes | no,没有默认值,使用/etc/yum.conf中的配置
Example
[root@GYQ-master ~]# ansible dbservers -i hosts -m yum_repository -a "name=epel baseurl='https://download.fedoraproject.org/pub/epel/$releasever/$basearch/' description='EPEL YUM repo'"
192.168.40.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"repo": "epel",
"state": "present"
}
[root@GYQ-master ~]# ansible dbservers -i hosts -m yum_repository -a "name=epel state=absent"
192.168.40.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"repo": "epel",
"state": "absent"
}
ansible webservers -i hosts -m yum -a "name=nginx state=present" #其他安装方式一致
ansible webservers -i hosts -m yum -a "name=nginx state=removed"
ansible webservers -i hosts -m yum -a "name='@Development tools' state=present"
daemon_reload 重新载入systemd, 扫描新的或有变动的单元
enabled 是否开机自启动yes |no
name 必选项,服务名称,比如httpd vsftpd
state 对当前服务执行启动,停止、重启、重新加载等操作
(started,stopped,restarted,reloaded)
[root@GYQ-master ~]# ansible webservers -i hosts -m systemd -a "daemon_reload=yes"
192.168.40.138 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"name": null,
"status": {}
}
ansible webservers -i hosts -m systemd -a "name=nginx state=started"
ansible webservers -i hosts -m systemd -a "name=nginx state=stopped"
[root@GYQ-master ~]# ansible dbservers -i hosts -m group -a "name=db_admin"
192.168.40.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 1000,
"name": "db_admin",
"state": "present",
"system": false
}
name 必须的参数,指定用户名
password 设置用户的密码,这里接受一个加密的值,因为会直接存到shadow
update_password 假如设置的密码不同于原密码,则会更新密码.在1.3版本中被加入
home 指定用户的家目录
shell 设置用户的shell
comment 用户的描述信息
create_home 在创建用户时,是否创建其家目录。默认创建,假如不创建,设置为no。2.5版本之前使用createhome
group 设置用户的主组
groups 将用户加入到多个其他组中,多个用逗号隔开。
默认会把用户从其他已经加入的组中删除。
append yes|no和groups配合使用,yes 时,不会把用户从其他已经加入的组中删除
system 设置为yes时,将会创建一个 系统账号
expires 设置用户的过期时间,值为时间戳,会转为为天数后,放在 shadow 的第8个字段里;
generate_ssh_key设置为 yles将会为用户生成密钥,这不会覆盖原来的密钥
ssh_key_type 指定用户的密钥类型,默认 rsa,具体的类型取决于被管理
节点
state 删除或添加用户, present为添加,absent 为删除;默认值present
remove 当与state=absent -起使用,删除一个用户 及关联的目录,比如家目录,邮箱目录。可选的值为: yes/no
[root@GYQ-master ~]# pass=$(echo "123456" | openssl passwd -1 -stdin)
[root@GYQ-master ~]# echo $pass
$1$KAvZ0DCJ$Tp7vgBM8qPyDq5KBQqwn2/
ansible all -i hosts -m user -a "name=foo password=${pass}"
nsible all -i hosts -m user -a "name=yangge generate_ssh_key=yes ssh_key_type=ecdsa"
ansible dbservers -i hosts -m user -a "name=tom expires=$(date +%s -d 20231001) groups=db_admin append=yes"
owner 定义文件/目录的属主
group 定义文件/目录的属组
mode 定义文件/目录的权限
path 必选项,定义文件/目录的路径
recurse 递归的设置文件的属性,只对目录有效
src 要被链接(软/硬)的源文件的路径,只应用于state=link的情况
dest 被链接到的路径,只应用于state=link的情况
state
directory 如果目录不存在,创建目录
file 文件不存在,则不会被创建,存在则返回文件的信息,常用于检查文件是否存在。
link 创建软链接
hard 创建硬链接
touch 如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent 删除目录、文件或者取消链接文件
ansible all -i hosts -m file -a "path=/tmp/foo.conf state=touch"
ansible all -i hosts -m file -a "path=/tmp/foo.conf owner=nobody group=nobody mode=0644"
ansible all -i hosts -m file -a "src=/tmp/foo.conf dest=/tmp/link.conf state=link"
ansible all -i hosts -m file -a "path=/tmp/testdir state=directory"
ansible all -i hosts -m file -a "path=tmp/link.conf state=absent"
ansible all -i hosts -m file -a "path=/tmp/foo.conf state=absent"
ansible all -i hosts -m file -a "path=/tmp/testdir state=absent"
name 指定一个cron job的名字。王定要指定,便于日之后删除。
minute 指定分钟,可以设置成(O-59,*,*/2等)格式。默认是*,也就是每分钟。
hour 指定小时,可以设置成(0-23,*,*/2等)格式。默认是*,也就是每小时。
day 指定天,可以设置成(1-31,*,*/2等)格式。默认是*,也就是每天。
month 指定月份,可以设置成(1-12,*,*/2等)格式。默认是*,也就是每周。
weekday 指定星期,可以设置成(O-6 for Sunday-Saturday,*等)格式。默认是*,也就是每星期。
job 指定要执行的内容,通常可以写个脚本,或者一段内容。
state 指定这个job的状态,可以是新增(present)或者是删除(absent)。默认为新增(present)
ansible all -i hosts -m cron -a "name='create new job' minute='0' job='ls -alh > /dev/null'"
ansible all -i hosts -m cron -a "name='create new job' state=absent"
src指定Ansible控制端的文件路径
dest指定Ansible 被控端的文件路径
owner 指定文件的属主
group指定文件的属组
mode指定文件的权限
backup 创建一个包含时间戳信息的备份文件,这样如果您以某种方式错误地破坏了原始文件,就可以将其恢复原状。yes/no
#cat hello_world.j2
Hello {{var}} !
#ansible all -i hosts -m template -a "src=hello_world.j2 dest=/tmp/hello_world" -e "var=world"
#cat /tmp/hello_world.world
Hello world !
path 被管理节点的目标文件路径,必须。
state 可选值absent 删除| present 替换(默认值)。
regexp 在文件的每- -行中查找的正则表达式。对于state=present ,仅找到的最后-行将被替换。
line 要在文件中插入/替换的行。需要state=present。
create 文件不存在时,是否要创建文件并添加内容。yes/no
ansible dbservers -i hosts -m lineinfile -a "path=/etc/sudoers regexp='^%wheel' state=absent"
ansible dbservers -i hosts -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SElinux=disabled' state=present"
文章来源地址https://www.toymoban.com/news/detail-476176.html
文章来源:https://www.toymoban.com/news/detail-476176.html
到了这里,关于Ansible自定义静态资产以及常用模块的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!