盐城网站开发市场,腐女做喜欢的网站,做一个好一点网站费用多少,wordpress 地址 .htmlPalybook组层部分
tasks 任务包含要在目标主机上执行的操作#xff0c;使用模块定义这些操作#xff0c;每个任务都是一个模块的调用variables变量:存储和传递数据#xff0c;变量可以自定义#xff0c;可以在palybook当中定义为全局变量#xff0c;也可以在外部传参temp…Palybook组层部分
tasks 任务包含要在目标主机上执行的操作使用模块定义这些操作每个任务都是一个模块的调用variables变量:存储和传递数据变量可以自定义可以在palybook当中定义为全局变量也可以在外部传参templates模版用于生产配置文件模版是包含占位符的文件占位符由ansible在执行时转换为变量值handlers处理器当需要有变更的时候可以执行触发器roles角色是一种组织和封装palybook的允许把相关的任务变量模版和处理器组织成一个可复用的单元
- name: first play
#一个name就是一个任务名名字可以不写gather_facts: false
#是否收集目标主机的系统信息false就是不收集,最好不写。hosts: 192.168.233.12
#执行的目标主机remote_user: root
#在目标主机执行的用户tasks:- name: ping testping:- name: close selinuxcommand: /sbin/setenforce 0ignore_errors: True- name: close firewalldservice: namefirewalld statestopped- name: install httpdyum: namehttpd- name: start httpdservice: enabledtrue namehttpd statestarted- name: editon index.htmlshell: echo this is httpd /var/www/html/index.htmlnotify: restart httpdhandlers:- name: restart httpdservice: namehttpd staterestarted[rootdocker1 opt]# ansible-playbook test1.yaml --syntax-check
#检查配置文件是否有错误
[rootdocker1 opt]# ansible-playbook test1.yaml --list-task
#检查生效的目标主机
[rootdocker1 opt]# ansible-playbook test1.yaml
#运行剧本文件
[rootdocker1 opt]# ansible-playbook test1.yaml --start-at-taskinstall httpd
#指定运行剧本第几行如需要切换用户在配置文件中写入
remote_user: dn
become: yes
become_ser: root
vim /etc/ansible/ansible.cfg
71行取消注释
vim /etc/ansible/hosts
[dbservers]
192.168.233.12 ansible_userroot ansible_password123
需要声明ip地址与主机名ansible-playbook test1.yaml -u root -k
#密码需要手动输入- hosts: 192.168.233.12remote_user: rootvars:groupname: guoqiusername: wangdefutasks:- name: create groupgroup:name: {{ groupname }}system: yesgid: 111- name: create useruser:name: {{ username }}uid: 1011group: {{ groupname }}shell: /sbin/nologin- name: copy filecopy:content: {{ hostvars[inventory_hostname][ansible_default_ipv4][address]}}dest: /opt/ky32.txt
#获取目标主机的ip地址然后打印出来这里是否获取主机的信息否被删除掉如果无法获取主机的信息就会报错
[rootdocker1 opt]# ansible-playbook test2.yaml -e usernameyst groupnameymr
#在外面传参
playbook的条件判断
when是一个比较常见的应用场景实现满足条件即执行不满足条件即跳过的任务
when是满足条件即执行不满足不执行格式
- hosts: 192.168.233.12
#可以用主机的ip地址也可以是用组名也可以用allremote_user: roottasks:- name: test whendebug:msg: 位置判断when: ansible_default_ipv4.address 192.168.233.20#when: inventory_hostname ! 192.168.233.20
#作用相同
#debugecho msg输出的内容用于脚本的调试在正式脚本中可以去除练习
条件1 ip10安装nginx 条件2 ip20安装httpd
版本1- hosts: allremote_user: roottasks:- name: nginxyum: namenginxwhen: ansible_default_ipv4.address 192.168.233.12- name: httpdyum: namehttpdwhen: ansible_default_ipv4.address 192.168.233.13版本2- hosts: allremote_user: roottasks:- name: nginxyum: namenginx- name: nginx ifodebug:msg: 安装nginxwhen: ansible_default_ipv4.address 192.168.233.12- name: httpdyum: namehttpd- name: httpd infodebug:msy: 安装httpdwhen: ansible_default_ipv4.address 192.168.233.13
ansible有多种循环格式with_items 循环遍历
- hosts: 192.168.233.12remote_user: rootgather_facts: falsetasks:- debug:msg: {{ item }}with_items: [a,b,c,d]
#声明变量item,playbook的内置变量with_item,会把item的值遍历列表当中的a,b,c,d- hosts: 192.168.233.12remote_user: rootgather_facts: falsetasks:- debug:msg: {{ item }}with_items:- [a,b,c,d]- [1,2,3,4]
#这里会被当成一个整体虽然声明的列表是两个但是wiith——items还是把两个列表当成整体进行遍历- hosts: 192.168.233.12remote_user: rootgather_facts: falsetasks:- debug:msg: {{ item }}with_list:- [a,b,c,d]- [1,2,3,4]
#这里会被分组打印一个列表打印一组- hosts: 192.168.233.12remote_user: rootgather_facts: falsetasks:- name: create filefile:path: {{ item }}state: touchwith_items:- [/opt/a,/opt/b,/opt/c,/opt/d]- [/opt/1,/opt/2,/opt/3,/opt/4]
#分组创建文件- hosts: 192.168.233.12remote_user: rootgather_facts: falsetasks:- debug:msg: {{ item }}with_together:- [a,b,c,d]- [1,2,3,4]
#组合输出一一对应列表组循环如果没有组合会输出null
#列表里面的元素定义了循环的次数第二层列表相当于内循环
with_items最常用的
with_list列别分组循环
with_together列表对应的列数据结合的方式循环
with_nested相当于双层循环第一层定义了循环的次数第二层表示第一次的每个元素会循环几次
#基于循环创建文件目录和用户组
- name: play1hosts: 192.168.233.12gather_facts: falsetasks:- name: create groupgroup:name: {{ item }}state: presentwith_items:- dn1- dn2- name: create useruser:name: {{ item.name }}state: presentgroups: {{ item.groups }}with_items:- {name: test1, groups: dn1}- {name: test2, groups: dn2}
yum 一键安装多个软件 tree sl nginx httpd vsftpd dhcp- name: play2hosts: 192.168.233.12gather_facts: falsetasks:- name: create tree sl nginx httpd vsftpd dhcpyum:name: {{ item }}with_list:- tree- sl- nginx- httpd- vsftpd- dhcp