美文网首页
mode,playbook

mode,playbook

作者: 快去学习不然怎么去看aimer | 来源:发表于2019-10-13 16:55 被阅读0次

mode:

cron,yum,service,user,group,script,setup

cron

day= # 日应该运行的工作( 1-31, , /2, )
hour= # 小时 ( 0-23, , /2, )
minute= # 分钟( 0-59, , /2, )
month= # 月( 1-12, *, /2, )
weekday= # 周 ( 0-6 for Sunday-Saturday,, )
job= # 指明运行的命令是什么
name= # 定时任务描述
special_time # 特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
state # 指定状态,present 表示添加定时任务,也是默认设置,absent 表示删除定时任务
user # 以哪个用户的身份执行

1 * * * * /usr/bin/date > /root/a.txt
ansible web -m cron -a 'name="echo every 1 min" minute=*/1 job="/usr/bin/date > /root/a.txt" '

crontab -r 
ansible web -m cron -a 'name="echo every 1 min" minute=*/1 job="/usr/bin/date > /root/a.txt" state=absent '

yum

name=   # 所安装的包的名称
state=  #present--->安装, latest--->安装最新的, absent---> 卸载软件。
update_cache  # 强制更新yum的缓存
conf_file  # 指定远程yum安装时所依赖的配置文件(安装本地已有的包)。
disable_gpg_check  # 是否禁止GPG checking,只用于 presentor latest。
disablerepo  # 临时禁止使用 yum 库。 只用于安装或更新时。
enablerepo   # 临时使用的 yum 库。只用于安装或更新时。

yum -y install vim
ansible web -m yum -a 'name=vim state=present'

service

arguments # 命令行提供额外的参数
enabled # 设置开机启动。
name= # 服务名称
runlevel # 开机启动的级别,一般不用指定。
sleep # 在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)
state # 有四种状态,分别为:started--->启动服务, stopped--->停止服务, restarted--->重启服务, reloaded--->重载配置

在docker启动时需要启动/usr/sbin/init来启动systemctel
systemctl start nginx    systenctl enable nginx 
ansible web -m service -a 'name=nginx state=started enabled=true' 

user

comment   # 用户的描述信息
createhome  # 是否创建家目录
force  # 在使用 state=absent 时, 行为与 userdel –force 一致.
group  # 指定基本组
groups  # 指定附加组,如果指定为(groups=)表示删除所有组
home   # 指定用户家目录
move_home  # 如果设置为home=时, 试图将用户主目录移动到指定的目录
name   # 指定用户名
non_unique  # 该选项允许改变非唯一的用户ID值
password  # 指定用户密码
remove  # 在使用 state=absent 时, 行为是与 userdel –remove 一致
shell  # 指定默认shell
state  # 设置帐号状态,不指定为创建,指定值为 absent 表示删除
system  # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户
uid   # 指定用户的uid

useradd -u 11111 -s /usr/sbin/nologin keer
ansible web -m user -a 'name=keer uid=11111 shell=/usr/sbin/nologin'

group

gid=  # 设置组的GID号
name=  # 指定组的名称
state=  # 指定组的状态,默认为创建,设置值为absent为删除
system=  # 设置值为yes,表示创建为系统组

ansible web -m group -a 'name=sanguo gid=12222'

script

在/root/test.sh中
ping -c 1 -w 1 192.168.1.2 > /root/a.txt

ansible web -w script -a '/root/test.sh'

setup

ansible web -m setup -a 'filter="*mem*" ' --tree /root/facts
取到子节点的有关于mem的信息

ansible 192.168.0.2 -m setup :
{
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "172.17.0.3"
        ], 
格式为字典,得到子节点的信息

--tree 就相当于 > 

playbook

使用yaml语言,与python一样,强制缩进
1、文件的第一行应该以 "---" (三个连字符)开始,表明YMAL文件的开始。
2、在同一行中,#之后的内容表示注释,类似于shell,python和ruby。
3、YMAL中的列表元素以”-”开头然后紧跟着一个空格,后面为元素内容。
4、同一个列表中的元素应该保持相同的缩进。否则会被当做错误处理。
5、play 中 hosts,variables,roles,tasks等对象的表示方法都是键值中间以":"分隔表示,":"后面还要增加一个空格。

playbook包含的元素
Hosts:主机组;
Tasks:任务列表;
Variables:变量,设置方式有四种;
Templates:包含了模板语法的文本文件;
Handlers:由特定条件触发的任务;

nginx.yaml
---
- hosts: web          
  remote_user: root     #远程的用户
  tasks: 
      - name: copy grabber.py   #解决的docker无法yum nginx
        copy: src=/root/grabber.py dest=/usr/lib/python2.7/site-packages/urlgrabber/
      - name: copy nginx.repo
        cpoy: src=/root/nginx.repo dest=/etc/yum.repos.d/
      - name: yum nginx
        yum: name=nginx state=present
      - name: copy nginx.conf
        copy: src=/root/nginx.conf dest=/etc/nginx/conf.d/
        notify: reload
        tag: reload nginx
      - name: start nginx
        shell: /usr/sbin/nginx
        tags: start nginx
  handlers:
      - name: reload
        shell: /usr/sbin/nginx -s reload

ansible-playbook nginx.yaml -C  检验剧本是否有语法错误
ansible-playbook nginx.yaml  直接执行

当只想执行某一模块时,就使用-t模块
ansible-playbook nginx.yaml -t start nginx
就会从start nginx模块开始往下执行

 由于的container的原因,无法使用service模块,否则启动,重启时使用service模块
reload: service: name=nginx state=restarted
start:    service: name=nginx state=started

使用%s/nginx/{{ rpmname }}/g 将playbook中的包名替换,用var在上面定义变量,这样可以方便脚本的使用

- name: copy grabber.py
  copy: src=grabber.py dest=/usr/lib/python2.7/site-packages/urlgrabber/grabber.py
- name: copy {{ rpmname }}.repo
  copy: src={{ rpmname }}.repo dest=/etc/yum.repos.d/
- name: install {{ rpmname }}
  yum: name={{ rpmname }} state=present
- name: copy {{ rpmname }}.conf
  copy: src=/root/{{ rpmname }}.conf dest=/etc/{{ rpmname }}/conf.d/
  notify: reload
  tag: reload {{ rpmname }}
- name: start {{ rpmname }} service
  shell: /usr/sbin/{{ rpmname }}
  tags: start{{ rpmname }}

相关文章

网友评论

      本文标题:mode,playbook

      本文链接:https://www.haomeiwen.com/subject/dznxmctx.html