五、ansible管理任务计划、ansible安装包和管理服务、使用ansible的playbook

一、ansible管理任务计划

# ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt'  weekday=6"

name指定任务计划的名字,job指定它的命令是什么,后面指定它的分时日月周,不定义就是*。

[root@yw02 ~]# crontab -l
#Ansible: test cron
* * * * 6 /bin/touch /tmp/1212.txt

第一行一定不能更改,一旦改了之后,就不能用这个工具对它去做操作了。


若要删除该cron 只需要加一个字段 state=absent

# ansible testhost -m cron -a "name='test cron' state=absent"


其他的时间表示:分钟 minute ,小时 hour ,日期 day ,月份 month 。


二、ansible安装rpm包和管理服务

# ansible testhost -m yum -a "name=httpd"

在name后面还可以加上state=installed/removed  安装/卸载

启动httpd的服务,用到的模块教service:

# ansible testhost -m service -a "name=httpd state=started enabled=yes"

启动服务后,就可以到机器上ps aux看到了。

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

Ansible文档的使用

ansible-doc -l   列出所有的模块

ansible-doc cron  查看指定模块的文档,相当于系统里面的man配置。


三、使用ansible的playbook

playbook说白了就是把所有的配置搞到一个配置文件里去,然后执行这个配置文件就行了。

相当于把模块写入到配置文件里面,例:

# vi  /etc/ansible/test.yml //加入如下内容
---
- hosts: yw02
  remote_user: root
  tasks:
    - name: test_playbook
      shell: touch /tmp/lishiming.txt

说明: 文件格式,后缀名是yml;第一行需要有三个杠,是固定格式;hosts参数指定了对哪些主机进行操作,如果是多台机器可以用逗号作为分隔,也可以使用主机组,在/etc/ansible/hosts里定义;

user参数指定了使用什么用户登录远程主机操作;

tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来,shell是ansible模块名字。

执行:ansible-playbook test.yml

PLAY [yw02] **************************************************************************************************************************

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

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: [yw02]

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


四、playbook里的变量

再来一个创建用户的例子:

# vi /etc/ansible/create_user.yml //加入如下内容
---
- name: create_user
  hosts: yw02
  user: root
  gather_facts: false
  vars:
    - user: "test"
  tasks:
    - name: create user
      user: name="{{ user }}"

说明:

name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印name变量的值 ,可以省略;

gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到,如果不写这行就会默认收集facts,如果机器过多,建议就不收集了,影响执行效率,也会给ansible压力。

vars参数,指定了变量,这里指定一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;

tasks下的user指定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值,{{user}}=test。

[root@fuxi01 ansible]# ansible-playbook create_user.yml 

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

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

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

如果这个test用户已经存在了,就不会创建了,changed=0。

猜你喜欢

转载自blog.51cto.com/13576245/2456041