设为首页 收藏本站
查看: 995|回复: 0

[经验分享] Ansible详解(二)

[复制链接]

尚未签到

发表于 2018-7-29 07:49:32 | 显示全部楼层 |阅读模式
  一、YAML简介
  二、Ansible组件
  三、主机清单Invetory
  四、PlayBook介绍
  一、YAML简介
  http://www.yaml.org
  YAML:可以使用简单清单,散列表,标题等数据结构。
  YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表、标量等数据结构。其结构(Structure)通过空格来展示,序列(Sequence)里的项用"-"来代表,Map里的键值对用":"分隔。下面是一个示例。
  yet another markup language //仍是一种标记语言
  name: John kona //key为name,value为John,kona
  age: 41
  gender: Male
  spouse: //kv可以嵌套,kev:value(key:value)
  name: Jane Smith
  age: 37
  gender: Female //k/v可以嵌套
  children:
  -   name: Jimmy Smith //-:列表,后面的都是列表元素
  age: 17        //name和age,gender都是children的键key
  gender: Male
  -   name: Jenny Smith
  age 13
  gender: Female
  YAML文件扩展名通常为.yaml,如example.yaml。
  1)list
  列表的所有元素均使用“-”打头,例如:
  # A list of tasty fruits
  - Apple
  - Orange
  - Strawberry
  - Mango
  2)dictionary
  字典通过key与value进行标识 //多个key-value联合起来就叫做字典
  例如:
  ---
  # An employee record
  name: Example Developer
  job: Developer
  skill: Elite
  也可以将key:value放置于{}中进行表示,例如:
  ---
  # An employee record
  {name: Example Developer, job: Developer, skill: Elite}
  二、Ansible组件
  Inventory
  Modules
  Ad Hoc Commands
  Playbooks组件:
  Tasks任务
  Variables变量,ansible有自带的变量,也可以自定义
  Templates模板,包含了模板语法的文本文件;
  Handlers处理器,特定条件下才会被触发的任务
  Roles角色
  三、主机清单Invetory
  1.主机与组:/etc/ansible/hosts
  Inventory
  ansible的默认的inventory file为/etc/ansible/hosts。
  inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成。
  inventory文件格式
  inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中;此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明。
  ntp.wolf.com
  [webservers]
  www1.wolf.com:2222
  www2.wolf.com
  [dbservers]
  db1.wolf.com
  db2.wolf.com
  db3.wolf.com
  如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机,例如:
  [webservers]
  www[01:50].example.com
  [databases]
  db-[a:f].example.com
  2.主机变量
  可以在inventory中定义主机时为其添加主机变量以便于在playbook中使用。例如:
  [webservers]
  www1.wolf.com http_port=80 maxRequestsPerChild=808
  www2.wolf.com http_port=8080 maxRequestsPerChild=909
  3.组变量
  组变量是指赋予给指定组内所有主机上的在playbook中可用的变量。例如:
  [webservers]
  www1.wolf.com
  www2.wolf.com
  [webservers:vars]
  ntp_server=ntp.wolf.com
  nfs_server=nfs.wolf.com
  4.组嵌套
  inventory中,组还可以包含其它的组,并且也可以向组中的主机指定变量。不过,这些变量只能在ansible-playbook中使用,而ansible不支持。例如:
  [apache]
  httpd1.wolf.com
  httpd2.wolf.com
  [nginx]
  ngx1.wolf.com
  ngx2.wolf.com
  [webservers:children]
  apache
  nginx
  [webservers:vars]
  ntp_server=ntp.wolf.com
  5.inventory参数
ansible基于ssh连接inventory中指定的远程主机时,还可以通过参数指定其交互方式;这些参数如下所示:  
ansible_ssh_host //将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
  
ansible_ssh_port  //ssh端口号.如果不是默认的端口号,通过此变量设置.
  
ansible_ssh_user  //默认的 ssh 用户名
  
