简单使用ansible-playbook

简介:

1.使用以下命令给客户端安装httpd服务:

[root@server ~]# 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      Size\n================================================================================\nInstalling:\n httpd        x86_64        2.4.6-67.el7.centos.6          updates        2.7 M\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 2.7 M\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : httpd-2.4.6-67.el7.centos.6.x86_64                           1/1 \n  Verifying  : httpd-2.4.6-67.el7.centos.6.x86_64                           1/1 \n\nInstalled:\n  httpd.x86_64 0:2.4.6-67.el7.centos.6                                          \n\nComplete!\n"
    ]
}

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

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

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

其他控制服务的命令:

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

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

[root@server ~]# 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       Size\n================================================================================\nRemoving:\n httpd        x86_64        2.4.6-67.el7.centos.6         @updates        9.4 M\n\nTransaction Summary\n================================================================================\nRemove  1 Package\n\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Erasing    : httpd-2.4.6-67.el7.centos.6.x86_64                           1/1 \n  Verifying  : httpd-2.4.6-67.el7.centos.6.x86_64                           1/1 \n\nRemoved:\n  httpd.x86_64 0:2.4.6-67.el7.centos.6                                          \n\nComplete!\n"
    ]
}
[root@server ~]# 

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

[root@client ~]# rpm -qa httpd
[root@client ~]# 

Ansible文档的使用:

1.列出所有可用的模块命令:

ansible-doc -l

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

ansible-doc cron

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


24.22 使用ansible playbook

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

[root@server ~]# 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命令执行该文件:

[root@server ~]# ansible-playbook /etc/ansible/test.yml

PLAY [testhost] ***********************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.77.128]

TASK [test_playbook] ******************************************************************************************************
 [WARNING]: Consider using file module with state=touch rather than running touch

changed: [192.168.77.128]

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

[root@server ~]# 

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

[root@client ~]# ls -l /tmp/test.txt 
-rw-r--r-- 1 root root 0 1月  30 11:58 /tmp/test.txt
[root@client ~]# 

如上,代表执行成功。


24.23 playbook里的变量

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

[root@server ~]# 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变量的值。

执行该文件:

[root@server ~]# ansible-playbook /etc/ansible/create_user.yml

PLAY [create_user] ********************************************************************************************************

TASK [create user] ********************************************************************************************************
changed: [192.168.77.128]

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

[root@server ~]# 

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

[root@client ~]# id test
uid=1003(test) gid=1003(test) 组=1003(test)
[root@client ~]# 

24.24 playbook里的循环

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

[root@server ~]# 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设置权限

执行该文件:

[root@server ~]# ansible-playbook /etc/ansible/while.yml 

PLAY [testhost] ***********************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.77.128]

TASK [change mode for files] **********************************************************************************************
changed: [192.168.77.128] => (item=1.txt)
changed: [192.168.77.128] => (item=2.txt)
changed: [192.168.77.128] => (item=3.txt)

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

[root@server ~]# 

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

[root@client ~]# ll /tmp/*.txt
-rw------- 1 root root  0 1月  30 15:54 /tmp/1.txt
-rw------- 1 root root  0 1月  30 15:54 /tmp/2.txt
-rw------- 1 root root  0 1月  30 15:54 /tmp/3.txt
[root@client ~]# 

24.25 playbook里的条件判断

我们都知道在脚本中循环和条件判断是必不可少的语句,所以在playbook里这两种语句也是有的,循环我们已经介绍完了,接下来我们通过一个简单的创建文件的例子演示一下条件判断语句的使用方式。

我们一般以setup模块收集到的主机信息,来作为判断条件。所以在编写代码之前,我们需要先获取相应的信息,例如我要以ip地址来作为判断条件,那么我就得先从setup里获取主机ip的相关信息。

执行以下命令可以查看到setup收集到的所有的facter信息,输出的信息是JSON格式的:

ansible testhost -m setup

编写文件内容如下:

[root@server ~]# 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模块里定义的语句。

执行该文件:

[root@server ~]# ansible-playbook /etc/ansible/when.yml 

PLAY [testhost] ***********************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.77.128]

TASK [use when] ***********************************************************************************************************
 [WARNING]: Consider using file module with state=touch rather than running touch

changed: [192.168.77.128]

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

[root@server ~]# 

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

[root@client ~]# ll /tmp/when.txt
-rw-r--r-- 1 root root 0 1月  30 16:33 /tmp/when.txt
[root@client ~]#

24.26 playbook中的handlers

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

下面用一个简单的例子来演示一下handlers的使用方式:

[root@server ~]# 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来进行指定调用哪一个。

执行该文件:

[root@server ~]# ansible-playbook /etc/ansible/handlers.yml 

PLAY [handlers test] ******************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.77.128]

TASK [copy file] **********************************************************************************************************
changed: [192.168.77.128]

RUNNING HANDLER [test handlers] *******************************************************************************************
changed: [192.168.77.128]

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

[root@server ~]# 

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

[root@client ~]# tail -n1 /tmp/test_passwd.txt 
This is a test string

[root@client ~]#




本文转自 ZeroOne01 51CTO博客,原文链接:http://blog.51cto.com/zero01/2066913,如需转载请自行联系原作者

相关文章
|
XML JSON 运维
Ansible-playbook 的使用 | 学习笔记
快速学习Ansible-playbook 的使用
Ansible-playbook 的使用 | 学习笔记
|
9月前
|
应用服务中间件 Linux 数据安全/隐私保护
ansible:playbook学习笔记
ansible:playbook学习笔记
98 0
|
10月前
|
Shell 网络安全 开发工具
|
缓存 运维 负载均衡
Ansible-playbook 的编写 | 学习笔记
快速学习Ansible-playbook 的编写
Ansible-playbook 的编写 | 学习笔记
|
Kubernetes Shell Linux
|
MySQL 关系型数据库 Shell
Ansible第二篇:ansible-playbook
一、Playbook语法 Ansible-playbook采用YAML语法编写。 示例:[root@LOCALHOST ~]# cat yaml/httpd.
1519 0
|
Python
Ansible 简单使用
安装rpm -Uvh https://mirrors.zju.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpmyum install -y ansible 配置hostsvi /etc/ansible/hosts aly ansible_ssh_port=10011 ansible_ssh_host=127.
1022 0