ansible常用模块

ansible常用模块

安装:依赖于epel源

yum install ansible -y 
配置文件:/etc/ansible/ansible.cfg
Invertoory: /etc/ansible/hosts

 

如何查看模块帮助:

    ansible-doc -l
ansible-doc -s MODULE_NAME`

 

ansible命令应用基础

语法:ansible <host-pattern> [options]
-f forks: 启动的并发线程数
-m module_name: 要使用的模块
-a args: 模块特有的参数

 

ansible常用模块

command: 命令模块,默认模块,用于在远程执行命令
ansible all -a 'date'
cron:
*/10 * * * * /bin/echo hello linux
state:
present:
  #安装 •ansible all -m cron -a 'minute="*/10" job="/bin/echo hello linux" name="test cron_job" state=present'
absent:
#移除 •ansible all -m cron -a 'minute="*/10" job="/bin/echo hello linux" name="test cron_job" state=absent'
•ansible all -a "crontab -l"
user:

# 创建用户
• ansible all -m user -a 'name="user1"'
• cat /etc/passwd # 最后一行

# 删除用户
• ansible all -m user -a 'name="user1" state="absent"'
group:  

# 创建组

• ansible all -m group -a 'name=mysql gid=306 system=yes'
• cat /etc/group

# 删除组

• ansible all -m group -a 'name=mysql gid=306 system=yes state=absent'
copy:

# 复制文件 src定义本地源文件路径 dest定义远程主机路径(绝对路径)

• ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640'

# content 生成内容的方式进行远程复制

• ansible all -m copy -a 'content="你好崔建忠\n" dest=/tmp/test.ansible'
file: # 设定文件属性

# 改变文件的属主属组

• ansible all -m file -a 'owner=mysql group=mysql mode=644 path=/tmp/fstab.ansible'

# link 创建文字符号链接

• ansible all -m file -a 'path=/tmp/fstab.link src=/tmp/fstab.ansible state=link'
ping:

# 测试主机是否能链接

• ansible all -m ping

service:

# 设置开机自启动,并且现在启动起来

• ansible all -m service -a 'enabled=true name=httpd state=started'

# 关闭某一个服务

• ansible all -m service -a 'enabled=true name=httpd state=stopped'

shell: 执行shell命令
• ansible all -m shell -a 'hello ansible|passwd --stdin user1'

script: 将本地脚本复制到远程主机并运行
• ansible all -m script -a '/root/test.sh'

yum: 安装程序包
name=: 指明要安装的程序包,可以带上版本号
state=: present,latest表示安装,absent表示卸载

setup: 收集远程主机的facts
每个被管理节点在接收并且运行管理命令之前,会将自己主机相关的信息,如操作系统版本、ip地址等报告给远程的ansible主机;

 

相关文章

----name:setpublickeyonremotehosts&setreomtehostssud...
环境准备#cat/etcedhat-releaseCentOSLinuxrelease7.9.2009(...
准备好环境,在安装之前请先了解openshift提供的ansible有大...
Ansible:运维工作:系统安装(物理机、虚拟机)-->程序包...
ansible与salt对比相同都是为了同时在多台机器上执行相同的命...
[root@node1playbook]#catnginx.yml-hosts:test\\主...