ansible_ssh_pass  //ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
  
ansible_sudo_pass  //sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)
  
ansible_sudo_exe (new in version 1.8)  // sudo 命令路径(适用于1.8及以上版本)
  
ansible_connection  // 与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.
  
ansible_ssh_private_key_file //ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.
  
ansible_shell_type //目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 或 'fish'.
  
ansible_python_interpreter
  
    目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如  \*BSD, 或者 /usr/bin/python
  
    不是 2.X 版本的 Python.我们不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,且要求 "python" 可执行程序名不可为 python以外的名字(实际有可能名为python26).
  
    与 ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....
  
[webservers]  //组名用[ ]
  
foo.example.com
  
bar.example.com
  
[dbservers]
  
badwolf.example.com:5309  //端口号不是默认22
  
jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50  //jumper是主机名,ansible_ssh_port和ansible_ssh_port是主机变量,适用于单个主机
  
other1.example.com     ansible_connection=ssh        ansible_ssh_user=mpdehaan //指定用户名
  
localhost              ansible_connection=local
  
[atlanta]
  
host1 http_port=80 maxRequestsPerChild=808  //主机变量
  
host2 http_port=303 maxRequestsPerChild=909
  
一个系统可以属于不同的组,比如一台服务器可以同时属于 webserver组 和 dbserver组.这时属于两个组的变量都可以为这台主机所用,至于变量的优先级关系将于以后讨论.
  ==============================================================================
  四、PlayBook介绍
  Playbook的基础组件:
  Hosts:运行指定任务的目标主机 //多个冒号分割
  remote_user:在远程主机上执行任务的用户
  sudo_user: //指定切换的身份
  tasks:任务列表
  模块,模块参数:自上而下运行
  格式:
  action:module arguments
  motuled:arguments //新版本的
  注意:shell和command模块后面直接跟命令,而非key=value类的参数列表
  1.某任务的状态在运行后为changed时,可通过notify通知给handlers;
  2.任务可以通过"tags"打标签,而后在ansible-playbook命令上使用-t指定
  1.第一个playbook
  [root@localhost ~]# cat a.yaml //格式严格对齐
  [root@localhost ~]# cat a.yaml
  - hosts: testweb
  remote_user: root
  tasks:
  - name: crete a user3
  user: name=user3 system=true uid=306
  -  hosts: testweb2
  remote_user: root
  tasks:
  - name: crete a user4
  user: name=user4 system=true uid=307
  [root@localhost ~]#
  //注运行顺序:先运行user3,在运行user4 task
  //空格,不是tab对齐。
  ansible-playbook
  -v verbose
  -s sudo
  --list-hosts //只输出匹配到的主机,而不返回任何命令
  --check //不做修改,仅测试在远程主机上将会改变什么,干跑一遍
  ansible-playbook --check a.yaml //格式必须对齐
  //提示OK=3,有一个是远程主机的facts变量
  ansible-doc -s setup
  ansible 192.168.4.106 -m setup //收集远程主机的变量信息
  2.tasks示例   
  - hosts: websrvs
  remote_user: root
  tasks:
  - name: install httpd package
  yum: name=httpd state=present
  - name: install configure file
  copy: src=files/httpd.conf dest=/etc/httpd/conf/
  - name: start service httpd
  service: name=httpd state=started
  - name: execute ss command
  shell: ss -tnl |grep 80
  ansible all -m yum -a "name=httpd state=absent" //卸载目标主机的httpd
  ansible websrvs -m shell -a "ss -tnl |grep 80"
  //ansible 可以与运行多遍,不管目标主机是否已经执行该操作
  3.handler:
  任务,在特定条件下触发
  接受到其他任务的通知时被触发
  某任务的状态为changed时,可通过notify通知相应的handlers
