ansible playbook 以及变量(2)
第一章 剧本编写
1.1PlayBook = 剧本
- 剧本组成:
- palybook 定义一个文件,并且以yml为结尾的后缀
- paly:定义的是主机的角色
- task:定义的是具体执行的任务
- 总结:playbook 是由一个或多个play组成,一个play可以包含多个task任务,可以理解为:使用不同的模块来完成一件事。
1.2编写playbook
host:对哪些主机进行操作
remote_user:我要对哪些主机进行操作
Tasks:具体执行什么操作
1.3安装httpd服务
[root@blog ~]# mkdir probject1
[root@blog ~]# cd probject1/
[root@blog probject1]# ls
检查语法:
[root@blog probject1]# ansible-playbook --syntax-check playbook1.yml
模拟执行:
[root@blog probject1]# ansible-playbook -C playbook1.yml
方式一:
[root@blog probject1]# cat playbook1.yml
- hosts: web
tasks:
- name: Installed Httpd Server
yum:
name: httpd
state: present
- name: Start Httpd Server
systemd:
name: httpd
state: started
enabled: yes
[root@blog probject1]# cat playbook1.yml
- hosts: web
tasks:
- name: Installed Httpd Server
yum: name=httpd state=present
- name: Configure Httpd Server Web
copy: content='我是被控端' dest=/var/www/html/index.html
- name: Start Httpd Server
systemd: name=httpd state=started enabled=yes
[root@zabbix-agent ~]# ps -ef|grep yum
root 11620 11077 0 14:39 pts/1 00:00:00 /bin/sh -c /usr/bin/python /root/.ansible/tmp/ansible-tmp-1574318347.4-135650022596860/AnsiballZ_yum.py && sleep 0
root 11633 11620 0 14:39 pts/1 00:00:00 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1574318347.4-135650022596860/AnsiballZ_yum.py
root 11635 11396 0 14:39 pts/0 00:00:00 grep --color=auto yum
第二章 编写多个play
2.1需求:
部署两台apache web服务器
访问103.80.27.77获取到”我是被控端01"
访问121.36.10.238获取到"我是被控端02”
[root@blog probject1]# cat playbook1.yml
- hosts: web
tasks:
- name: Installed Httpd Server
yum: name=httpd state=present
- name: Start Httpd Server
systemd: name=httpd state=started enabled=yes
- hosts: ansible
tasks:
- name: Configure Httpd Web Server 01
copy: content='I am charged end 01' dest=/var/www/html/index.html
- hosts: agent01
tasks:
- name: Configure Httpd Web Server 02
copy: content='I am charged end 02' dest=/var/www/html/index.html
第三章 ansible Playbook 定义变量方式
3.1 Ansible Playbook变量
1)通过命令进行变量的定义
2)在play文件中进行定义变量
3)通过在主机组或单个主机中设置变量
如果定义的变量出现重复,且造成冲突,优先级如下
1.命令行的定义变量-高于》-play文件定义的变量-高于》inventory文件定义的变量
3.2.playbook变量可以通过多种方式进行定义,最简单的方式就是在Playbook的开头通过vars进行定义
安装两个软件包进行进行测试
[root@blog probject1]# cat variable.yml
- hosts: web
vars:
- nginx: nginx
- apache: httpd
tasks:
- name: Install Web Nginx Apache Server
yum:
name:
- "{{ nginx }}"
- "{{ apache }}"
state: present
[root@zabbix-agent yum.repos.d]# rpm -qa|grep nginx
nginx-filesystem-1.16.1-1.el7.noarch
nginx-mod-http-xslt-filter-1.16.1-1.el7.x86_64
nginx-mod-mail-1.16.1-1.el7.x86_64
nginx-mod-stream-1.16.1-1.el7.x86_64
nginx-1.16.1-1.el7.x86_64
nginx-all-modules-1.16.1-1.el7.noarch
nginx-mod-http-perl-1.16.1-1.el7.x86_64
nginx-mod-http-image-filter-1.16.1-1.el7.x86_64
[root@zabbix-agent yum.repos.d]# rpm -qa|grep httpd
httpd-2.4.6-90.el7.centos.x86_64
httpd-tools-2.4.6-90.el7.centos.x86_64
3.3使用变量文件vars-files定义变量
变量文件
[root@blog probject1]# cat vars.yml
nginx: nginx
apache: httpd
安装文件
[root@blog probject1]# cat variable.yml
- hosts: web
vars_files: ./vars.yml //绝对路径或相对路径
tasks:
- name: Install Web Nginx Apache Server
yum:
name:
- "{{ nginx }}"
- "{{ apache }}"
state: present
3.4在inventory中定义变量,主机的变量高于主机组的变量(不推荐使用)
[root@blog probject1]# cat /etc/ansible/hosts
[web]
agent01 ansible_ssh_host=121.36.10.238
[web:vars]
file_name=ansible.sh
[root@blog probject1]# cat file.yml
- hosts: web
tasks:
- name: Create files
file: path=/tmp/{{ file_name }} state=touch
[root@blog probject1]# ansible web -m shell -a 'ls /tmp/'
agent01 | CHANGED | rc=0 >>
ansible.sh
3.5. group_vars host_vars定义变量
更好的方式是在ansible的项目目录中创建额外的两个变量目录,分别是host_vars和group_vars
group_vars目录必须存放和inventory清单文件中定义的组名一致,
如下:
[root@blog probject1]# cat /etc/ansible/hosts
[job]
agent01 ansible_ssh_host=121.36.10.238
[root@blog probject1]# cat group_vars/job
vsftpd: vsftpd
apache: httpd
[root@blog probject1]# cat vsftpd.yml
- hosts: job
tasks:
- name: Install Web Nginx Apache Server
yum:
name:
- "{{ vsftpd }}"
- "{{ apache }}"
state: present
[root@zabbix-agent ~]# rpm -qa|grep vsftpd
vsftpd-3.0.2-25.el7.x86_64
[root@zabbix-agent ~]# rpm -qa|grep httpd
httpd-2.4.6-90.el7.centos.x86_64
httpd-tools-2.4.6-90.el7.centos.x86_64
New:系统提供了特殊的组,all,也就是说在group_vars目录下创建一个all文件,定义变量对所有的主机都生效。
[root@blog probject1]# cat hosts_vars/job
vsftpd: zlib-static
apache: zmap
[root@blog probject1]# cat group_vars/job
vsftpd: tree
apache: htop
[root@blog probject1]# cat vsftpd.yml
- hosts: job
tasks:
- name: Install Web Nginx Apache Server
yum:
name:
- "{{ vsftpd }}"
- "{{ apache }}"
state: present
3.6 通过命令行变量
通过命令行覆盖变量,inventory的变量会被playbook文件中覆盖,这两种方式的变量都会被命令行指定变量所覆盖,使用—extra-vars or -e 参数来设定变量
[root@blog probject1]# ansible-playbook vsftpd.yml -e "vsftpd=tree" -e "apache=htop"
New:优先级
命令行>play中的vars_file>play中的 vars变量> host_vars中定义的变量 >group_vars组中定义的变量>froup_vars/all
3.7变量也支持层级定义,使用"."可能会有问题,建议使用“[]”代替.
变量文件
[root@blog probject1]# cat vars1.yml
rainbow:
web:
web_package: sl
db_package: htop
code:
web:
filename: code_web_filename
app:
ftpd:
ftp: vsftpd
apache: httpd
[root@blog probject1]# cat p1.yml
- hosts: job
vars_files: ./vars1.yml
tasks:
- name: Install command
yum: name= "{{ rainbow['web']['web_package'] }}", name= "{{ rainbow['web']['db_package'] }}"
- name: Create files
file: path=/tmp/"{{ code['web']['filename'] }}", state=touch
#path: /tmp/{{ code.web.filename }}
#state: touch
- name: Install Ftp
yum: name= "{{ app['ftpd']['ftp'] }}", name= "{{ app['ftpd']['apache'] }}"
3.8 变量注册register
- hosts: webservers
tasks:
- name: Get Network Port Status
shell: netstat -lntp
register: net_port
- name: OutPut Network Port Status
debug:
msg: "{{ net_port.stdout_lines }}"
3.9 facts变量
facts是在被管理主机上通过ansible自动采集发现的变量。
[root@blog probject1]# cat p3.yml
- hosts: job
tasks:
- name: ansible facts
debug:
msg: >
Server Hostname "{{ ansible_fqdn }}" is "{{ ansible_all_ipv4_addresses }}" or "{{ ansible_date_time.weekday }}"
[root@blog probject1]#