码迷,mamicode.com
首页 > 系统相关 > 详细

Ansible Linux commonly used moudle example

时间:2021-01-19 12:09:17      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:web   opp   数据   creat   back   code   htm   conf   service   

Ansible 常用模块

参考地址:

https://docs.ansible.com/ansible/latest/modules/modules_by_category.html

ping
command and shell
# 注意区别和相同
# command 不能识别变量以及引号内容
root@unix (2)[f:10]$ command echo $HOSTNAME
172.16.37.2 | CHANGED | rc=0 >>
$HOSTNAME
172.16.37.4 | CHANGED | rc=0 >>
$HOSTNAME
root@unix (2)[f:10]$ shell echo $HOSTNAME
172.16.37.2 | CHANGED | rc=0 >>
ansible-master
172.16.37.4 | CHANGED | rc=0 >>
ansible-node1
root@node1 (1)[f:10]$ shell echo passwd |passwd --stdin test
172.16.37.4 | CHANGED | rc=0 >>
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。
root@node1 (1)[f:10]$ command echo passwd |passwd --stdin test
172.16.37.4 | CHANGED | rc=0 >>
passwd |passwd --stdin test
script
copy and fetch
hostname
file
unarchive and archive
ansible all -m unarchive -a ‘src=/tmp/foo.zip dest=/data copy=no mode=0777‘
ansible all -m unarchive -a ‘src=https://example.com/example.zip dest=/data copy=no‘
ansible websrvs -m archive  -a ‘path=/var/log/ dest=/data/log.tar.bz2 format=bz2  owner=wang mode=0600‘
yum
cron
#备份数据库脚本
[root@centos8 ~]#cat mysql_backup.sh 
mysqldump -A -F --single-transaction --master-data=2 -q -uroot |gzip > /data/mysql_date +%F_%T.sql.gz
#创建任务
ansible 10.0.0.8 -m cron -a ‘hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh‘
ansible websrvs   -m cron -a "minute=*/5 job=‘/usr/sbin/ntpdate 172.20.0.1 &>/dev/null‘ name=Synctime"
#禁用计划任务
ansible websrvs   -m cron -a "minute=*/5 job=‘/usr/sbin/ntpdate 172.20.0.1 &>/dev/null‘ name=Synctime disabled=yes"
#启用计划任务
ansible websrvs   -m cron -a "minute=*/5 job=‘/usr/sbin/ntpdate 172.20.0.1 &>/dev/null‘ name=Synctime disabled=no"
#删除任务
ansible websrvs -m cron -a "name=‘backup mysql‘ state=absent"
ansible websrvs -m cron -a ‘state=absent name=Synctime‘
service
ansible all -m service -a ‘name=httpd state=started enabled=yes‘
ansible all -m service -a ‘name=httpd state=stopped‘
ansible all -m service -a ‘name=httpd state=reloaded’
ansible all -m shell -a "sed -i ‘s/^Listen 80/Listen 8080/‘ /etc/httpd/conf/httpd.conf"
ansible all -m service -a ‘name=httpd state=restarted‘ 
user and group
#创建用户
ansible all -m user -a ‘name=user1 comment=“test user” uid=2048 home=/app/user1 group=root‘
ansible all -m user -a ‘name=nginx comment=nginx uid=88 group=nginx groups="root,daemon" shell=/sbin/nologin system=yes create_home=no  home=/data/nginx non_unique=yes‘
#删除用户及家目录等数据
ansible all -m user -a ‘name=nginx state=absent remove=yes‘
#创建组
ansible websrvs -m group  -a ‘name=nginx gid=88 system=yes‘
#删除组
ansible websrvs -m group  -a ‘name=nginx state=absent‘
lineinfile and replace

lineinfile

ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,存在问题,无法正常进行替换 。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可以方便的进行替换功能:相当于sed,可以修改文件内容

ansible all -m   lineinfile -a "path=/etc/selinux/config regexp=‘^SELINUX=‘ line=‘SELINUX=enforcing‘"
ansible all -m lineinfile  -a ‘dest=/etc/fstab state=absent regexp="^#"‘

replace

该模块有点类似于sed命令,主要也是基于正则进行匹配和替换

ansible all -m replace -a "path=/etc/fstab regexp=‘^(UUID.*)‘ replace=‘#\1‘"  
ansible all -m replace -a "path=/etc/fstab regexp=‘^#(.*)‘ replace=‘\1‘"
setup

setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机较多,会影响执行速度,可以使用gather_facts: no

ansible all -m setup
ansible all -m setup -a "filter=ansible_os_family
ansible all -m setup -a "filter=ansible_hostname"

Ansible Linux commonly used moudle example

标签:web   opp   数据   creat   back   code   htm   conf   service   

原文地址:https://www.cnblogs.com/petersonx/p/14294406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!