【1.2】自动化运维 ansible 2

本文介绍了如何使用Ansible进行自动化运维,包括管理任务计划、安装和管理服务、编写及执行playbook、使用变量以及实现playbook中的循环。通过示例展示了如何使用cron模块设置crontab任务,使用yum模块安装和卸载软件,以及service模块管理服务。同时,详细解释了playbook的结构和变量的使用,以及如何进行循环操作。
摘要由CSDN通过智能技术生成

24.20 ansible管理任务计划

ansible 组名/ip/机器名 -m cron -a "name=’ ’ job=’ ’ weekday= " 远程管理任务计划

[root@arslinux-01 ~]# ansible 192.168.194.132 -m cron -a "name='test cron' job='/bin/touch /tmp/1234546.txt' weekday=6"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron"
    ]
}
[root@arslinux-02 ~]# crontab -l
#Ansible: test cron
* * * * 6 /bin/touch /tmp/1234546.txt

说明: cron 是模块;name 自定义 crontab任务的名称;job 指的是任务;weekday 指每周几
其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month

  • ansible 组名/ip/机器名 -m cron -a “name=’ ’ state=absent” 删除 cron
[root@arslinux-01 ~]# ansible testhost -m cron -a "name='test cron' state=absent"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}
[root@arslinux-02 ~]# crontab -l
[root@arslinux-02 ~]#

24.21 ansible安装包和管理服务

  • ansible 组名/ip/机器名 -m yum -a “name=包名” 远程 yum 安装包
  • ansible 组名/ip/机器名 -m yum -a “name=包名 state=removed” 远程卸载
[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd"
[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd state=removed"

重新安装、启动并设置开机启动

[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd state=remove state=installed"
  • ansible 组名/ip/机器名 -m service -a "name= state= enabled= " 启动服务并设开机启动
[root@arslinux-01 ~]# ansible 192.168.194.132 -m service -a "name=httpd state=started enabled=no"
[root@arslinux-02 ~]# ps aux|grep httpd
root      11746  0.3  0.5 224052  5000 ?        Ss   23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11747  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11749  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11750  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11751  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11752  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
root      11769  0.0  0.0 112724   988 pts/1    R+   23:07   0:00 grep --color=auto httpd
[root@arslinux-02 ~]# date
2020年 01月 02日 星期四 23:07:43 CST

说明:name 是服务名称;state 是操作、状态;enabled 指是否开机启动

  • Ansible文档的使用
    ansible-doc -l 列出所有的模块
    ansible-doc cron 查看指定模块的文档

24.22 使用ansible playbook

Ansible playbook 相当于把模块写入到配置文件里面
1、编辑 test.yml 文件

[root@arslinux-01 ansible]# vim test.yml
---
- hosts: 192.168.194.132
  remote_user: root
  tasks:
    - name: test_playbook
      shell: touch /tmp/arsenal4life.txt

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

2、用 ansible-playbook 执行

[root@arslinux-01 ansible]# ansible-playbook test.yml 

PLAY [192.168.194.132] *********************************************************************************************************

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

TASK [test_playbook] ***********************************************************************************************************
 [WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command because
file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid
of this message.

changed: [192.168.194.132]

PLAY RECAP *********************************************************************************************************************
192.168.194.132            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[root@arslinux-01 ansible]#

3、查看

[root@arslinux-02 ~]# ll /tmp/arsenal4life.txt 
-rw-r--r-- 1 root root 0 1月   2 20:57 /tmp/arsenal4life.txt

24.23 playbook里的变量

创建用户的 playbook:
1、编辑 create_user.yml

[root@arslinux-01 ansible]# vim create_user.yml
---
- name: create_user
  hosts: 192.168.194.132
  user: root
  gather_facts: false
  vars:
    - user: "test"
  tasks:
    - name: create user
      user: name="{{ user }}"

说明:name 参数对该 playbook 实现的功能做一个概述,后面执行过程中,会打印 name 变量的值 ,可以省略;gather_facts 参数指定了在以下任务部分执行前,是否先执行 setup 模块获取主机相关信息,这在后面的 task 会使用到 setup 获取的信息时用到;vars 参数,指定了变量,这里指定一个 user 变量,其值为 test ,需要注意的是,变量值一定要用引号引住;user 提定了调用 user 模块,name 是 user 模块里的一个参数,而增加的用户名字调用了上面 user 变量的值;{{ }} 定义变量

2、执行

[root@arslinux-01 ansible]# ansible-playbook create_user.yml 

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

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

PLAY RECAP *********************************************************************************************************************
192.168.194.132            : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

3、查看

[root@arslinux-02 ~]# id test
uid=1001(test) gid=1001(test)=1001(test)

4、如果用户已经存在,那么不会变更

[root@arslinux-01 ansible]# ansible-playbook create_user.yml 

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

TASK [create user] *************************************************************************************************************
ok: [192.168.194.132]

PLAY RECAP *********************************************************************************************************************
192.168.194.132            : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

24.24 playbook里的循环

1、编辑 while.yml

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

说明: with_items 为循环的对象,防止文件不存在,增加 state=touch 创建文件

2、执行

[root@arslinux-01 ansible]# ansible-playbook while.yml 

PLAY [192.168.194.132] *********************************************************************************************************

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

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

PLAY RECAP *********************************************************************************************************************
192.168.194.132            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

3、查看

[root@arslinux-02 ~]# ll /tmp/*.txt
-rw------- 1 root root  0 1月   2 21:45 /tmp/1.txt
-rw------- 1 root root  0 1月   2 21:45 /tmp/2.txt
-rw------- 1 root root  0 1月   2 21:45 /tmp/3.txt
-rw-r--r-- 1 root root 43 1月   2 22:28 /tmp/ansible_test.txt
-rw-r--r-- 1 root root  0 1月   2 21:08 /tmp/arsenal4lifeaaa.txt
-rw-r--r-- 1 root root  0 1月   2 20:57 /tmp/arsenal4life.txt
[root@arslinux-02 ~]# date
2020年 08月 02日 星期四 21:46:07 CST
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值