
linux企业实战
linux
weixin_43384009
抓住每次可以学习的机会
展开
-
ansible:tasks、meta、vars、templates、handler目录
目录标题1. tasks目录2. 默认情况下无法同时调用多次角色,将allow_duplicates: true则可以多次调用角色3. vars目录4. templates目录5. handler目录1. tasks目录使role的值不再是全局变量[root@server4 ~]# vim /etc/ansible/ansible.cfg # role的值不在是全局变量129 pr...原创 2020-04-03 22:26:59 · 1496 阅读 · 0 评论 -
ansible:jinja2的caller和宏的调用
目录标题1. caller可以将宏中的内容替换2. 宏中调用宏3. caller接收参数1. caller可以将宏中的内容替换[root@server4 jjia]# cat test29.j2 {% macro testfunc() %}test string{{caller()}}{% endmacro %}{% call testfunc() %}westos---hih...原创 2020-04-03 22:16:59 · 393 阅读 · 0 评论 -
ansible:jinja2的宏和参数
目录标题1. 定义宏(函数)2. 定义函数传递参数3. 默认参数4. 注意:没有默认值的参数应该在有默认值参数之前,否则会出错5. 可变参数varargs6. 关键字参数1. 定义宏(函数)[root@server4 jjia]# cat test22.j2 {% macro testfunc() %} test string{% endmacro %}{{ testfunc(...原创 2020-04-03 22:14:31 · 635 阅读 · 0 评论 -
ansible:jinja2对特殊符号的处理
目录标题1. 一些特殊符号用引号将其引起来当作字符串处理2. 多个特殊符号不被jinji2解析的使用{% raw %}...{% endraw %}2. 使双小括号中的变量有意义1. 一些特殊符号用引号将其引起来当作字符串处理[root@server4 jjia]# cat test19.j2 {{ '{{' }}{{ '}}' }}{{ '{{ test wesots }}' }}...原创 2020-04-03 22:08:45 · 2989 阅读 · 0 评论 -
ansible:jinja2的continue、break、do
目录标题让ansible支持jinja2的扩展模块让ansible支持jinja2的扩展模块[root@server4 jjia]# find / -name ansible.cfg/etc/ansible/ansible.cfg[root@server4 jjia]# vim /etc/ansible/ansible.cfg132 jinja2_extensions = jinja...原创 2020-04-03 22:04:12 · 1882 阅读 · 0 评论 -
ansible:jinja2的for和if的使用
目录标题1. for循环2. 使用loop.index获取每次循环的次数3. 使用range4. for和if一起使用5. for、if、loop.index6. for、if、else1. for循环[root@server4 jjia]# cat test10.j2 {% for i in [3,5,2,7] %}{{ i }}{% endfor %}测试:[root@se...原创 2020-04-03 22:01:00 · 3667 阅读 · 0 评论 -
ansible:jinja2的{# #}和{% %}的使用
目录标题1. {# #}注释2. {% %}控制语句if的使用1. {# #}注释[root@server4 jjia]# cat test7.j2 jinja2 westos{#注释信息模板被渲染以后最终的文件中不会包含这些信息#}jinja2 haha测试:[root@server4 jjia]# ansible testB -m template -a "src=tes...原创 2020-04-03 21:53:07 · 1035 阅读 · 0 评论 -
ansible:jinja2模板、{{ }}的使用
目录标题1. jinja2的用法2. {{}}的使用3. 将变量的选取写道文件中,然后在playbook中定义变量并使用文件中选取的内容4. 在jinjia2中使用过滤器5. 测试defined、exists、file、directory1. jinja2的用法2. {{}}的使用变量:[root@server4 ~]# cat test.j2 westos pythonte...原创 2020-04-03 21:50:19 · 1131 阅读 · 0 评论 -
使用ansible对redis的操作
目录标题1. 使用ansible在redis配置文件中写入内容2. 使用playbook实现更改redis的6379.conf配置文件并重起redis1. 使用ansible在redis配置文件中写入内容在ansible主机上安装redistar zxf redis-5.0.3.tar.gz cd redis-5.0.3yum install gccmake MALLOC=libc...原创 2020-04-03 21:42:49 · 658 阅读 · 0 评论 -
ansible:loop模块
目录标题1. loop模块:循环执行语句2. 使用loop和dict替换with_dict3. loop+filter替换loop+loopup3. loop代替with_list,当初处理嵌套的列表时,列表不会被拉平4. flatten过滤器可以替代with_flatten,当处理多层嵌套的列表时,列表中所有的嵌套层级都会被拉平5. flatten过滤器(加参数)可以替代with_items,当...原创 2020-04-03 21:38:39 · 1310 阅读 · 0 评论 -
ansible:block模块
目录标题1. block模块2. 实现任务执行失败返回error3. block中的内容任意一个出错都回执行rescue的内容4. block、rescue和always之间的关系:block执行有误时执行rescue,而always无论block是否成功执行都会执行1. block模块[root@server4 bl]# cat bl1.yml --- - hosts: testB ...原创 2020-03-31 00:34:15 · 1795 阅读 · 0 评论 -
ansible:when(subset、superset、判断一个对象是否是字符串、判断对象是否为一个数字)
目录标题1. subset、superset2. 判断一个对象是否是字符串3. 判断对象是否为一个数字1. subset、superset[root@server4 pd]# cat pd10.yml ---- hosts: testB remote_user: root gather_facts: no vars: a: - 2 - 5 b...原创 2020-03-31 00:31:04 · 1093 阅读 · 0 评论 -
ansible: when(file、directory、link、mount、exists、lower、upper、even、odd、divisibleby、比较)
目录标题1. file、directory、link、mount、exists2. lower、upper3. even、odd、divisibleby4. 比较1. file、directory、link、mount、exists[root@server4 pd]# cat pd6.yml ---- hosts: testB remote_user: root gather_...原创 2020-03-31 00:28:38 · 716 阅读 · 0 评论 -
ansible:when(exists、not exists、defined、undefined、none、success、failure、change、skip)
目录标题1. exists和not exists2. defined、undefined、none3. success、failure、change、skip1. exists和not existsexists:[root@server4 pd]# cat pd3.yml ---- hosts: testB remote_user: root gather_facts: no...原创 2020-03-31 00:23:24 · 3772 阅读 · 0 评论 -
ansible: 判断的使用when(not、 ignore_errors和.rc)
目录标题1. when2. 获取ansible主机的信息3. 判断主机信息是否一致4. not的使用5. ignore_errors和.rc判断1. when[root@server4 pd]# cat pd.yml # 当item大于1时输出---- hosts: testB remote_user: root gather_facts: no tasks: -...原创 2020-03-31 00:18:13 · 3979 阅读 · 0 评论 -
ansible:with_dict、with_subelements、with_file、with_fileglob
目录标题1. with_dict以字典的形式返回结果2. 多层字典嵌套3. 字典嵌套在列表中,with_subelements将hobby中的每个内容和{name,gender}结合4. 使用with_file获取ansible主机的内容,无论你的hosts主机是谁都可以获取5. with_fileglob输出ansible主机下指定目录的所有文件(目录不会输出)1. with_dict以字典...原创 2020-03-31 00:13:56 · 710 阅读 · 0 评论 -
ansible:with_indexed_items、with_sequence、 with_random_choice
目录标题1. with_indexed_items给输出添加数字索引2. with_indexed_items中使用列表嵌套列表3. with_sequence指定序列的开始和结尾以及步长4. 创建偶数数列的目录westos2,westos4,westos6,westos8,westos105. with_random_choice:随机选择一个数返回1. with_indexed_items...原创 2020-03-31 00:07:43 · 397 阅读 · 0 评论 -
ansible:with_list、with_flattened、with_together、with_cartesian、with_nested、with_cartesian
目录标题1. with_list:嵌套列表2. with_flattened和with_items类似3. with_together成对输出(将两个列表对应位置的值合并输出)4. with_cartesian和with_nested实现组排列组合5. with_cartesian实现目录的建立1. with_list:嵌套列表[root@server4 ~]# cat xh5.yml -...原创 2020-03-31 00:02:30 · 802 阅读 · 0 评论 -
ansible:with_items的使用
目录标题1. 在playbook中使用循环with_items2. 自定义with_items的值输出3. 使用item.获取指定值4. 使用with_items建立四个文件:用vars来定义文件名,使用with_items逐个建立5. with_items与shell结合使用1. 在playbook中使用循环with_items[root@server4 ~]# vim /etc/ansi...原创 2020-03-30 23:55:52 · 6944 阅读 · 0 评论 -
ansible:获取ansible的主机信息
目录标题1. 使用ansible_version获取ansible的版本信息2. 使用inventory_hostname获取主机信息3. 获取主机短信息:inventory_hostname_short4. 获取所有的groups清单信息5. 获取其中一个groups的清单信息groups.testA6. 查看某台主机属于哪个分组group_names7. 获取所有未分组的主机8. invent...原创 2020-03-30 23:48:00 · 4147 阅读 · 0 评论 -
ansible:变量调用set_fact和register
目录标题1. 在tasks内部调用变量,使用set_fact2. 使用set_fact调用register中的值3. 只有set_fact定义的变量才能在不同的play中应用,而vars定义的变量不能在不同的play中应用4. register的注册变量也可以传递到不同的play中1. 在tasks内部调用变量,使用set_fact[root@server4 ~]# cat bl2.yml ...原创 2020-03-30 23:43:23 · 3109 阅读 · 0 评论 -
ansible:在playbook之外传入变量值
目录标题1. 在playbook之外给变量传值2. 在playbook之外给变量传多个值3. 在playbook之外给变量传值的优先级大于playbook之中定义的变量4. 直接用命令行输出变量5. 使用playbook调用文件中写入的变量1. 在playbook之外给变量传值[root@server4 ~]# cat bl.yml ---- hosts: testB remote_...原创 2020-03-30 23:38:19 · 530 阅读 · 0 评论 -
ansible:注册变量、vars_prompt、增加用户和设置密码
目录标题1. 注册变量2. vars_prompt来实现人机交互3. 增加用户和设置密码1. 注册变量将shell中命令的执行结果,给register中的变量testvar[root@server4 ~]# cat register.yml ---- hosts: testB remote_user: root tasks: - name: test shell:...原创 2020-03-26 16:48:15 · 2763 阅读 · 0 评论 -
ansible:debug模块
目录标题debug模块2. debug模块与变量结合3. 将获取信息模块和playbook结合debug模块[root@server4 ~]# cat debug.yml ---- hosts: testB remote_user: root tasks: - name: touch file file: path: /testdir/testfile...原创 2020-03-26 16:39:37 · 1795 阅读 · 0 评论 -
ansible:vars
目录标题1. ansible的变量2. vars_files2. 获取信息1. ansible的变量[root@server4 ~]# cat vars.yml ---- hosts: testB vars: testvar1: testfile # 或者- testvar1: testfile remote_user: root tasks: - na...原创 2020-03-26 16:35:03 · 609 阅读 · 0 评论 -
ansible:tags模块
目录标题1. tags1.1 always1.2 关于tags的其它标签用法:tagged、untagged、all1.3 nerver1. tags只执行某个任务而不是全部任务,使用打标签的方法tags格式一:[root@server4 ~]# cat mkdir.yml ---- hosts: testB remote_user: root tasks: - na...原创 2020-03-26 16:20:08 · 330 阅读 · 0 评论 -
ansible:handlers、meta、listen
目录标题1. 修改httpd的80端口并重起httpd2. handlers3. meta4. listen1. 修改httpd的80端口并重起httpd实验一:[root@server4 ~]# cat httpd.yml ---- hosts: testB remote_user: root tasks: - name: modify the configuration...原创 2020-03-26 16:08:13 · 1025 阅读 · 0 评论 -
ansible:playbook
目录标题1. playbook1. playbook[root@server4 ~]# vim ping.yml # 注意:以空格作为缩进---- hosts: testB # 主机名 remote_user: root # 用户 tasks: # 执行的任务 - name: Ping the host ping: - name: make di...原创 2020-03-23 20:52:51 · 96 阅读 · 0 评论 -
ansible:包管理模块---yum_repository、yum、
目录标题1. yum_repository模块2. yum模块1. yum_repository模块# 配置yum源[root@server4 testdir]# ansible testB -m yum_repository -a "name=rhel7.5 description='ansible_yum' baseurl=http://172.25.60.250/rhel7....原创 2020-03-23 20:51:08 · 292 阅读 · 0 评论 -
ansible:系统模块---group模块
目录标题group模块group模块# 创建组[root@server4 testdir]# ansible testB -m group -a "name=hjf"[root@server3 opt]# cat /etc/grouphjf:x:1007:# 删除用户组,需要注意不能用用户将此组作为主组[root@server4 testdir]# ansible test...原创 2020-03-23 20:45:37 · 184 阅读 · 0 评论 -
ansible:系统模块---cron模块、service模块和user模块
目录标题1. cron模块2. service模块3. user模块1. cron模块# 创建名字为test_cron,时间为每天的1时5分,执行echo test[root@server4 testdir]# ansible testB -m cron -a "name='test_cron' minute=5 hour=1 job='echo test'"[root@...原创 2020-03-23 20:42:34 · 239 阅读 · 0 评论 -
ansible:script模块
目录标题script模块script模块[root@server4 testdir]# sh test.sh hello ansible[root@server4 testdir]# cat test.sh #!/bin/bashecho hello ansible[root@server4 testdir]# ansible testB -m script -a "chdir...原创 2020-03-23 20:37:27 · 491 阅读 · 0 评论 -
ansible:shell模块
目录标题shell模块shell模块# 在/testdir中的test文件中写入hello[root@server4 testdir]# ansible testB -m shell -a "chdir=/testdir echo hello > test"server3 | CHANGED | rc=0 >>...原创 2020-03-23 20:36:23 · 188 阅读 · 0 评论 -
ansible:command模块
目录标题command模块command模块# 查看/testdir目录下的内容[root@server4 testdir]# ansible testB -m command -a "chdir=/testdir ls"# creates表示test存在就不执行[root@server4 testdir]# ansible testB -m command -a "create...原创 2020-03-23 20:35:21 · 179 阅读 · 0 评论 -
ansible:replace模块
目录标题replace模块replace模块# 将文件test中所有的test替换为TEST[root@server4 testdir]# ansible testB -m replace -a "path=/testdir/test regexp='test' replace='TEST'# 将源文件备份[root@server4 testdir]# ansible testB...原创 2020-03-23 20:34:06 · 490 阅读 · 0 评论 -
ansible:find模块
目录标题find模块find模块ctime:change time — ls -lcatime:access time — ls -lumtime: modify time — ls -l# 查找/testdir 目录中内容有abc的文件[root@server4 testdir]# ansible testB -m find -a "path=/testdir con...原创 2020-03-23 20:33:00 · 1018 阅读 · 0 评论 -
ansible:lineinfile模块
目录标题lineinfile模块lineinfile模块# 如果test文件中不存在test test,则将其添加到末尾[root@server4 testdir]# ansible testB -m lineinfile -a "path=/testdir/test line='test test'"# 将line开头的行替换为test test[root@server4 ...原创 2020-03-23 20:31:13 · 215 阅读 · 0 评论 -
ansible:blockinfile模块
目录标题blockinfile模块blockinfile模块[root@server3 testdir]# cp /etc/rc.d/rc.local .[root@server3 testdir]# ls1 a c hardwestos linkwestos rc.local westos# 默认在文件末尾插入[root@server4 testdir]...原创 2020-03-23 20:29:50 · 466 阅读 · 0 评论 -
ansible:file模块
目录标题file模块file模块# 在testB中创建/testdir目录[root@server4 testdir]# ansible testB -m file -a "path=/testdir state=directory"# 在testB中/testdir/目录下创建westos文件[root@server4 testdir]# ansible testB -m...原创 2020-03-23 20:19:52 · 227 阅读 · 0 评论 -
ansible:copy模块
目录标题1. copy模块1. copy模块[root@server4 testdir]# ansible testB -m copy -a "src=/testdir/copytest dest=/opt" # 将ansible主机的/testdir/copytest文件放到testB的dest=/opt中server3 | CHANGED => { "ans...原创 2020-03-23 20:12:41 · 357 阅读 · 0 评论