前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ansible-语法

ansible-语法

作者头像
丁D
发布2022-08-12 20:38:27
5360
发布2022-08-12 20:38:27
举报
文章被收录于专栏:老铁丁D

定义变量

使用vars关键字

代码语言:javascript
复制
--- 
- hosts: test70 
vars: 
testvar1: testfile 
remote_user: root 
tasks: 
- name: task1 
file: 
path: /testdir/{{ testvar1 }} 
state: touch 

上述使用vars定义一个变量testvar1 并使用{{ testvar1 }}来引用

代码语言:javascript
复制
vars: 
testvar1: testfile 
testvar2: testfile2 

上述定义多个变量

代码语言:javascript
复制
--- 
- hosts: test70 
remote_user: root 
vars: 
nginx: 
conf80: /etc/nginx/conf.d/80.conf 
conf8080: /etc/nginx/conf.d/8080.conf 
tasks: 
- name: task1 
file: 
path: "{{nginx.conf80}}" 
state: touch 
- name: task2 
file: 
path: "{{nginx.conf8080}}" 
state: touch 

上述playbook 引用变量 “{{nginx.conf8080}}” 如果让在开头要用引号

变量文件分离

可以在某个文件定义变量,,然后playbook引用该文件,关键字 vars_files 必须用-开头

代码语言:javascript
复制
# cat nginx_vars.yml 
nginx: 
conf80: /etc/nginx/conf.d/80.conf 
conf8080: /etc/nginx/conf.d/8080.conf 
--- 
- hosts: test70 
remote_user: root 
vars_files: 
- /testdir/ansible/nginx_vars.yml 
tasks: 
- name: task1 
file: 
path={{nginx.conf80}} 
state=touch 
- name: task2 
file: 
path={{nginx['conf8080']}} 
state=touch 

收集远程节点的信息

ansible每运行一个playbook默认会运行一个【Gathering Facts】任务,通过这个任务可以收集远程主机的信息(如ip地址,主机名,系统版本,硬件配置等) 当我们想要查看【Gathering Facts】任务收集的信息需要使用setup模块

代码语言:javascript
复制
ansible test70 -m setup 

alt
alt

上面 返回的 内容很多不好查看,可以使用下面的filter进行过滤, 也可以使用通配符

代码语言:javascript
复制
ansible test70 -m setup -a 'filter=ansible_memory_mb' 
ansible test70 -m setup -a "filter=*mb*" 

循环

使用with_items来处理循环,使用item来获取每一个循环。

代码语言:javascript
复制
//例子1: 通过循环输出为分组的信息 
--- 
- hosts: test70 
remote_user: root 
gather_facts: no 
tasks: 
- debug: 
msg: "{{item}}" 
with_items: "{{groups.ungrouped}}" 
//例子2: 通过debugger输出循环 1 2 3 
--- 
- hosts: test70 
remote_user: root 
gather_facts: no 
tasks: 
- debug: 
msg: "{{item}}" 
with_items: 
- 1 
- 2 
- 3 
//例子3 通过循环创建4个file 
--- 
- hosts: test70 
remote_user: root 
gather_facts: no 
vars: 
dirs: 
- "/opt/a" 
- "/opt/b" 
- "/opt/c" 
- "/opt/d" 
tasks: 
- file: 
path: "{{item}}" 
state: touch 
with_items: "{{dirs}}" 
//例子4 循环体是对象信息 
--- 
- hosts: test70 
remote_user: root 
gather_facts: no 
tasks: 
- debug: 
msg: "{{item.test1}}" 
with_items: 
- { test1: a, test2: b } 
- { test1: c, test2: d } 

逻辑判断

使用when来处理逻辑判断 不是if

代码语言:javascript
复制
//ansible_distribution 是一个变量正常来说是要{{ansible_distribution }}来引用但是在when中不用 
//例子2 只有ansible_distribution 等于 "CentOS"才输出 "System release is centos" 
--- 
- hosts: test70 
remote_user: root 
tasks: 
- debug: 
msg: "System release is centos" 
when: ansible_distribution == "CentOS" 
//例子2 当item 的值大于1才输出 
--- 
- hosts: test70 
remote_user: root 
gather_facts: no 
tasks: 
- debug: 
msg: "{{ item }}" 
with_items: 
- 1 
- 2 
- 3 
when: item > 1 
//例子3 多个条件 
--- 
- hosts: test70 
remote_user: root 
tasks: 
- debug: 
msg: "System release is centos7" 
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7" 

“==“ :比较两个对象是否相等,相等为真 “!=“ :比较两个对象是否不等,不等为真 “>” :比较两个值的大小,如果左边的值大于右边的值,则为真 “<“ :比较两个值的大小,如果左边的值小于右边的值,则为真 “>=“ :比较两个值的大小,如果左边的值大于右边的值或左右相等,则为真 “<=“ :比较两个值的大小,如果左边的值小于右边的值或左右相等,则为真 and :逻辑与,当左边与右边同时为真,则返回真 or :逻辑或,当左边与右边有任意一个为真,则返回真 not :取反,对一个操作体取反 ( ) :组合,将一组操作体包装在一起,形成一个较大的操作体 in/not in可以判断字符串是否是子字符串(注意整个判断必须引号起来,,关键字也要引号)

代码语言:javascript
复制
when: '"no such file" in super_status.stdout' 
//判断是否含有字符串(从而判断supervisor是否启动supervisor 这个是弱智方法) 
- name: juge supervisor status 
shell: supervisorctl status 
register: super_status 
ignore_errors: yes 
- name: print info 
debug: var=super_status 
- name: start supervisor 
shell: /usr/bin/supervisord -c /etc/supervisor/supervisord.conf 
when: '"no such file" in super_status.stdout' 
改造后的2020.06. 
- name: juge supervisor status 
shell: ps -ef | grep '/usr/bin/supervisord -c /etc/supervisor/supervisord.conf' | grep -v grep | wc -l 
register: super_status 
- name: 'print info super_status' 
debug: 
msg: var=super_status 
- name: start supervisor 
shell: /usr/bin/supervisord -c /etc/supervisor/supervisord.conf 
when: super_status.stdout|int < 1 
//debug引用register变量使用var关键字 并且不能跟msg同时使用 
--- 
- name: chek supervisor is exists 
stat: path=/usr/bin/supervisord 
register: supervisor_bin 
- name: install pip 
easy_install: name=supervisor 
when: not supervisor_bin.stat.exists 
tags: install_pip 
- name: print info 
debug: var=supervisor_bin 

liunx vim编辑时查找关键字

代码语言:javascript
复制
在命令模式下敲斜杆( / )这时在状态栏(也就是屏幕左下脚)就出现了 “/” 然后输入你要查找的关键字敲回车就可以了。 
如果你要继续查找此关键字,敲字符 n 就可以继续查找了。 

ansible环境变量问题

代码语言:javascript
复制
ansible这类远程执行的non-login shell 并不会加载/etc/profile和~/.bash_profile下的环境变量 
只是加载“~/.bashrc”和/etc/bashrc 如果需要在ansible中执行需要特定环境变量的命令,可以将环境变量写在~/.bashrc 并 source一下~/.bash_profile 。 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-04-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 定义变量
    • 变量文件分离
      • 收集远程节点的信息
      • 循环
      • 逻辑判断
        • ansible环境变量问题
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档