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

[经验分享] 自动化运维工具ansible playbook和roles的使用

[复制链接]

尚未签到

发表于 2018-7-30 11:57:43 | 显示全部楼层 |阅读模式
  ansible的结构:
  Inventory 用来定义被控制端
  Modules 定义被控制端可用的操作
  Ad Hoc Commands 定义被控制端可以执行命令的
  Playbook 批量运行的方式
  Tasks:
  任务:由各模块所支持执行的特定操作;可以通过ansible-doc module_name来查看帮助文档,非常详细
  -m  user -a ‘name= password=’
  Variables:
  变量
  Templates:
  模板:(如执行httpd服务时,各节点上httpd的配置文件内容都不一样,如何让各节点的配置都不同。是通过定义好配置文件模板)
  文本文件模板:使用模板语言来定义;
  Handlers:
  处理器:
  事先定义好的可以在某些条件下被触发的操作;
  Roles:
  角色:
  层次型组织playbook及其所依赖的各种资源的一种机制;
  角色可被单独调用;
  playbooks
  Contain one or more plays
  Written in YAML   文件时yaml格式的
  Declarative config
  not code
  Executed in the order in is written (aka Imperative)
  语法格式
  - 表示列表中的一项,具有相同缩进格式的是同一个列表中的元素
  - name: deploy web server
  user: foouser
  sudo: True
  hosts: all
  tasks:
  -name: install apache
  apt: pkg=apache2-mpm-prefork state=latest
  一、YAML
  1.1 YAML介绍
  YAML是一个可读性高的用来表达资料序列的格式。YAML参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822等。Clark Evans在2001年在首次发表了这种语言,另外Ingy dot Net与Oren Ben-Kiki也是这语言的共同设计者。
  YAML Ain't Markup Language,即YAML不是XML。不过,在开发的这种语言时,YAML的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。其特性:
  YAML的可读性好
  YAML和脚本语言的交互性好
  YAML使用实现语言的数据类型
  YAML有一个一致的信息模型
  YAML易于实现
  YAML可以基于流来处理
  YAML表达能力强,扩展性好
  更多的内容及规范参见http://www.yaml.org。
  1.2 YAML语法
  YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表、标量等数据结构。其结构(Structure)通过空格来展示,序列(Sequence)里的项用"-"来代表中的某一个元素,Map里的键值对用":"分隔。下面是一个示例。
  name: John Smith  键值对
  age: 41
  gender: Male
  spouse:
  name: Jane Smith
  age: 37
  gender: Female
  children:
  -   name: Jimmy Smith
  age: 17
  gender: Male
  -   name: Jenny Smith
  age 13
  gender: Female
  YAML文件扩展名通常为.yaml,如example.yaml。  .yml也行
  1.2.1 list
  列表的所有元素均使用“-”打头,例如:-和元素之间有一个空格
  # A list of tasty fruits
  - Apple
  - Orange
  - Strawberry
  - Mango
  1.2.2 dictionary
  字典通过key与valuef进行标识,例如:
  ---
  # An employee record
  name: Example Developer
  job: Developer
  skill: Elite
  也可以将key:value放置于{}中进行表示,例如:
  ---
  # An employee record
  {name: Example Developer, job: Developer, skill: Elite}
  二、Ansible基础元素
  2.1 变量
  2.1.1 变量命名
  变量名仅能由字母、数字和下划线组成,且只能以字母开头。
  2.1.2 facts
  facts是由正在通信的远程目标主机发回的信息,这些信息被保存在ansible变量中。要获取指定的远程主机所支持的所有facts,可使用如下命令进行:
  # ansible hostname -m setup
  setup     Gathers facts about remote hosts  收集关于远程主机的信息
  例如
  # ansible 192.168.20.161 -m setup
  [root@node1 ~]# ansible constrol -m setup
  2.1.3 register
  把任务的输出定义为变量,然后用于其他任务,示例如下:
  tasks:
  - shell: /usr/bin/foo
  register: foo_result
  ignore_errors: True
  2.1.4 通过命令行传递变量
  在运行playbook的时候也可以传递一些变量供playbook使用,示例如下:
  ansible-playbook test.yml --extra-vars "hosts=www user=mageedu"
  2.1.5 通过roles传递变量
  当给一个主机应用角色的时候可以传递变量,然后在角色内使用这些变量,示例如下:
  - hosts: webservers
  roles:
  - common
  - { role: foo_app_instance, dir: '/web/htdocs/a.com',  port: 8080 }
  2.2 Inventory
  ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory file中将其分组命名。默认的inventory file为/etc/ansible/hosts。
  inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成。
  2.2.1 inventory文件格式
  inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中;此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明。
  ntp.magedu.com
  [webservers]
  www1.magedu.com:2222
  www2.magedu.com
  [dbservers]
  db1.magedu.com
  db2.magedu.com
  db3.magedu.com
  如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机,例如:
  [webservers]
  www[01:50].example.com
  [databases]
  db-[a:f].example.com
  2.2.2 主机变量
  可以在inventory中定义主机时为其添加主机变量以便于在playbook中使用。例如:
  [webservers]
  www1.magedu.com http_port=80 maxRequestsPerChild=808
  www2.magedu.com http_port=8080 maxRequestsPerChild=909
  2.2.3 组变量
  组变量是指赋予给指定组内所有主机上的在playbook中可用的变量。例如:
  [webservers]
  www1.magedu.com
  www2.magedu.com
  [webservers:vars]
  ntp_server=ntp.magedu.com
  nfs_server=nfs.magedu.com
  2.2.4 组嵌套
  inventory中,组还可以包含其它的组,并且也可以向组中的主机指定变量。不过,这些变量只能在ansible-playbook中使用,而ansible不支持。例如:
  [apache]
  httpd1.magedu.com
  httpd2.magedu.com
  [nginx]
  ngx1.magedu.com
  ngx2.magedu.com
  [webservers:children]
  apache
  nginx
  [webservers:vars]
  ntp_server=ntp.magedu.com
  2.2.5 inventory参数
  ansible基于ssh连接inventory中指定的远程主机时,还可以通过参数指定其交互方式;这些参数如下所示:
  ansible_ssh_host
  The name of the host to connect to, if different from the alias you wish to give to it.
  ansible_ssh_port
  The ssh port number, if not 22
  ansible_ssh_user
  The default ssh user name to use.
  ansible_ssh_pass
  The ssh password to use (this is insecure, we strongly recommend using --ask-pass or SSH keys)
  ansible_sudo_pass
  The sudo password to use (this is insecure, we strongly recommend using --ask-sudo-pass)
  ansible_connection
  Connection type of the host. Candidates are local, ssh or paramiko.  The default is paramiko before Ansible 1.2, and 'smart' afterwards which detects whether usage of 'ssh' would be feasible based on whether ControlPersist is supported.
  ansible_ssh_private_key_file
  Private key file used by ssh.  Useful if using multiple keys and you don't want to use SSH agent.
  ansible_shell_type
  The shell type of the target system. By default commands are formatted using 'sh'-style syntax by default. Setting this to 'csh' or 'fish' will cause commands executed on target systems to follow those shell's syntax instead.
  ansible_python_interpreter
  The target host python path. This is useful for systems with more than one Python or not located at "/usr/bin/python" such as \*BSD, or where /usr/bin/python is not a 2.X series Python.  We do not use the "/usr/bin/env" mechanism as that requires the remote user's path to be set right and also assumes the "python" executable is named python, where the executable might be named something like "python26".
  ansible\_\*\_interpreter
  Works for anything such as ruby or perl and works just like ansible_python_interpreter. This replaces shebang of modules which will run on that host.
  下面就用一个yaml文件来练练手
  让节点安装httpd,且安装后要开机自动启动,如何写playbooks
  #vim web.yaml
  -    name: web servers      剧本的名称,用于描述剧本自己的结构
  remote_user: root      远程主机上以那个用户的身份运行
  hosts: constrol                 此剧本运用于哪些主机
  tasks:此剧本有多个部分内容
  -    name: install httpd (- name(中间有一个空格))
  yum:name=httpd state=present (用什么来安装)
  -    name: httpd service
  service: name=httpd enabled=yes state=started (这些选项是在service模块下的内容)
  运行playbooks
  #ansible-playbook web.yaml
  [root@node1 ~]# cat web.yaml  这个是yaml文件的内容
  - name : web server
  remote_user : root
  hosts : constrol
  tacks :
  - name : install httpd
  yum : name=httpd   这里可以不用写state,因为默认为preset就是安装
  - name : start httpd
  service : name=httpd enabled=yes state=started
  [root@node1 ~]# ansible-playbook web.yaml
  ERROR: tacks is not a legal parameter of an Ansible Play  提示tacks参数出错,是没有tacks,应该是tasks,会自动检测语法
  [root@node1 ~]# vim web.yaml
  [root@node1 ~]# ansible-playbook web.yaml  运行.yaml
  PLAY [web server] *************************************************************
  GATHERING FACTS ***************************************************************
  ok: [192.168.21.234]
  ok: [192.168.21.230]
  TASK: [install httpd] *********************************************************
  changed: [192.168.21.230]
  changed: [192.168.21.234]
  TASK: [start httpd] ***********************************************************
  changed: [192.168.21.234]
  changed: [192.168.21.230]
  PLAY RECAP ********************************************************************
  192.168.21.230             : ok=3    changed=2    unreachable=0    failed=0
  192.168.21.234             : ok=3    changed=2    unreachable=0    failed=0
  这里ok=3就是  安装ok  开机启动ok  启动ok
  [root@node1 ~]# cat web.yaml  修改后的.yaml
  - name : web server
  remote_user : root
  hosts : constrol
  tasks :
  - name : install httpd
  yum : name=httpd
  - name : start httpd
  service : name=httpd enabled=yes state=started
  [root@node1 ~]# ansible constrol -m command -a 'rpm -q httpd'  安装ok
  192.168.21.230 | success | rc=0 >>
  httpd-2.2.15-39.el6.centos.x86_64
  192.168.21.234 | success | rc=0 >>
  httpd-2.2.15-39.el6.centos.x86_64
  [root@node1 ~]# ansible constrol -m command -a 'service httpd status'  运行ok
  192.168.21.234 | success | rc=0 >>
  httpd (pid  6871) is running...
  192.168.21.230 | success | rc=0 >>
  httpd (pid  24315) is running...
  [root@node1 ~]# ansible constrol -m command -a 'chkconfig --list httpd'  开机自启动ok
  192.168.21.234 | success | rc=0 >>
  httpd              0:off    1:off    2:on    3:on    4:on    5:on    6:off
  192.168.21.230 | success | rc=0 >>
  httpd              0:off    1:off    2:on    3:on    4:on    5:on    6:off
  3.3 条件测试
  如果需要根据变量、facts或此前任务的执行结果来作为某task执行与否的前提时要用到条件测试。
  3.3.1 when语句
  在task后添加when子句即可使用条件测试;when语句支持Jinja2表达式语法。例如:
  tasks:
  - name: "shutdown Debian flavored systems"
  command: /sbin/shutdown -h now
  when: ansible_os_family == "Debian"  条件
  when语句中还可以使用Jinja2的大多“filter”,例如要忽略此前某语句的错误并基于其结果(failed或者sucess)运行后面指定的语句,可使用类似如下形式:
  tasks:
  - command: /bin/false
  register: result   注册器
  ignore_errors: True  忽略错误信息
  - command: /bin/something
  when: result|failed  第一条命令失败时(result 为failed时),才执行第二条命令
  - command: /bin/something_else
  when: result|success  (result 为success时),才执行
  - command: /bin/still/something_else
  when: result|skipped  skipped:已经执行过跳过执行
  此外,when语句中还可以使用facts或playbook中定义的变量。
  通过条件测试,如果httpd安装了,忽略错误,先把httpd停止后删除,这里特别需要注意yaml对格式要求表严格,一定要按规范写
  [root@node1 ~]# cat web.yaml
  - name: web server
  remote_user: root
  hosts: constrol
  tasks:
  - command: /bin/false
  register: result
  ignore_errors: True
  - name: reinstall httpd
  yum: name=httpd
  when: result|failed
  - name: stop httpd
  service: name=httpd enabled=no state=stopped
  - name: remove httpd
  yum: name=httpd state=absent
  [root@node1 ~]# ansible-playbook web.yaml
  PLAY [web server] *************************************************************
  GATHERING FACTS ***************************************************************
  ok: [192.168.21.230]
  ok: [192.168.21.234]
  TASK: [command /bin/false] ****************************************************
  failed: [192.168.21.230] => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.001693", "end": "2015-04-14 14:56:28.192788", "rc": 1, "start": "2015-04-14 14:56:28.191095", "warnings": []}
  ...ignoring
  failed: [192.168.21.234] => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.013089", "end": "2015-04-14 14:56:28.354546", "rc": 1, "start": "2015-04-14 14:56:28.341457", "warnings": []}
  ...ignoring
  TASK: [reinstall httpd] *******************************************************
  ok: [192.168.21.230]
  ok: [192.168.21.234]
  TASK: [stop httpd] ************************************************************
  changed: [192.168.21.230]
  changed: [192.168.21.234]
  TASK: [remove httpd] **********************************************************
  changed: [192.168.21.234]
  changed: [192.168.21.230]
  PLAY RECAP ********************************************************************
  192.168.21.230             : ok=5    changed=3    unreachable=0    failed=0
  192.168.21.234             : ok=5    changed=3    unreachable=0    failed=0
  [root@node1 ~]# ansible constrol -m command -a 'rpm -q httpd'  已经删除了
  192.168.21.230 | FAILED | rc=1 >>
  package httpd is not installed
  192.168.21.234 | FAILED | rc=1 >>
  package httpd is not installed
  3.4 迭代
  在迭代中只能使用item变量,变量引用为{{  }}两个大括号,变量两边有空格
  当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句来指明迭代的元素列表即可。例如:
  - name: add several users
  user: name=` item ` state=present groups=wheel  stae=present(用户得存在,加入wheel组中)
  with_items:
  - testuser1  分别使用testuser1替换name=`item`中的item项
  - testuser2
  上面语句的功能等同于下面的语句:
  - name: add user testuser1
  user: name=testuser1 state=present groups=wheel
  - name: add user testuser2
  user: name=testuser2 state=present groups=wheel
  事实上,with_items中可以使用元素还可为hashes,例如:
  - name: add several users
  user: name=` item`.`name ` state=present groups=` item`.`groups `
  with_items:
  - { name: 'testuser1', groups: 'wheel' }
  - { name: 'testuser2', groups: 'root' }
  ansible的循环机制还有更多的高级功能,具体请参见官方文档(http://docs.ansible.com/playbooks_loops.html)。
  如:使用迭代添加用户
  [root@node1 ~]# cat adduser.yaml
  - name: add users
  remote_user: root
  hosts: constrol
  tasks:
  - name: add  server users
  user: name=` item ` state=present
  with_items:
  - testuser1
  - testuser2
  - testuser3
  [root@node1 ~]# ansible-playbook adduser.yaml
  PLAY [add users] **************************************************************
  GATHERING FACTS ***************************************************************
  ok: [192.168.21.230]
  ok: [192.168.21.234]
  TASK: [add  server users] *****************************************************
  changed: [192.168.21.230] => (item=testuser1)
  changed: [192.168.21.234] => (item=testuser1)
  changed: [192.168.21.230] => (item=testuser2)
  changed: [192.168.21.230] => (item=testuser3)
  changed: [192.168.21.234] => (item=testuser2)
  changed: [192.168.21.234] => (item=testuser3)
  PLAY RECAP ********************************************************************
  192.168.21.230             : ok=2    changed=1    unreachable=0    failed=0
  192.168.21.234             : ok=2    changed=1    unreachable=0    failed=0
  [root@node1 ~]# ansible constrol -m command -a 'tail -5 /etc/passwd'  查看发现用户创建成功了
  192.168.21.234 | success | rc=0 >>
  openstack:x:500:500::/home/openstack:/bin/bash
  apache:x:48:48:Apache:/var/www:/sbin/nologin
  testuser1:x:501:501::/home/testuser1:/bin/bash
  testuser2:x:502:502::/home/testuser2:/bin/bash
  testuser3:x:503:503::/home/testuser3:/bin/bash
  192.168.21.230 | success | rc=0 >>
  openstack:x:500:500::/home/openstack:/bin/bash
  apache:x:48:48:Apache:/var/www:/sbin/nologin
  testuser1:x:501:501::/home/testuser1:/bin/bash
  testuser2:x:502:502::/home/testuser2:/bin/bash
  testuser3:x:503:503::/home/testuser3:/bin/bash
  [root@node1 ~]# ansible constrol -m shell -a 'pidof httpd|wc -l'   用shell模块时,可以使用管道
  192.168.21.230 | success | rc=0 >>
  1
  192.168.21.234 | success | rc=0 >>
  1
  [root@node1 ~]# ansible constrol -m command -a 'pidof httpd|wc -l'  使用command模块时,不能使用管道
  192.168.21.234 | FAILED | rc=1 >>
  pidof: invalid options on command line!
  192.168.21.230 | FAILED | rc=1 >>
  pidof: invalid options on command line!
  通过setup模块可以查看到很多,远程主机的信息,下面来看看
  [root@node1 ~]# ansible constrol -m setup
  。。。。
  "ansible_default_ipv4": {   eth1网卡的详细信息
  "address": "192.168.21.230",
  "alias": "eth1",
  "gateway": "192.168.20.254",
  "interface": "eth1",
  "macaddress": "00:50:56:3a:5a:49",
  "mtu": 1500,
  "netmask": "255.255.254.0",
  "network": "192.168.20.0",
  "type": "ether"
  },
  .......
  "ansible_distribution": "CentOS",     系统相关的信息
  "ansible_distribution_major_version": "6",
  "ansible_distribution_release": "Final",
  "ansible_distribution_version": "6.6",
  ........
  "ansible_kernel": "2.6.32-504.el6.x86_64",  内核
  "ansible_machine": "x86_64",   架构
  "ansible_os_family": "RedHat",   操作系统家族
  "ansible_pkg_mgr": "yum",    软件包管理
  。。。。
  安装一个软件包tree,要使用yum来安装,且只有远程主机包管理器是yum时,才使用yum来安装
  [root@node1 ~]# cat tree.yaml  测试不是使用yum,用apt-get
  - name: tree install
  remote_user: root
  hosts: constrol
  tasks:
  - name: yum install screen package
  yum: name=screen
  when: ansible_pkg_mgr == "apt-get"
  [root@node1 ~]# ansible-playbook tree.yaml
  PLAY [tree install] ***********************************************************
  GATHERING FACTS ***************************************************************
  ok: [192.168.21.234]
  ok: [192.168.21.230]
  TASK: [yum install screen package] ********************************************
  skipping: [192.168.21.234]
  skipping: [192.168.21.230]
  PLAY RECAP ********************************************************************
  192.168.21.230             : ok=1    changed=0    unreachable=0    failed=0
  192.168.21.234             : ok=1    changed=0    unreachable=0    failed=0
  [root@node1 ~]# ansible constrol -m command -a 'rpm -q screen'   screen没有安装
  192.168.21.234 | FAILED | rc=1 >>
  package screen is not installed
  192.168.21.230 | FAILED | rc=1 >>
  package screen is not installed
  [root@node1 ~]# cat tree.yaml  测试用yum
  - name: tree install
  remote_user: root
  hosts: constrol
  tasks:
  - name: yum install tree package
  yum: name=tree
  when: ansible_pkg_mgr == "yum"
  [root@node1 ~]# ansible-playbook tree.yaml
  PLAY [tree install] ***********************************************************
  GATHERING FACTS ***************************************************************
  ok: [192.168.21.230]
  ok: [192.168.21.234]
  TASK: [yum install tree package] **********************************************
  changed: [192.168.21.234]
  changed: [192.168.21.230]
  PLAY RECAP ********************************************************************
  192.168.21.230             : ok=2    changed=1    unreachable=0    failed=0
  192.168.21.234             : ok=2    changed=1    unreachable=0    failed=0
  [root@node1 ~]# ansible constrol -m command -a 'rpm -q tree'  发现安装成功了
  192.168.21.230 | success | rc=0 >>
  tree-1.5.3-2.el6.x86_64
  192.168.21.234 | success | rc=0 >>
  tree-1.5.3-2.el6.x86_64
  四、ansible playbooks
  playbook是由一个或多个“play”组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲,所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中,即可以让它们联同起来按事先编排的机制同唱一台大戏。下面是一个简单示例。
  - hosts: webnodes
  vars:  自定义变量
  http_port: 80
  max_clients: 256
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
  yum: name=httpd state=latest
  remote_user: username(tasks下以指定用户运行)
  - name: ensure apache is running
  service: name=httpd state=started
  handlers:  只有被调用时,才能执行
  - name: restart apache
  service: name=httpd state=restarted
  4.1 playbook基础组件
  4.1.1 Hosts和Users
  playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。hosts用于指定要执行指定任务的主机,其可以是一个或多个由冒号分隔主机组;remote_user则用于指定远程主机上的执行任务的用户。如上面示例中的
  -hosts: webnodes
  remote_user: root
  不过,remote_user也可用于各task中。也可以通过指定其通过sudo的方式在远程主机上执行任务,其可用于play全局或某任务;此外,甚至可以在sudo时使用sudo_user指定sudo时切换的用户。
  - hosts: webnodes
  remote_user: mageedu
  tasks:
  - name: test connection
  ping:
  remote_user: mageedu
  sudo: yes
  4.1.2 任务列表和action
  play的主体部分是task list。task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。在运行自上而下某playbook时,如果中途发生错误,所有已执行任务都将回滚,因此,在更正playbook后重新执行一次即可。
  task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。模块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致。
  每个task都应该有其name,用于playbook的执行结果输出,建议其内容尽可能清晰地描述任务执行步骤。如果未提供name,则action的结果将用于输出。
  定义task的可以使用“action: module options”或“module: options”的格式,推荐使用后者以实现向后兼容。如果action一行的内容过多,也可在行首使用几个空白字符进行换行。
  tasks:
  - name: make sure apache is running
  service: name=httpd state=running
  在众多模块中,只有command和shell模块仅需要给定一个列表而无需使用“key=value”格式,例如:
  tasks:
  - name: disable selinux
  command: /sbin/setenforce 0
  如果命令或脚本的退出码不为零,可以使用如下方式替代:
  tasks:
  - name: run this command and ignore the result
  shell: /usr/bin/somecommand || /bin/true
  或者使用ignore_errors来忽略错误信息:
  tasks:
  - name: run this command and ignore the result
  shell: /usr/bin/somecommand
  ignore_errors: True    前一个命令执行失败了,就忽略掉
  4.1.3 handlers
  用于当关注的资源发生变化时采取一定的操作。
  “notify”这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作。
  - name: template configuration file
  template: src=template.j2 dest=/etc/foo.conf 使用template.j2这个模板配置文件,保存为foo.conf配置文件,当foo.conf配置文件改变时,通知notify触发某个handlers
  notify:
  - restart memcached
  - restart apache
  handler是task列表,这些task与前述的task并没有本质上的不同。handler需要和tasks对齐
  handlers:
  - name: restart memcached
  service:  name=memcached state=restarted
  - name: restart apache
  service: name=apache state=restarted
  修改httpd.conf下监听的端口为8080,把此配置文件复制到远程节点上,
  写一个playbook,把文件复制到远程节点上后再启动服务
  [root@node1 conf]# cat httpd.yaml  yaml内容如下
  - hosts: constrol
  remote_user: root
  tasks:
  - name: Install httpd
  yum: name=httpd
  - name: copy configuration file
  copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
  notify: restart httpd
  - name: start httpd
  service: name=httpd enabled=yes state=started
  handlers:
  - name: restart httpd
  service: name=httpd state=restarted
  [root@node1 conf]# ansible-playbook httpd.yaml  执行
  PLAY [constrol] ***************************************************************
  GATHERING FACTS ***************************************************************
  ok: [192.168.21.230]
  ok: [192.168.21.234]
  TASK: [Install httpd] *********************************************************
  changed: [192.168.21.230]
  changed: [192.168.21.234]
  TASK: [copy configuration file] ***********************************************
  ok: [192.168.21.234]
  ok: [192.168.21.230]
  TASK: [start httpd] ***********************************************************
  changed: [192.168.21.234]
  changed: [192.168.21.230]
  PLAY RECAP ********************************************************************
  192.168.21.230             : ok=4    changed=2    unreachable=0    failed=0
  192.168.21.234             : ok=4    changed=2    unreachable=0    failed=0
  [root@node1 conf]# ansible constrol -m shell -a 'ss -tnlp|grep 80'  查看远程80端口是否启用了
  192.168.21.234 | success | rc=0 >>
  LISTEN     0      128                      :::80                      :::*      users:(("httpd",9374,5),("httpd",9377,5),("httpd",9378,5),("httpd",9379,5),("httpd",9380,5),("httpd",9381,5),("httpd",9382,5),("httpd",9383,5),("httpd",9384,5))
  192.168.21.230 | success | rc=0 >>
  LISTEN     0      128                      :::80                      :::*      users:(("httpd",26676,5),("httpd",26679,5),("httpd",26680,5),("httpd",26681,5),("httpd",26682,5),("httpd",26683,5),("httpd",26684,5),("httpd",26685,5),("httpd",26686,5))
  [root@node1 conf]# vim httpd.conf   修改监听端口为8080
  [root@node1 conf]# grep "8080" httpd.conf
  Listen 8080
  [root@node1 conf]# ansible constrol -m shell -a 'ss -tnlp|grep 8080'
  192.168.21.230 | FAILED | rc=1 >>
  192.168.21.234 | FAILED | rc=1 >>
  [root@node1 conf]# ansible-playbook httpd.yaml 重新执行
  PLAY [constrol] ***************************************************************
  GATHERING FACTS ***************************************************************
  ok: [192.168.21.230]
  ok: [192.168.21.234]
  TASK: [Install httpd] *********************************************************
  ok: [192.168.21.230]
  ok: [192.168.21.234]
  TASK: [copy configuration file] ***********************************************
  changed: [192.168.21.234]
  changed: [192.168.21.230]
  TASK: [start httpd] ***********************************************************
  ok: [192.168.21.234]
  ok: [192.168.21.230]
  NOTIFIED: [restart httpd] *****************************************************
  changed: [192.168.21.230]
  changed: [192.168.21.234]
  PLAY RECAP ********************************************************************
  192.168.21.230             : ok=5    changed=2    unreachable=0    failed=0
  192.168.21.234             : ok=5    changed=2    unreachable=0    failed=0
  [root@node1 conf]# ansible constrol -m shell -a 'ss -tnlp|grep 8080'  8080端口ok
  192.168.21.234 | success | rc=0 >>
  LISTEN     0      128                      :::8080                    :::*      users:(("httpd",9668,6),("httpd",9671,6),("httpd",9672,6),("httpd",9673,6),("httpd",9674,6),("httpd",9675,6),("httpd",9676,6),("httpd",9677,6),("httpd",9678,6))
  192.168.21.230 | success | rc=0 >>
  LISTEN     0      128                      :::8080                    :::*      users:(("httpd",26967,6),("httpd",26970,6),("httpd",26971,6),("httpd",26972,6),("httpd",26973,6),("httpd",26974,6),("httpd",26975,6),("httpd",26976,6),("httpd",26977,6))
  这里我们每次修改配置文件只要做的事就是重启一下服务,不用再按照httpd.yaml依次往下的执行,我们通过设定标签,为每个任务分别添加一个标签,重启对应的服务,只需要重启对应的标签即可。用tags来指定,再来测试如下
  [root@node1 ~]# man ansible-playbook
  ANSIBLE-PLAYBOOK(1)     System administration commands     ANSIBLE-PLAYBOOK(1)
  NAME
  ansible-playbook - run an ansible playbook
  SYNOPSIS
  ansible-playbook <filename.yml> ... [options]
  ..............
  -t, TAGS, --tags=TAGS
  Only run plays and tasks tagged with these values.
  --skip-tags=SKIP_TAGS
  Only run plays and tasks whose tags do not match these values.
  [root@node1 conf]# cat httpd.yaml
  - hosts: constrol
  remote_user: root
  tasks:
  - name: Install httpd
  yum: name=httpd
  - name: copy configuration file
  copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
  tags: conf
  notify: restart httpd
  - name: start httpd
  service: name=httpd enabled=yes state=started
  handlers:
  - name: restart httpd
  service: name=httpd state=restarted
  [root@node1 conf]# vim httpd.conf
  [root@node1 conf]# grep 80 httpd.conf
  #Listen 12.34.56.78:80
  Listen 80
  #ServerName www.example.com:80
  #NameVirtualHost *:80
  # (e.g. :80) if mod_ssl is being used, due to the nature of the
  #<VirtualHost *:80>
  [root@node1 conf]# ansible-playbook httpd.yaml -t conf
  PLAY [constrol] ***************************************************************
  GATHERING FACTS ***************************************************************
  ok: [192.168.21.234]
  ok: [192.168.21.230]
  TASK: [copy configuration file] ***********************************************   复制配置文件发生了changed
  changed: [192.168.21.234]
  changed: [192.168.21.230]
  NOTIFIED: [restart httpd] *****************************************************    触发restart了也为changed
  changed: [192.168.21.234]
  changed: [192.168.21.230]
  PLAY RECAP ********************************************************************
  192.168.21.230             : ok=3    changed=2    unreachable=0    failed=0
  192.168.21.234             : ok=3    changed=2    unreachable=0    failed=0
  [root@node1 conf]# ansible constrol -m shell -a 'ss -tnlp|grep 80'
  192.168.21.234 | success | rc=0 >>
  LISTEN     0      128                      :::80                      :::*      users:(("httpd",9873,6),("httpd",9875,6),("httpd",9876,6),("httpd",9877,6),("httpd",9878,6),("httpd",9879,6),("httpd",9881,6),("httpd",9882,6),("httpd",9883,6))
  192.168.21.230 | success | rc=0 >>
  LISTEN     0      128                      :::80                      :::*      users:(("httpd",27169,6),("httpd",27172,6),("httpd",27173,6),("httpd",27174,6),("httpd",27175,6),("httpd",27176,6),("httpd",27177,6),("httpd",27178,6),("httpd",27179,6))
  对httpd.yaml进行切分,通过- include来包含切分后的任务
  [root@node1 conf]# cat httpd.yaml
  - hosts: constrol
  remote_user: root
  tasks:
  - include: tasks/tasks.yaml  tasks文件的位置
  handlers:
  - include: handlers/handlers.yaml  handlers文件的位置
  [root@node1 conf]# mkdir tasks handlers  新建目录和文件
  [root@node1 conf]# vim handlers/handlers.yaml
  [root@node1 conf]# cat handlers/handlers.yaml
  - name: restart httpd
  service: name=httpd state=restarted
  [root@node1 conf]# vim tasks/tasks.yaml
  [root@node1 conf]# cat tasks/tasks.yaml
  - name: Install httpd
  yum: name=httpd
  - name: copy configuration file
  copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
  notify: restart httpd
  - name: start httpd
  service: name=httpd enabled=yes state=started
  [root@node1 conf]# vim httpd.conf
  [root@node1 conf]# grep "8080" httpd.conf   修改了一下端口
  Listen 8080
  [root@node1 conf]# ansible-playbook httpd.yaml  执行yaml
  ...
  [root@node1 conf]# ansible constrol -m shell -a 'ss -tnlp|grep 8080'  修改端口ok
  192.168.21.234 | success | rc=0 >>
  LISTEN     0      128                      :::8080                    :::*      users:(("httpd",10379,6),("httpd",10382,6),("httpd",10383,6),("httpd",10384,6),("httpd",10385,6),("httpd",10386,6),("httpd",10387,6),("httpd",10388,6),("httpd",10389,6))
  192.168.21.230 | success | rc=0 >>
  LISTEN     0      128                      :::8080                    :::*      users:(("httpd",27646,6),("httpd",27649,6),("httpd",27650,6),("httpd",27651,6),("httpd",27652,6),("httpd",27653,6),("httpd",27654,6),("httpd",27655,6),("httpd",27656,6))
  在httpd.yaml下也可以使用tags,可以自行测试,后面再介绍roles,且以一个实际项目来进行测试,欢迎关注

运维网声明 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-543554-1-1.html 上篇帖子: 运维自动化之ansible playbook一键化安装redis主从 下篇帖子: ansible中角色和模板使用及部署lamp平台
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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