- hosts: websrvs  
  remote_user: root
  
  tasks:
  
  - name: install httpd package
  
    yum: name=httpd state=present
  
  - name: install configure file
  
    copy: src=files/httpd.conf dest=/etc/httpd/conf/
  
    notify: restart httpd        //只有在notify发生改变的时候,重复执行才会触发handler,
  
  - name: start service httpd
  
    service: name=httpd state=started
  
  handlers:
  
  - name: restart httpd
  
    service: name=httpd state=restarted
  
//只有在配置文件发生改变的时候,handlers才会被触发
  
    handlers:--> changed //发生了改变,实验只是修改了httpd.conf文件
  
    handlers:-->ok //文件没有发生改变
  注:会重复执行这些操作{即使已经操作过了},install ,install和start //重复操作,可以打标签,指定只运行哪些操作
  例如:发现目标httpd已经安装了,不再重新安装而已
  4.tag
  ansible-playboot -t instconf --check a.yaml //-t可以指定多个标签
[root@localhost ~]# cat b.yaml  
- hosts: websrvs
  
  remote_user: root
  
  tasks:
  
  - name: install httpd package
  
    yum: name=httpd state=present
  
    tags: package
  
  - name: install configure file
  
    copy: src=files/httpd.conf dest=/etc/httpd/conf/
  
    tags: package        //两个tags可以同名,将会被同时执行上
  
    notify: restart httpd
  
  - name: start service httpd
  
    service: name=httpd state=started
  
    tags: start
  
  handlers:
  
  - name: restart httpd
  
    service: name=httpd state=restarted
  ansible-playbook --tag package b.yaml
  5.变量: variables:
  变量来源:
  来源一.inventory file中定义的变量
  来源二.playbook中定义的变量
  来源三.include文件和角色中定义的变量
  来源四.系统facts变量,可以通过 ansible hostname -m setup来获取。当然你也可以通过安装拓展包来获取更多的系统变量信息。例如拓展facter和ohai。
  来源五.local facts
  1.facts:由setup模块提供,可直接调用
  ansible-doc -s setup
  关闭facts:
  - host: all
  gather_facts: no
  ansible <hostname> -m setup -a &quot;filter=ansible_local&quot;
  在template和playbook中访问该数据:{{ ansible_local.preferences.general.asdf }} /{{ ansible_eth0.ipv4.address }}
  在ansible中缓存facts使用redis //yum install redis &&start
  [defaults]
  gathering = smart
  fact_caching = redis
  fact_caching_connection = /path/to/cachedir
  fact_caching_timeout = 86400
  # seconds
  2.ansible-playbook命令行中的自定义变量
  -e ARGS,--extra-vars=VARS
  3.通过roles传递变量
  4.Host Inventory //可以向不同主机传递不同的变量
  a.向不同的主机传递不同的变量
  b.向组中的主机传递相同的变量
  [testvaer] //建立一个新的组,然后添加
  http_port=8080
  192.168.4.100
  192.168.4.112   //这样这两个主机使用的变量都一样了
  注:组可以嵌套
  ansible-playbook -e pkname=memcached --check c.yaml
[root@localhost ~]# cat c.yaml  
- hosts: websrvs
  
  remote_user: root
  
  tasks:
  
  - name: install {{ pkname }}
  
    yum: name={{ pkname }} state=present
  =========================================
  每个主机使用不同的变量,被赋予不同的主机名
  ansible-playbook --check c.yaml
[root@localhost ~]# cat c.yaml  
- hosts: websrvs
  
  remote_user: root
  
  tasks:
  
  - name: install {{ hname }}
  
    hostname: name={{ hname }}
  [root@localhost ~]# cat /etc/ansible/hosts
  # This is the default ansible 'hosts' file.
  [websrvs]
  192.168.4.110 hname=www1
  192.168.4.106 hname=www2
  参考:http://www.ansible.com.cn/docs/intro_inventory.html#inventoryformat
  注:template和role后再后续介绍

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-542774-1-1.html 上篇帖子: Ansible 输出结果保存至 excel 下篇帖子: Ansible详解(三)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表