zhu894532094 发表于 2018-7-29 07:34:16

简单使用ansible-playbook

  笔记内容:简单使用ansible-playbook
  笔记日期:2018-01-30


[*]24.21 ansible安装包和管理服务
[*]24.22 使用ansible playbook
[*]24.23 playbook里的变量
[*]24.24 playbook里的循环
[*]24.25 playbook里的条件判断
[*]24.26 playbook中的handlers
24.21 ansible安装包和管理服务
  1.使用以下命令给客户端安装httpd服务:
  

# ansible testhost -m yum -a "name=httpd"  
192.168.77.128 | SUCCESS => {
  "changed": true,
  "msg": "",
  "rc": 0,
  "results": [

  "Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.cn99.com\n * epel: mirrors.tongji.edu.cn\n * extras: mirrors.aliyun.com\n * updates: mirrors.163.com\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-67.el7.centos.6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package      Arch          Version                        Repository   >  ]
  
}
  

  

  2.执行以下命令启动httpd服务:
  

# ansible testhost -m service -a "name=httpd state=started enabled=yes"  ## 然后会输出一堆状态信息,只要第一句为SUCCESS则代表启动成功
  

  注:这里的name是centos系统里的服务名,可以通过chkconfig --list查看到。
  其他控制服务的命令:
  

# 停止服务  
# ansible testhost -m service -a "name=httpd state=stopped"
  
# 重新启动服务
  
# ansible testhost -m service -a "name=httpd state=restarted"
  
# 重载服务
  
# ansible testhost -m service -a "name=httpd state=reloaded"
  

  3.在name后面还可以加上state=installed或removed,加上removed的话,表示卸载这个服务,如果不指定state的值默认是installed:
  

# ansible testhost -m yum -a "name=httpd state=removed"  
192.168.77.128 | SUCCESS => {
  "changed": true,
  "msg": "",
  "rc": 0,
  "results": [
  "Loaded plugins: fastestmirror, langpacks\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-67.el7.centos.6 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package      Arch          Version                     Repository      >  ]
  
}
  
#
  

  然后到客户端上通过rpm -qa httpd命令查看是否已卸载成功:
  

# rpm -qa httpd  
#
  

  Ansible文档的使用:
  1.列出所有可用的模块命令:

  ansible-doc -l

  2.查看指定模块的文档,例如我要查看cron模块的文档,可使用以下命令:

  ansible-doc cron

  ansible-doc后面跟模块名就可以查看该模块的文档。

24.22 使用ansible playbook
  playbook相当于可以把模块命令都写入到配置文件里面,这样就可以直接执行配置文件了,有点脚本的意思:
  

# vim /etc/ansible/test.yml  
---
  
- hosts: testhost
  remote_user: root
  tasks:
  - name: test_playbook
  shell: touch /tmp/test.txt
  

  文件格式说明:


[*]第一行需要有三个杠,hosts参数指定了对哪些主机进行参作,如果是多台机器可以用逗号作为分隔,也可以使用主机组,在/etc/ansible/hosts里定义;
[*]user参数指定了使用什么用户登录远程主机操作;
[*]tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来,shell是ansible模块名字
  编辑完成之后,使用ansible-playbook命令执行该文件:
  

# ansible-playbook /etc/ansible/test.yml  

  
PLAY ***********************************************************************************************************
  

  
TASK ****************************************************************************************************
  
ok:
  

  
TASK ******************************************************************************************************
  : Consider using file module with state=touch rather than running touch
  

  
changed:
  

  
PLAY RECAP ****************************************************************************************************************
  
192.168.77.128             : ok=2    changed=1    unreachable=0    failed=0
  

  
#
  

  然后到客户端上看看是否有创建test.txt文件:
  

# ls -l /tmp/test.txt  
-rw-r--r-- 1 root root 0 1月30 11:58 /tmp/test.txt
  
#
  

  如上,代表执行成功。

24.23 playbook里的变量
  我们通过一个创建用户的例子,来演示一下playbook里的变量使用方式:
  

# vim /etc/ansible/create_user.yml# 编辑内容如下  
---
  
- name: create_user
  hosts: testhost
  user: root
  gather_facts: false
  vars:
  - user: "test"
  tasks:
  - name: create user
  user: name="{{ user }}"
  

  说明:


[*]name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;
[*]gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,如果需要在后面的tasks里获取setup收集到的信息,就需要把这个参数设置为True;
[*]vars参数,指定了变量,这里声明了一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;
[*]user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。
  执行该文件:
  

# ansible-playbook /etc/ansible/create_user.yml  

  
PLAY ********************************************************************************************************
  

  
TASK ********************************************************************************************************
  
changed:
  

  
PLAY RECAP ****************************************************************************************************************
  
192.168.77.128             : ok=1    changed=1    unreachable=0    failed=0
  

  
#
  

  到客户端上看看用户是否已创建:
  

#>
uid=1003(test) gid=1003(test) 组=1003(test)
  
#
  

24.24 playbook里的循环
  playbook除了有变量,还有循环语句,以下通过一个简单的例子来演示一下循环的使用方式:
  

# vim /etc/ansible/while.yml  
---
  
- hosts: testhost
  user: root
  tasks:
  - name: change mode for files
  file: path=/tmp/{{ item }} state=touch mode=600
  with_items:
  - 1.txt
  - 2.txt
  - 3.txt
  

  说明:


[*]file模块可以对文件进行相关的操作,例如创建文件或者更改文件权限等,具体可以查看该模块的文档
[*]with_items为循环的对象,相当于是一个数组或集合,写在下面的1.txt、2.txt以及3.txt是该集合的元素。而item则表示的是遍历出来的元素,也就是说item指代的是1.txt、2.txt以及3.txt。
[*]state的值设置为touch表示如果该文件不存在就进行创建
[*]path表示文件的路径
[*]mode设置权限
  执行该文件:
  

# ansible-playbook /etc/ansible/while.yml  

  
PLAY ***********************************************************************************************************
  

  
TASK ****************************************************************************************************
  
ok:
  

  
TASK **********************************************************************************************
  
changed: => (item=1.txt)
  
changed: => (item=2.txt)
  
changed: => (item=3.txt)
  

  
PLAY RECAP ****************************************************************************************************************
  
192.168.77.128             : ok=2    changed=1    unreachable=0    failed=0
  

  
#
  

  到客户端上看看文件是否已创建:
  

# ll /tmp/*.txt  
-rw------- 1 root root0 1月30 15:54 /tmp/1.txt
  
-rw------- 1 root root0 1月30 15:54 /tmp/2.txt
  
-rw------- 1 root root0 1月30 15:54 /tmp/3.txt
  
#
  

24.25 playbook里的条件判断
  我们都知道在脚本中循环和条件判断是必不可少的语句,所以在playbook里这两种语句也是有的,循环我们已经介绍完了,接下来我们通过一个简单的创建文件的例子演示一下条件判断语句的使用方式。
  我们一般以setup模块收集到的主机信息,来作为判断条件。所以在编写代码之前,我们需要先获取相应的信息,例如我要以ip地址来作为判断条件,那么我就得先从setup里获取主机ip的相关信息。
  执行以下命令可以查看到setup收集到的所有的facter信息,输出的信息是JSON格式的:

  ansible testhost -m setup

  编写文件内容如下:
  

# vim /etc/ansible/when.yml  
---
  
- hosts: testhost
  user: root
  gather_facts: True
  tasks:
  - name: use when
  shell: touch /tmp/when.txt
  when: ansible_eno16777736.ipv4.address == "192.168.77.128"
  

  说明:


[*]ansible_eno16777736是一个数组存储着网卡相关信息,ipv4属于该数组的子元素,但是ipv4也是一个数组,而address则是ipv4数组的子元素。我们需要使用address 来作为判断条件。
[*]所以要访问address就需要使用这样的格式:ansible_eno16777736.ipv4.address,address表示的是键,而"192.168.77.128"则是值,when为判断语句相当于if,所以其判断条件为:该键的值为"192.168.77.128"时就执行shell模块里定义的语句。
  执行该文件:
  

# ansible-playbook /etc/ansible/when.yml  

  
PLAY ***********************************************************************************************************
  

  
TASK ****************************************************************************************************
  
ok:
  

  
TASK ***********************************************************************************************************
  : Consider using file module with state=touch rather than running touch
  

  
changed:
  

  
PLAY RECAP ****************************************************************************************************************
  
192.168.77.128             : ok=2    changed=1    unreachable=0    failed=0
  

  
#
  

  到客户端上看看文件是否已创建:
  

# ll /tmp/when.txt  
-rw-r--r-- 1 root root 0 1月30 16:33 /tmp/when.txt
  
#
  

24.26 playbook中的handlers
  有一种情况就是执行了tasks里面的内容之后,服务器发生了变化,这时我们可能需要执行一些相关的操作。例如我们修改了某个服务的配置文件后,则需要重启一下服务。而handlers就是完成这样的事情的,它相当于编程中的回调函数,当tasks里的内容执行成功后,就会执行handlers里定义的内容。也类似于shell脚本中的&&符号,例如 cat 1.txt && rm -f 1.txt ,当cat 1.txt命令执行成功之后就会执行rm -f 1.txt命令,否则不执行。
  下面用一个简单的例子来演示一下handlers的使用方式:
  

# vim /etc/ansible/handlers.yml  
---
  
- name: handlers test
  hosts: testhost
  user: root
  tasks:
  - name: copy file
  copy: src=/etc/passwd dest=/tmp/test_passwd.txt
  notify: test handlers
  handlers:
  - name: test handlers
  shell: echo "This is a test string" >> /tmp/test_passwd.txt
  

  说明:


[*]只有copy模块执行成功后,才会去调用下面的handlers里定义的内容。也就是说如果/etc/passwd和/tmp/test_passwd.txt内容是一样的话,就不会去执行handlers里面的shell相关命令,因为copy没有被执行。 这种比较适合配置文件发生更改后,重启服务的操作。
[*]notify用于指定handlers的name参数的值,因为handlers可以定义多个,所以需要使用notify来进行指定调用哪一个。
  执行该文件:
  

# ansible-playbook /etc/ansible/handlers.yml  

  
PLAY ******************************************************************************************************
  

  
TASK ****************************************************************************************************
  
ok:
  

  
TASK **********************************************************************************************************
  
changed:
  

  
RUNNING HANDLER *******************************************************************************************
  
changed:
  

  
PLAY RECAP ****************************************************************************************************************
  
192.168.77.128             : ok=3    changed=2    unreachable=0    failed=0
  

  
#
  

  到客户端上看看文件末尾的那一行是否是我们echo进去的那一行内容:
  

# tail -n1 /tmp/test_passwd.txt  
This is a test string
  
#
页: [1]
查看完整版本: 简单使用ansible-playbook