ansible的脚本
playbook剧本
1、playbook的组成
1、Tasks:任务,每一个task就是一个模块2、Variables:变量,存储和传递数据,可以自定义变量也可以是全局变量,也可以是脚本外传参3、Templates:模版,用生成配置文件和多任务的编排4、Handlers:处理器,用于满足某些条件时触发的操作,一般用于重启等操作5、Roles:角色,组织和封装剧本的过程,角色可以把任务、变量、模版、处理器,组合成一个可用单元
2、yaml文件的语法
vim test1.yaml- name: first play
#定义剧本的名称,可以省略gather_facts: false
#表示在执行剧本之前是否收集目标主机的信息,false是不收集,可以加快执行速度。如果不写,默认就是收集hosts: 192.168.230.20
#指定目标主机,可以是组名也可以是ip地址remote_user: root
#在目标主机的执行用户tasks: - name: test connection
#定义一个任务的名称,可以自定义ping:
#ping是模块名称- name: close selinuxcommand: '/sbin/setenforce 0'ignore_errors: True
#如果在任务执行中报错,返回码非0报错,task就会停止,ignore_errors: True就会忽略错误,继续执行下一个任务- name: close firewalldservice: name=firewalld state=stopped
#调用service模块,关闭防火墙- name: install httpdyum: name=httpd state=latest
#latest表示安装当前库中的最新版本的软件- name: interviewshell: echo "this is httpd" > /var/www/html/index.html
#执行shell模块,修改默认的访问页面notify: restart httpd
#ansible在执行完任务之后并不会立即执行重启,通过notify指令对应的名称传给触发器,让触发器在任务的最后执行重启,避免在任务中多次执行重启,影响执行的效率handlers:- name: restart httpdservice: name=httpd state=restarted #运行
ansible-playbook test1.yaml
3、安装nginx
安装方式为yum,传一个配置文件到目标主机,修改默认端口为8080,访问页面内容this is nginx
vim test2.yaml- name: first playgather_facts: falsehosts: 192.168.230.30remote_user: roottasks:- name: test connectionping:- name: close selinuxcommand: '/sbin/setenforce 0'ignore_errors: True- name: close firewalldservice: name=firewalld state=stopped- name: install nginxyum: name=nginx state=latest- name: interviewshell: echo "this is nginx" > /usr/share/nginx/html/index.html- name:copy: 'src=/opt/nginx.conf dest=/etc/nginx/'notify: restart nginxhandlers:- name: restart nginxservice: name=nginx state=restarted#运行
ansible-playbook test2.yaml
4、定义变量,引用变量
脚本当中定义,以及脚本外传参
vim test3.yaml- name: second playhosts: 192.168.230.30remote_user: rootvars: groupname: mysqlusername: nginx1
#定义变量tasks: - name: create groupgroup: name: "{{ groupname }}"system: yesgid: 306- name: create useruser:name: "{{ username }}"uid: 306group: "{{ groupname }}"#运行
ansible-playbook test3.yaml#往脚本里传参
ansible-playbook test1.yaml -e 'groupname=test1 username=test2'#检查脚本语法是否有错
ansible-playbook test1.yaml --syntax-check#检查脚本中有几个任务
ansible-playbook test1.yaml --list-task#查看对哪些主机生效
ansible-playbook test1.yaml --list-hosts#指定从哪个任务开始运行
ansible-playbook test1.yaml --start-at-task='create user' -e 'username=test3 groupname=test4'#切换用户
- name: second playhosts: 192.168.230.30remote_user: dnbecome: yes
#先用普通用户执行,但是需要切换到其他的用户。例如切换到管理员become_user: root
5、在脚本中实现条件判断
when 满足条件的主机执行,不满足的跳过
vim test4.yaml- name: this is ifhosts: allremote_user: roottasks:- name: test whendebug: msg='条件满足'
#debug相当于echowhen: ansible_default_ipv4.address == "192.168.230.30"ansible_default_ipv4.address != "192.168.230.30"
#取反#运行
ansible-playbook test4.yaml
6、循环结构
ansible有多种循环方式,一般都命名为with_items,定义循环的内容
#with_items 单循环输出- name: item testhosts: 192.168.230.20remote_user: rootgather_facts: falsetasks:- debug:msg: "{{item}}"with_items:- [a,b,c,d]- [1,2,3,4]输出item的值,with_items:a b c d 依次传入
with_list:整个列表作为一个整体进行输出
with_together:作为整体两两配对输出
with_nested:每一层都会遍历执行一遍输出结果
7、创建递归目录
条件判断,主机的ip=192.168.230.20才会执行,一次性创建4个文件 /opt/a /opt/b /opt/c /opt/d
vim test5.yaml
方法一:
- name: hosts: 192.168.230.20gather_facts: falsevars: test:- /opt/test1- /opt/test2- /opt/test3- /opt/test4tasks:- name: create mulufile:path: "{{item}}"state: directorywith_items: "{{test}}"方法二:
- name: hosts: 192.168.230.20gather_facts: falsetasks:- name: create mulufile:path: "{{item}}"state: directorywith_items: [/opt/test1,/opt/test2,/opt/test3/opt/test4]#运行
ansible-playbook test5.yaml