不用iis建立网站,网站制作费用属于广告费吗,个人如何创建公众号,北京网站定制制作任务块
可以通过block关键字#xff0c;将多个任务组合到一起可以将整个block任务组#xff0c;一起控制是否要执行 # 如果webservers组中的主机系统发行版是Rocky#xff0c;则安装并启动nginx[rootpubserver ansible]# vim block1.yml---- name: block taskshosts: webse…任务块
可以通过block关键字将多个任务组合到一起可以将整个block任务组一起控制是否要执行 # 如果webservers组中的主机系统发行版是Rocky则安装并启动nginx[rootpubserver ansible]# vim block1.yml---- name: block taskshosts: webserverstasks:- name: define a group of tasksblock:- name: install nginx # 通过yum安装nginxyum:name: nginxstate: present- name: start nginx # 通过service启动nginx服务service:name: nginxstate: startedenabled: yeswhen: ansible_distributionRocky # 条件为真才会执行上面的任务[rootpubserver ansible]# ansible-playbook block1.yml rescue和always block和rescue、always联合使用 block中的任务都成功rescue中的任务不执行block中的任务出现失败failedrescue中的任务执行block中的任务不管怎么样always中的任务总是执行
[rootpubserver ansible]# vim block2.yml---- name: block testhosts: webserverstasks:- name: block / rescue / always test1block:- name: touch a filefile:path: /tmp/test1.txtstate: touchrescue:- name: touch file test2.txtfile:path: /tmp/test2.txtstate: touchalways:- name: touch file test3.txtfile:path: /tmp/test3.txtstate: touch# 执行playbook web1上将会出现/tmp/test1.txt和/tmp/test3.txt[rootpubserver ansible]# ansible-playbook block2.yml[rootweb1 ~]# ls /tmp/test*.txt/tmp/test1.txt /tmp/test3.txt# 修改上面的playbook使block中的任务出错[rootweb1 ~]# rm -f /tmp/test*.txt[rootpubserver ansible]# vim block2.yml---- name: block testhosts: webserverstasks:- name: block / rescue / always test1block:- name: touch a filefile:path: /tmp/abcd/test11.txtstate: touchrescue:- name: touch file test22.txtfile:path: /tmp/test22.txtstate: touchalways:- name: touch file test33.txtfile:path: /tmp/test33.txtstate: touch# 因为web1上没有/tmp/abcd目录所以block中的任务失败。但是playbook不再崩溃而是执行rescue中的任务。always中的任务总是执行[rootpubserver ansible]# ansible-playbook block2.yml[rootweb1 ~]# ls /tmp/test*.txt/tmp/test22.txt /tmp/test33.txt
loop循环
相当于shell中for循环ansible中循环用到的变量名是固定的叫item
# 在test组中的主机上创建5个目录/tmp/{aaa,bbb,ccc,ddd,eee}[rootpubserver ansible]# vim loop1.yml---- name: use loophosts: webserverstasks:- name: create directoryfile:path: /tmp/{{item}}state: directoryloop: [aaa,bbb,ccc,ddd,eee]# 上面写法也可以改为---- name: use loophosts: webserverstasks:- name: create directoryfile:path: /tmp/{{item}}state: directoryloop: - aaa- bbb- ccc- ddd- eee[rootpubserver ansible]# ansible-playbook loop1.yml# 使用复杂变量。创建zhangsan用户密码是123创建lisi用户密码是456# item是固定的用于表示循环中的变量# 循环时loop中每个-后面的内容作为一个整体赋值给item。# loop中{}中的内容是自己定义的写法为key:val# 取值时使用句点表示。如下例中取出用户名就是{{item.uname}}[rootpubserver ansible]# vim loop_user.yml---- name: create usershosts: webserverstasks:- name: create multiple usersuser:name: {{item.uname}}password: {{item.upass|password_hash(sha512)}}loop:- {uname: zhangsan, upass: 123}- {uname: lisi, upass: 456}[rootpubserver ansible]# ansible-playbook loop_user.yml
role角色
为了实现playbook重用可以使用role角色角色role相当于把任务打散放到不同的目录中再把一些固定的值如用户名、软件包、服务等用变量来表示role角色定义好之后可以在其他playbook中直接调用
# 使用常规playbook修改/etc/motd的内容# 1. 创建motd模板文件[rootpubserver ansible]# vim motdHostname: {{ansible_hostname}} # facts变量主机名Date: {{ansible_date_time.date}} # facts变量日期Contact to: {{admin}} # 自定义变量# 2. 编写playbook[rootpubserver ansible]# vim motd.yml---- name: modifty /etc/motdhosts: webserversvars:admin: roottedu.cn # 自定义名为admin的变量tasks:- name: modify motdtemplate:src: motddest: /etc/motd[rootpubserver ansible]# ansible-playbook motd.yml[rootweb1 ~]# cat /etc/motd Hostname: web1Date: 2021-11-01Contact to: roottedu.cn# 创建角色# 1. 声明角色存放的位置[rootpubserver ansible]# vim ansible.cfg [defaults]inventory hostsroles_path roles # 定义角色存在当前目录的roles子目录中# 2. 创建角色目录[rootpubserver ansible]# mkdir roles# 3. 创建名为motd的角色[rootpubserver ansible]# ansible-galaxy init roles/motd[rootpubserver ansible]# ls roles/motd # 生成了motd角色目录[rootpubserver ansible]# yum install -y tree[rootpubserver ansible]# tree roles/motd/roles/motd/├── defaults # 定义变量的目录优先级最低│ └── main.yml├── files # 保存上传的文件如copy模块用到的文件├── handlers # handlers任务写到这个目录的main.yml中│ └── main.yml├── meta # 保存说明数据如角色作者、版本等│ └── main.yml├── README.md # 保存角色如何使用之类的说明├── tasks # 保存任务│ └── main.yml├── templates # 保存template模块上传的模板文件├── tests # 保存测试用的playbook。可选│ ├── inventory│ └── test.yml└── vars # 定义变量的位置推荐使用的位置└── main.yml# 4. 将不同的内容分别写到对应目录的main.yml中# 4.1 创建motd模板文件[rootpubserver ansible]# vim roles/motd/templates/motdHostname: {{ansible_hostname}}Date: {{ansible_date_time.date}}Contact to: {{admin}}# 4.2 创建变量[rootpubserver ansible]# vim roles/motd/vars/main.yml # 追加一行admin: zzgtedu.cn# 4.3 创建任务[rootpubserver ansible]# vim roles/motd/tasks/main.yml # 追加- name: modify motdtemplate:src: motd # 这里的文件自动到templates目录下查找dest: /etc/motd# 5. 创建playbook调用motd角色[rootpubserver ansible]# vim role_motd.yml---- name: modify motd with rolehosts: webserversroles:- motd# 6. 执行playbook[rootpubserver ansible]# ansible-playbook role_motd.yml
role练习
创建名为pkgs的角色。用于装包。包名使用变量pkg代表创建inst_nginx.yml调用pkgs角色安装nginx创建inst_mysql.yml调用pkgs角色安装mysql
# 1. 创建名为pkgs的角色。# 1.1 创建角色目录[rootpubserver ansible]# ansible-galaxy init roles/pkgs# 1.2 创建装包的任务包名使用变量pkg代表[rootpubserver ansible]# vim roles/pkgs/tasks/main.yml ---# tasks file for roles/pkgs- name: install rpm pkgyum:name: {{pkg}}state: present# 1.3 定义变量[rootpubserver ansible]# vim roles/pkgs/defaults/main.yml ---# defaults file for roles/pkgspkg: nginx# 2. 创建inst_nginx.yml调用pkgs角色安装nginx[rootpubserver ansible]# vim inst_nginx.yml---- name: install nginx pkghosts: webserversroles:- pkgs[rootpubserver ansible]# ansible-playbook inst_nginx.yml# 3. 创建inst_mysql.yml调用pkgs角色安装mysql-server[rootpubserver ansible]# vim inst_mysql.yml ---- name: install mysql pkghosts: dbsvars:pkg: mysql-serverroles:- pkgs[rootpubserver ansible]# ansible-playbook inst_mysql.yml
ansible的公共角色仓库Ansible Galaxy
# 在公共仓库中搜索与nginx相关的角色[rootmyhost ~]# ansible-galaxy search nginx# 如果找到相应的角色如名字为mynginx可以下载它到roles目录[rootmyhost ~]# ansible-galaxy install mynginx -p roles/
ansible加解密文件
ansible加解密文件使用ansible-vault命令
[rootpubserver ansible]# echo Hi ni hao hello.txt [rootpubserver ansible]# cat hello.txtHi ni hao# 加密文件[rootpubserver ansible]# ansible-vault encrypt hello.txtNew Vault password: 123456Confirm New Vault password: 123456Encryption successful[rootpubserver ansible]# cat hello.txt$ANSIBLE_VAULT;1.1;AES256373733663535663462356137313965666465333933613861313136323065636333363339633734656164323461356130303863633964393339363738653036310a666564313832316263393061616330323731333231623538643164353664393862666166613739363635633736343563653266373361656336636230366564650a3832396362306236333565656234613264313936346566663063306635336235# 解密[rootpubserver ansible]# ansible-vault decrypt hello.txtVault password: 123456Decryption successful[rootpubserver ansible]# cat hello.txt Hi ni hao# 加密后更改密码[rootpubserver ansible]# ansible-vault encrypt hello.txt New Vault password: 123456Confirm New Vault password: 123456Encryption successful[rootpubserver ansible]# ansible-vault rekey hello.txt # 改密码Vault password: 123456 # 旧密码New Vault password: abcd # 新密码Confirm New Vault password: abcdRekey successful# 不解密文件查看内容[rootpubserver ansible]# ansible-vault view hello.txt Vault password: abcdHi ni hao# 使用密码文件进行加解密# 1. 将密码写入文件[rootpubserver ansible]# echo tedu.cn pass.txt# 2. 创建明文文件[rootpubserver ansible]# echo hello world data.txt# 3. 使用pass.txt中的内容作为密码加密文件[rootpubserver ansible]# ansible-vault encrypt --vault-idpass.txt data.txtEncryption successful[rootpubserver ansible]# cat data.txt # 文件已加密# 4. 使用pass.txt中的内容作为密码解密文件[rootpubserver ansible]# ansible-vault decrypt --vault-idpass.txt data.txtDecryption successful[rootpubserver ansible]# cat data.txt hello world
使用ansible管理远程主机存储敏感数据时如文件中包含密码应该将其加密执行playbook时通过--ask-vault-password选项提示输入密码
# 1. 编写有密码的playbook[rootpubserver ansible]# vim user_zhangsan.yml---- name: create a userhosts: webserverstasks:- name: create user zhangsanuser:name: zhangsanpassword: {{123|password_hash(sha512)}}# 2. 加密playbook[rootpubserver ansible]# ansible-vault encrypt user_zhangsan.ymlNew Vault password: 123456Confirm New Vault password: 123456Encryption successful# 3. 直接执行playbook报错[rootpubserver ansible]# ansible-playbook user_zhangsan.ymlERROR! Attempting to decrypt but no vault secrets found# 4. 使用--ask-vault-password选项[rootpubserver ansible]# ansible-playbook --ask-vault-password user_zhangsan.ymlVault password: 123456
sudo命令
一般用于普通用户执行需要root权限的命令在web1上配置zhangsan拥有sudo权限
# 如果没有zhangsan手工创建[rootweb1 ~]# visudo # 将会打开vi在尾部追加以下一行zhangsan ALL(ALL) ALL# 中间的ALL(ALL)在集中认证的域环境中才有效单机忽略即可# zhangsan是用户名最后的ALL表示zhangsan可以以管理员的身份执行所有命令# 切换成zhangsan用户执行命令[rootweb1 ~]# su - zhangsan[zhangsanweb1 ~]$ useradd wangwu # 失败因为还是张三身份[zhangsanweb1 ~]$ sudo useradd wangwu # 以管理员身份执行... ...[sudo] password for zhangsan: # 输入zhangsan的密码不是root# 配置lisi不输入密码可以直接运行sudo[rootweb1 ~]# visudo # 在最后追加一行lisi ALL(ALL) NOPASSWD: ALL# 切换成lisi运行[rootweb1 ~]# su - lisi[lisiweb1 ~]$ ls /root/ # 没权限ls: cannot open directory /root/: Permission denied[lisiweb1 ~]$ sudo ls /root/ # 成功运行无需输入密码a3.txt anaconda-ks.cfg
特殊的主机清单变量
如果远程主机没有使用免密登陆如果远程主机ssh不是标准的22端口可以设置特殊的主机清单变量ansible_ssh_user指定登陆远程主机的用户名ansible_ssh_pass指定登陆远程主机的密码ansible_ssh_port指定登陆远程主机的端口号
# 删除远程主机的/root/.ssh/authorized_keys以便恢复通过密码登陆[rootpubserver ansible]# ansible all -m file -a path/root/.ssh/authorized_keys stateabsent# 创建新的工作目录[rootpubserver ~]# mkdir myansible[rootpubserver ~]# cd myansible[rootpubserver myansible]# vim ansible.cfg[defaults]inventory inventory[rootpubserver myansible]# vim inventory[group1]web1web2db1[rootpubserver myansible]# ansible all -m ping # 报错因为无法免密执行# 修改web1 ssh服务的端口为220[rootweb1 ~]# systemctl stop firewalld[rootweb1 ~]# echo Port 220 /etc/ssh/sshd_config[rootweb1 ~]# systemctl restart sshd# 退出再登陆时需要指定端口号[rootmyhost ~]# ssh -p220 192.168.88.11 # 配置ssh通过用户名、密码管理远程主机通过220端口连接web1[rootpubserver myansible]# vim inventory [group1]web1 ansible_ssh_userroot ansible_ssh_passa ansible_ssh_port220web2 ansible_ssh_userroot ansible_ssh_passadb1 ansible_ssh_userroot ansible_ssh_passa[rootpubserver myansible]# ansible all -m ping