1. ✅ansible变量1.1. 主机清单变量-自动修改主机名1️⃣修改主机清单定义主机名变量vim hosts [lb] 172.16.1.5 hostnamelb01.zhubl.xyz 172.16.1.6 hostnamelb02.zhubl.xyz [web] 172.16.1.7 hostnameweb01.zhubl.xyz 172.16.1.8 hostnameweb02.zhubl.xyz 172.16.1.9 hostnameweb03.zhubl.xyz 172.16.1.10 hostnameweb04.zhubl.xyz [db] 172.16.1.51 hostnamedb01.zhubl.xyz 172.16.1.52 hostnamedb02.zhubl.xyz [nfs] 172.16.1.31 hostnamenfs01.zhubl.xyz [bak] 172.16.1.41 hostnamebackup.zhubl.xyz [web:vars] passwordzhubl0007 [db:vars] passwordzhubl09962️⃣准备hosts文件vim ./files/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.1.5 lb01 lb01.zhubl.xyz 172.16.1.6 lb02 lb02.zhubl.xyz 172.16.1.7 web01 web01.zhubl.xyz 172.16.1.8 web02 web02.zhubl.xyz 172.16.1.9 web03 web03.zhubl.xyz 172.16.1.10 web04 web04.zhubl.xyz 172.16.1.31 nfs01 nfs01.zhubl.xyz 172.16.1.41 backup backup.zhubl.xyz 172.16.1.51 db01 db01.zhubl.xyz 172.16.1.52 db02 db02.zhubl.xyz 172.16.1.61 m01 m01.zhubl.xyz 172.16.1.62 m02 m02.zhubl.xyz3️⃣编写playbook剧本- hosts: web:db tasks: - name: 修改主机名 hostname: name: {{ hostname }} - name: 查看主机名 debug: msg: {{ hostname }} - name: 拷贝hosts文件 copy: src: ./files/hosts dest: /etc/hosts backup: yes - name: 修改用户密码 user: name: zbl password: {{ password|password_hash(sha512,zbl) }} state: present - name: 查看用户密码 debug: msg: {{ password }}4️⃣运行playbook剧本ansible-playbook -i hosts 09.hostname.yml5️⃣检查ansible -i hosts all -m shell -a hostname2. ✅ansible循环列表变量fruits: - a - b - c fruits[a,b,c] loop/with_items 循环对列表进行取值2.1. ☀️批量添加用户1️⃣编写playbook剧本- hosts: web tasks: - name: groupadd group: name: {{ item.name }} gid: {{ item.gid }} state: present loop: - { name: zbl01,gid: 5060 } - { name: zbl02,gid: 5070 } - { name: zbl03,gid: 5080 } - { name: zbl04,gid: 5090 } - name: useradd user: name: {{ item.name }} uid: {{ item.uid }} group: {{ item.name }} state: present loop: - { name: zbl01,uid: 5060 } - { name: zbl02,uid: 5070 } - { name: zbl03,uid: 5080 } - { name: zbl04,uid: 5090 }2️⃣运行playbook剧本ansible-playbook -i hosts 10.useradd.yml3️⃣检查ansible -i hosts web -m shell -a egrep zbl0[1-4] /etc/passwd3. ✅facts变量与条件判断when结合3.1. 不同的操作系统安装软件包编写playbook剧本when与facts变量结合- hosts: web gather_facts: true tasks: - name: yum yum: name: cowsay state: latest when: ansible_distribution is match(Kylin|Rocky) - name: apt apt: name: cmatrix state: latest when: ansible_distribution is match(Ubuntu|Debian)运行playbook剧本ansible-playbook -i hosts 11.yum.yml检查ansible -i hosts web -m shell -a rpm -qa cowsay4. ✅register变量与条件判断when结合编写playbook剧本- hosts: web:ubt gather_facts: true tasks: - name: register shell: hostnamectl | awk /Operating System/{print $3} register: os - name: yum yum: name: cowsay state: latest when: os.stdout is match(Kylin|Rocky) - name: apt apt: name: cmatrix state: latest when: os.stdout is match(Ubuntu|Debian)运行playbook剧本ansible-playbook -i hosts 12.when_register.yml5. ✅j2模板与if条件判断结合5.1. 部署keepalived服务使用j2模板发送配置文件到被管理主机上1️⃣准备j2模板vim files/keepalived.conf.j2 ! Configuration File for keepalived global_defs { router_id {{ansible_hostname}} } {% if ansible_hostname is match(lb01) %} vrrp_script check_lb.sh { script /server/scripts/check_lb.sh interval 2 weight 1 user root } {% endif %} vrrp_instance lb_vip_3 { {% if ansible_hostname is match(lb01) %} state MASTER priority 100 {% elif ansible_hostname is match(lb02) %} state BACKUP priority 50 {% endif %} interface ens33 virtual_router_id 51 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3/24 dev ens33 label ens33:1 } {% if ansible_hostname is match(lb01) %} track_script { check_lb.sh } {% endif %} }2️⃣编写playbook剧本- hosts: lb gather_facts: true tasks: - name: install yum: name: keepalived state: latest - name: copy template: src: ./files/keepalived.conf.j2 dest: /etc/keepalived/keepalived.conf backup: true - name: start systemd: name: keepalived enabled: yes state: restarted3️⃣运行playbook剧本ansible-playbook -i hosts 13.keepalived_j2.yml4️⃣检查ansible -i hosts lb -m shell -a hostname -I6. ✅j2模板与for循环结合6.1. 分发nfs配置文件单变量1️⃣定义变量列表vim group_vars/all/vars.yml nfs_dir: - /nfs01/ - /nfs02/ - /nfs03/2️⃣准备j2模板文件vim files/exports.j2 {% for dir in nfs_dir %} #共享目录{{ dir }} {{ dir }} 172.16.1.31/24(rw,all_squash) {% endfor %}3️⃣编写playbook剧本- hosts: nfs tasks: - name: template template: src: ./files/exports.j2 dest: /tmp/exports4️⃣运行playbook剧本ansible-playbook -i hosts 14.export_j2.yml5️⃣检查ansible -i hosts nfs -m shell -a cat /tmp/exports6.2. 分发rsync配置文件多变量1️⃣定义变量列表vim group_vars/all/vars.yml rsync_dirs: - {module: data,path: /data/} - {module: blog,path: /app/code/blog/} - {module: backup,path: /nfs/backup/}2️⃣准备j2模板文件vim files/rsyncd.conf.j2 {% for dir in rsync_dirs %} [{{ dir.module }}] comment {{ dir.module }} path {{ dir.path }} {% endfor %}3️⃣编写playbook剧本- hosts: bak tasks: - name: rsync template: src: ./files/rsyncd.conf.j2 dest: /tmp/rsyncd.conf4️⃣运行playbookansible-playbook -i hosts 15.rsync_j2.yml5️⃣检查ansible -i hosts bak -m shell -a cat /tmp/rsyncd.conf6.3. 分发nginx配置文件1️⃣定义变量列表vim group_vars/all/vars.yml sites: - bird - ba - china2️⃣准备j2模板文件vim files/zhubl.xyz.conf.j2 server { listen 80; server_name {{ item }}.zhubl.xyz access_log /var/log/nginx/{{ item }}-access.log main; error_log /var/log/nginx/{{ item }}-error.log notice; root /app/code/{{ item }}/ location / { index index.html } }3️⃣编写playbook剧本- hosts: web tasks: - name: copy template: src: ./files/zhubl.xyz.conf.j2 dest: /tmp/{{ item }}.zhubl.xyz.conf loop: {{ sites }}4️⃣运行playbookansible-playbook -i hosts 16.nginx_j2.yml5️⃣检查ansible -i hosts web -m shell -a ls -l /tmp/*.conf生成数字序列{% for ip in range(2,11) %} 10.0.0.{{ ip }} {% endfor %} {% for ip in [1,2] %} 10.0.0.{{ ip }} {% endfor %}7. ✅总结剧本循环jinja2模板循环批量修改主机名主机清单变量目标rsync,nfs,lsync,web,nginx,php剧本剧本文件变量文件配置文件jinja2