国外的电商网站,厦门有什么好企业网站,WordPress 发布内容,深圳南园网站建设目录
playbook 剧本
playbooks 本身由以下各部分组成
定义、引用变量
指定远程主机sudo切换用户
when条件判断
迭代
Templates 模块
1.先准备一个以 .j2 为后缀的 template 模板文件#xff0c;设置引用的变量
2.修改主机清单文件#xff0c;使用主机变量定义一个变…目录
playbook 剧本
playbooks 本身由以下各部分组成
定义、引用变量
指定远程主机sudo切换用户
when条件判断
迭代
Templates 模块
1.先准备一个以 .j2 为后缀的 template 模板文件设置引用的变量
2.修改主机清单文件使用主机变量定义一个变量名相同而值不同的变量
3.编写 playbook
tags 模块
用剧本安装lnmp
Roles 模块
roles 的目录结构
roles 内各目录含义解释
在一个 playbook 中使用 roles 的步骤
编写httpd模块
编写mysql模块
编写php模块
编写roles示例 playbook 剧本
playbooks 本身由以下各部分组成
1Tasks任务即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行 2Variables变量 3Templates模板 4Handlers处理器当changed状态条件满足时notify触发执行的操作 5Roles角色
示例
vim test1.yaml
--- #yaml文件以---开头以表明这是一个yaml文件可省略
- name: first play #定义一个play的名称可省略gather_facts: false #设置不进行facts信息收集这可以加快执行速度可省略hosts: webservers #指定要执行任务的被管理主机组如多个主机组用冒号分隔remote_user: root #指定被管理主机上执行任务的用户tasks: #定义任务列表任务列表中的各任务按次序逐个在hosts中指定的主机上执行- name: test connection #自定义任务名称ping: #使用 module: [options] 格式来定义一个任务- name: disable selinuxcommand: /sbin/setenforce 0 #command模块和shell模块无需使用keyvalue格式ignore_errors: True #如执行命令的返回值不为0就会报错tasks停止可使用ignore_errors忽略失败的任务- name: disable firewalldservice: namefirewalld statestopped #使用 module: options 格式来定义任务option使用keyvalue格式- name: install httpdyum: namehttpd statelatest- name: install configuration file for httpdcopy: src/opt/httpd.conf dest/etc/httpd/conf/httpd.conf #这里需要一个事先准备好的/opt/httpd.conf文件notify: restart httpd #如以上操作后为changed的状态时会通过notify指定的名称触发对应名称的handlers操作- name: start httpd serviceservice: enabledtrue namehttpd statestartedhandlers: #handlers中定义的就是任务此处handlers中的任务使用的是service模块- name: restart httpd #notify和handlers中任务的名称必须一致service: namehttpd staterestarted
##Ansible在执行完某个任务之后并不会立即去执行对应的handler而是在当前play中所有普通任务都执行完后再去执行handler这样的好处是可以多次触发notify但最后只执行一次对应的handler从而避免多次重启。
- name: the first play for install apachegather_facts: falsehosts: dbserversremote_user: roottasks:- name: disable firewalldservice: namefirewalld statestopped enabledno- name: disable selinuxcommand: /usr/sbin/setenforce 0ignore_errors: true- name: disable selinux foreverreplace: path/etc/selinux/config regexpenforcing replacedisabled- name: mount cdrommount: src/dev/sr0 path/mnt fstypeiso9660 statemounted- name: copy local yum configuration filecopy: src/etc/yum.repos.d/repo.bak/local.repo dest/etc/yum.repos.d/local.repo- name: install apacheyum: namehttpd statelatest- name: prepare httpd configuration filecopy: src/etc/ansible/playbook/httpd.conf dest/etc/httpd/conf/httpd.confnotify: reload httpd- name: start apacheservice: namehttpd statestarted enabledyeshandlers: - name: reload httpdservice: namehttpd statereloaded
运行playbook
ansible-playbook demo1.yaml 在另一台机器上就会发现apache已经安装并暴露出端口号
netstat -lntp | grep 80
tcp6 0 0 :::80 :::* LISTEN 11454/httpd补充参数-k–ask-pass用来交互输入ssh密码-K-ask-become-pass用来交互输入sudo密码-u指定用户 ansible-playbook test1.yaml --syntax-check #检查yaml文件的语法是否正确 ansible-playbook test1.yaml --list-task #检查tasks任务 ansible-playbook test1.yaml --list-hosts #检查生效的主机 ansible-playbook test1.yaml --start-at-taskinstall httpd #指定从某个task开始运行 定义、引用变量
- name: second playhosts: dbserversremote_user: rootvars: #定义变量- groupname: mysql #格式为 key: value- username: nginxtasks:- name: create groupgroup: name{{groupname}} systemyes gid306 #使用 {{key}} 引用变量的值- name: create useruser: name{{username}} uid306 group{{groupname}} - name: copy filecopy: content{{ansible_default_ipv4}} dest/opt/vars.txt #在setup模块中可以获取facts变量信息
示例
- name: second playhosts: dbserversremote_user: rootbecome: yesbecome_user: zxrvars:- username: ggl- groupname: zxr- filename: /opt/ggl.txt- uid: 1234gather_facts: truetasks:- name: create groupgroup: name{{groupname}} gid1314- name: create user join groupuser: name{{username}} uid{{uid}} groups{{groupname}}- name: copy filecopy: content{{ansible_default_ipv4.address}} dest{{filename}}- name: modify username and groupname of filefile: path{{filename}} owner{{username}} group{{groupname}}在另一个主机上输入id ggl
id ggl
uid1234(ggl) gid1234(ggl) 组1234(ggl),1314(zxr)在外面传参
ansible-playbook test1.yaml -e usernamenginx #在命令行里定义变量
在另一个主机上输入id nginx
id nginx
uid1235(nginx) gid1235(nginx) 组1235(nginx),1314(zxr)
指定远程主机sudo切换用户
---
- hosts: dbserversremote_user: zhangsan become: yes #2.6版本以后的参数之前是sudo意思为切换用户运行become_user: root #指定sudo用户为root
执行playbook时ansible-playbook test1.yml -k -K
示例
- name: second playhosts: dbserversremote_user: zxrbecome: yesbecome_user: rootvars:- username: ycx- groupname: zxr- filename: /opt/ggl.txt- uid: 1236gather_facts: truetasks:- name: create groupgroup: name{{groupname}} gid1314- name: create user join groupuser: name{{username}} uid{{uid}} groups{{groupname}}- name: copy filecopy: content{{ansible_default_ipv4.address}} dest{{filename}}- name: modify username and groupname of filefile: path{{filename}} owner{{username}} group{{groupname}}
ansible-playbook demo2.yaml -k -K
SSH password:
BECOME password[defaults to SSH password]:PLAY [second play] *************************************************************TASK [Gathering Facts] *********************************************************
ok: [192.168.110.70]TASK [create group] ************************************************************
ok: [192.168.110.70]TASK [create user join group] **************************************************
changed: [192.168.110.70]TASK [copy file] ***************************************************************
ok: [192.168.110.70]TASK [modify username and groupname of file] ***********************************
changed: [192.168.110.70]PLAY RECAP *********************************************************************
192.168.110.70 : ok5 changed2 unreachable0 failed0 skipped0 rescued0 ignored0
注在另一台主机上要确保用户加入到/etc/sudoers文件中确保zxr要有root权限
when条件判断
在Ansible中提供的唯一一个通用的条件判断是when指令当when指令的值为true时则该任务执行否则不执行该任务。
//when一个比较常见的应用场景是实现跳过某个主机不执行任务或者只有满足条件的主机执行任务
vim demo2.yaml
---
- hosts: allremote_user: roottasks:- name: shutdown host command: /sbin/shutdown -r nowwhen: ansible_default_ipv4.address 192.168.110.70 #when指令中的变量名不需要手动加上 {{}}
或 when: inventory_hostname 主机名
示例
- name: third playhosts: allremote_user: roottasks:- name: touch filefile: path/opt/zzz.txt statetouch#when: ansible_default_ipv4.address ! 192.168.110.70when: inventory_hostname 192.168.110.70
ansible-playbook demo3.yamlPLAY [third play] **************************************************************TASK [Gathering Facts] *********************************************************
ok: [192.168.110.90]
ok: [192.168.110.70]TASK [touch file] **************************************************************
skipping: [192.168.110.90]
changed: [192.168.110.70]PLAY RECAP *********************************************************************
192.168.110.70 : ok2 changed1 unreachable0 failed0 skipped0 rescued0 ignored0
192.168.110.90 : ok1 changed0 unreachable0 failed0 skipped1 rescued0 ignored0
迭代
Ansible提供了很多种循环结构一般都命名为with_items作用等同于 loop 循环。
vim test3.yaml
---
- name: play1hosts: dbserversgather_facts: falsetasks: - name: create filefile:path: {{item}}state: touchwith_items: [ /opt/a, /opt/b, /opt/c, /opt/d ]- name: play2hosts: dbserversgather_facts: false vars:test:- /tmp/test1- /tmp/test2- /tmp/test3- /tmp/test4tasks: - name: create directoriesfile:path: {{item}}state: directorywith_items: {{test}}- name: play3hosts: dbserversgather_facts: falsetasks:- name: add usersuser: name{{item.name}} statepresent groups{{item.groups}}with_items:- name: test1groups: wheel- name: test2groups: root
或with_items:- {name: test1, groups: wheel}- {name: test2, groups: root}
示例
- name: fouth playhosts: dbserversremote_user: rootvars:myfile:- /opt/a- /opt/b- /opt/c- /opt/dtasks:- name: touch directorywith_items: {{myfile}}file: path{{item}} statedirectory- name: touch filewith_items:- /root/a- /root/b- /root/c- /root/dfile:path: {{item}}state: touch
ansible-playbook demo4.yamlPLAY [fouth play] **************************************************************TASK [Gathering Facts] *********************************************************
ok: [192.168.110.70]TASK [touch directory] *********************************************************
changed: [192.168.110.70] (item/opt/a)
changed: [192.168.110.70] (item/opt/b)
changed: [192.168.110.70] (item/opt/c)
changed: [192.168.110.70] (item/opt/d)TASK [touch file] **************************************************************
changed: [192.168.110.70] (item/root/a)
changed: [192.168.110.70] (item/root/b)
changed: [192.168.110.70] (item/root/c)
changed: [192.168.110.70] (item/root/d)PLAY RECAP *********************************************************************
192.168.110.70 : ok3 changed2 unreachable0 failed0 skipped0 rescued0 ignored0Templates 模块
Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件可以看作是一个编译过的模板文件用来产生目标文本传递Python的变量给模板去替换模板中的标记。
1.先准备一个以 .j2 为后缀的 template 模板文件设置引用的变量
cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2
vim /opt/httpd.conf.j2
Listen {{http_port}} #42行修改
ServerName {{server_name}} #95行修改
DocumentRoot {{root_dir}} #119行修改
2.修改主机清单文件使用主机变量定义一个变量名相同而值不同的变量
vim /etc/ansible/hosts
[webservers]
192.168.110.90 http_port192.168.80.11:80 server_namewww.zxr.com:80 root_dir/var/www/html/zxr[dbservers]
192.168.110.70 http_port192.168.80.12:80 server_namewww.yyds.com:80 root_dir/var/www/html/yyds
3.编写 playbook
- name: sixth playhosts: webserversremote_user: rootvars:- pkg: httpdtasks:- name: install apacheyum: namehttpd statelatest- name: create root dirfile: statedirectory path{{item}}with_items:- /var/www/html/zxr- /var/www/html/yyds- name: create index.html in www.zxr.comcopy: contenth1this is zxr web/h1 dest/var/www/html/zxr/index.htmlwhen: ansible_default_ipv4.address 192.168.110.90- name: create index.html in www.yyds.comcopy: contenth1this is yyds web/h1 dest/var/www/html/yyds/index.htmlwhen: inventory_hostname 192.168.110.70- name: prepare configuration filetemplate: src/opt/playbook/httpd.conf.j2 dest/etc/httpd/conf/httpd.confnotify: reload apache- name: start apacheservice: name{{pkg}} statestarted enabledyeshandlers:- name: reload apacheservice: name{{pkg}} statereloaded
ansible-playbook demo6.yamlPLAY [sixth play] **************************************************************TASK [Gathering Facts] *********************************************************
ok: [192.168.110.90]TASK [install apache] **********************************************************
ok: [192.168.110.90]TASK [create root dir] *********************************************************
ok: [192.168.110.90] (item/var/www/html/zxr)
ok: [192.168.110.90] (item/var/www/html/yyds)TASK [create index.html in www.zxr.com] ****************************************
ok: [192.168.110.90]TASK [create index.html in www.yyds.com] ***************************************
skipping: [192.168.110.90]TASK [prepare configuration file] **********************************************
changed: [192.168.110.90]TASK [start apache] ************************************************************
ok: [192.168.110.90]RUNNING HANDLER [reload apache] ************************************************
changed: [192.168.110.90]PLAY RECAP *********************************************************************
192.168.110.90 : ok7 changed2 unreachable0 failed0 skipped1 rescued0 ignored0
tags 模块
可以在一个playbook中为某个或某些任务定义“标签”在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。 playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时无论执行哪一个tags时定义有always的tags都会执行。
- name: seventh playhosts: dbserversremote_user: roottasks:- name: create abc.txtfile: path/opt/abc.txt statetouchtags:- zxr- name: create bac.txtfile: path/opt/123.txt statetouchtags:- ycx- name: create cba.txtcopy: contentggl like dancing dest/opt/ggl.txttags:- ggl
ansible-playbook dbhosts.yaml --tagszxr
分别去两台被管理主机上去查看文件创建情况
ansible-playbook demo7.yaml --tagszxrPLAY [seventh play] ************************************************************TASK [Gathering Facts] *********************************************************
ok: [192.168.110.70]TASK [create abc.txt] **********************************************************
changed: [192.168.110.70]PLAY RECAP *********************************************************************
192.168.110.70 : ok2 changed1 unreachable0 failed0 skipped0 rescued0 ignored0
示例
用剧本安装lnmp
- name: lnmp playhosts: dbserversremote_user: roottasks:- name: perpare condifurecopy: src/etc/yum.repos.d/nginx.repo dest/etc/yum.repos.d/nginx.repo- name: install nginxyum: namenginx statelatest- name: start nginxservice: namenginx statestarted enabledyes- name: install mysqlyum: namemysql57-community-release-el7-10.noarch.rpm statelatest- name: modify filereplace:path: /etc/yum.repos.d/mysql-community.reporegexp: gpgcheck1replace: gpgcheck0- name: install mysql-community-serveryum: namemysql-community-server statelatest- name: start mysqlservice: namemysqld statestarted enabledyes- name: add yum filecommand: wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm -P /etc/yum.repos.d - name: rpm epelcommand: rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm- name: rpm el7command: rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm- name: install phpcommand: yum install -y php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache- name: start php-fpmservice: namephp-fpm statestarted enabledyes- name: copy configurecopy: src/usr/local/nginx/conf/nginx.conf dest/etc/nginx/conf.d/default.conf- name: restart nginxservice: namenginx statestarted enabledyes
Roles 模块
roles用于层次性、结构化地组织playbook。roles能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令引入即可。 简单来讲roles就是通过分别将变量、文件、任务、模板及处理器放置于单独的目录中并可以便捷的include它们的一种机制。roles一般用于基于主机构建服务的场景中但也可以是用于构建守护进程等场景中。主要使用场景代码复用度较高的情况下。
假如我们现在有3个被管理主机第一个要配置成httpd第二个要配置成haproxy服务器第三个要配置成MySQLmariadb服务器。我们如何来定义playbook 第一个play用到第一个主机上用来构建httpd第二个play用到第二个主机上用来构建haproxy。这些个play定义在playbook中比较麻烦将来也不利于模块化调用不利于多次调用。比如说后来又加进来一个主机这第3个主机既是httpd服务器又是haproxy服务器我们只能写第3个play上面写上安装httpd和haproxy。这样playbook中的代码就重复了。 为了避免代码重复可以定义一个角色叫httpd第二个角色叫haproxy并使用roles实现代码重复被调用。
roles 的目录结构
cd /etc/ansible/
tree roles/
roles/
├── web/ #相当于 playbook 中的 每一个 play 主题
│ ├── files/
│ ├── templates/
│ ├── tasks/
│ ├── handlers/
│ ├── vars/
│ ├── defaults/
│ └── meta/
└── db/├── files/├── templates/├── tasks/├── handlers/├── vars/├── defaults/└── meta/
roles 内各目录含义解释
●files 用来存放由 copy 模块或 script 模块调用的文件。
●templates 用来存放 jinjia2 模板template 模块会自动在此目录中寻找 jinjia2 模板文件。
●tasks 此目录应当包含一个 main.yml 文件用于定义此角色的任务列表此文件可以使用 include 包含其它的位于此目录的 task 文件。
●handlers 此目录应当包含一个 main.yml 文件用于定义此角色中触发条件时执行的动作。
●vars 此目录应当包含一个 main.yml 文件用于定义此角色用到的变量。
●defaults 此目录应当包含一个 main.yml 文件用于为当前角色设定默认变量。 这些变量具有所有可用变量中最低的优先级并且可以很容易地被任何其他变量覆盖。所以生产中我们一般不在这里定义变量
●meta 此目录应当包含一个 main.yml 文件用于定义此角色的元数据信息及其依赖关系。 在一个 playbook 中使用 roles 的步骤
1创建以 roles 命名的目录 mkdir /etc/ansible/roles/ -p #yum装完默认就有
2创建全局变量目录可选 mkdir /etc/ansible/group_vars/ -p touch /etc/ansible/group_vars/all #文件名自己定义引用的时候注意
3在 roles 目录中分别创建以各角色名称命名的目录如 httpd、mysql mkdir /etc/ansible/roles/httpd mkdir /etc/ansible/roles/mysql
4在每个角色命名的目录中分别创建files、handlers、tasks、templates、meta、defaults和vars目录用不到的目录可以创建为空目录也可以不创建 mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta}
5在每个角色的 handlers、tasks、meta、defaults、vars 目录下创建 main.yml 文件千万不能自定义文件名 touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
6修改 site.yml 文件针对不同主机去调用不同的角色
vim /etc/ansible/site.yml
---
- hosts: webserversremote_user: rootroles:- httpd
- hosts: dbserversremote_user: rootroles:- mysql
7运行 ansible-playbook
cd /etc/ansible
ansible-playbook site.yml
示例
mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -ptouch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/php/{defaults,vars,tasks,meta,handlers}/main.yml
编写httpd模块
写一个简单的tasks/main.yml
vim /etc/ansible/roles/httpd/tasks/main.yml
- name: install apacheyum: name{{pkg}} statelatest
- name: start apacheservice: enabledtrue name{{svc}} statestarted
定义变量可以定义在全局变量中也可以定义在roles角色变量中一般定义在角色变量中
vim /etc/ansible/roles/httpd/vars/main.yml
pkg: httpd
svc: httpd
编写mysql模块
vim /etc/ansible/roles/mysql/tasks/main.yml
- name: install mysqlyum: name{{pkg}} statelatest
- name: start mysqlservice: enabledtrue name{{svc}} statestartedvim /etc/ansible/roles/mysql/vars/main.yml
pkg:- mariadb- mariadb-server
svc: mariadb
编写php模块
vim /etc/ansible/roles/php/tasks/main.yml
- name: install phpyum: name{{pkg}} statelatest
- name: start php-fpmservice: enabledtrue name{{svc}} statestarted
vim /etc/ansible/roles/php/vars/main.yml
pkg:- php- php-fpm
svc: php-fpm 编写roles示例
vim /etc/ansible/site.yml
---
- hosts: webserversremote_user: rootroles:- httpd- mysql- php
cd /etc/ansible
ansible-playbook site.yml