Ansible基础

安装Ansible

yum install epel-release
yum install ansible -y

建立主控被控信任关系

ssh-keygen -t rsa   #主控
ssh-copy-id root@192.168.188.9

Ansible 资产

静态资产

/etc/ansible/hosts     #默认
cat inventory.ini
ansible all -i inventory.ini ...    #自定义资产

资产选择器

ansible PATTERN -i inventory -m module -a argument
--PATTERN
ip,ip     
domains
all
WebServer:DBServer   #所有
WebServer:&DBServer  #交集
WebServer:!DBServer   #排除 在Web不在DB
1.1.1.*

Ad-Hoc 命令

ansible pattern [-i inventory] -m module -a argument

联机帮助

ansible-doc -l    #列出所有核心模块和附加模块
ansible-doc modulename
ansible-doc -s modulename     #查询模块简洁帮助信息

Command & Shell模块

ansible all  -a "echo 'hello'"     #command模块是ad-hoc的默认模块
ansible all -m shell -a "echo 'hello'"

shell 模块可以执行SHELL 的内置命令和特性command却不能

Script 模块

ansible webservers -i hosts -m script -a "/root/a.sh"

Copy模块

  • src/dest 源地址/目标地址
  • backup 拷贝前若目标文件发生变化则进行备份
  • owner/group/mode
ansible all -m copy -a "src=./1.txt dest=/opt/1.txt owner=nobody group=nobody mode=0755"

Yum仓库模块

  • name 仓库中括号名称*
  • description 描述信息*
  • baseurl repo目录Url*
  • file 保存被管理节点文件名
  • state present/absent 添加/删除
  • gpgcheck yes/no
 ansible all hosts -m yum_repository -a "name=epel state=absent"

Yum模块

  • name
  • present/installed/latest/absent/removed
ansible webservers -m yum -a  "name=nginx state=installed"

Systemd模块

  • daemon_reload 重新载⼊ systemd,扫描新的或有变动的单元
  • enabled yes/no
  • state started/stopped/restarted/reloaded
ansible webservers -m systemd -a  "daemon_reload=yes"
ansible webservers -m systemed -a "name=nginx state=remove"

Group模块

  • name
  • system 是否为系统组默认no
  • state present/absent
 ansible dbservers -m group -a  "name=db_admin"
 

User模块

  • name
  • password/update_password
  • home/creat_home 默认创建若不创建设置no
  • group/groups 设置主组/其他组(默认删去已加其他组 append=yes时不会除)
  • state present/absent/remove
  • generate_ssh_key=yes
  • ssh_key_type=ecdsa
  • expires
pass=$(echo "123456" |openssl passwd -1 -stdin)
ansible all -m user -a "name=hapa password=${pass}"

File模块

  • owner/group/mode
  • path*
  • src/dest
  • state directory(如果目录不存在创建)/file(检查文件是否存在)/link/hard/touch/absent
ansible all -m file -a "src=/tmp/1.conf dest=/tmp/2.conf state=link"

Cron模块

  • name
  • minute/hour/day/month/weekday
  • job
  • state present/absent
ansible all -m cron -a " name='crontab test' minute=5 hour=1 job='echo hello '"

Debug模块

  • var 变量值
  • msg 格式化字符串
ansible all -m debug -a "msg='role is {{role}} '" -e "role=web"

Template 模块

  • src/dest 控制端路径/被控
  • owner/group/mode
  • backup
echo  Hello {{var}} !  > hello_world.j2
ansible all  -m template -a 
"src=hello_world.j2 dest=/tmp/hello_world.world" -e
"var=world"

Lineinfile 模块

  • path*
  • state absent/present
  • regexp
  • line 要在⽂件中插入/替换的行。
ansible all -m lineinfile -a
"path=/etc/selinux/config regexp='^SELINUX='
line='SELINUX=disabled' state=present"

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