ansible学习命令总结1
安装方式:
1.yum安装,在epel源中 也可以先用yum search epel / ansible 会显示出需要安装的包,之后可以通过先安装yum install 包名,有了软件源以后yum install ansible就可以了。
2.pip安装
首先安装:
yum -y install python-pip python-devel
再安装ansible:
pip install ansible
3.源码包编译安装
4.git安装
yum info ansible
rpm -ql ansible
[root@wangqs ~]# ssh-keygen
[root@wangqs ~]#sshpass -p 123456 ssh-copy-id root@192.168.3.112 -p22
[root@wangqs ~]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@wangqs ~]# yum makecache
[root@wangqs ~]# yum info ansible
[root@wangqs ~]# yum install ansible
练习 常用模块
[root@wangqs profile.d]# ansible all -m ping
[root@wangqs ~]# ansible node -m command -a 'ls /root/'
[root@wangqs ~]# ansible all -m shell -a 'echo "wqs1213" | passwd --stdin wangqs' ##command 不支持特殊字符$ < > & 等等
[root@wangqs ~]# ansible master -m command -a 'ls /root/' -u wangqs -k -b -K
默认以当前用户身份登录,-u 指定用户 -k指定输入ssh密码 -b切换sudo权限 -K指定输入sudo密码
[root@wangqs ~]# ansible all -m scripts -a 'f1.sh'
[root@wangqs ~]# ansible all -m copy -a 'src=/etc/ssh/sshd_config dest=/root/ owner=wangqs mode=600 backup=yes' 拷贝文件到远端主机 backup没有生效
[root@wangqs ~]# ansible all -m copy -a "content='hello\nthink\n' dest=/root/song.txt" #利用content直接生成内容写到远端主机文件里
[root@wangqs ~]# ansible all -m fetch -a "src=/root/sshd_config dest=/root/ mode=644 backup=yes" 拷贝远端主机文件到本地
[root@wangqs ~]# ansible all -m file -a "name=/tmp/test1 state=touch " touch创建文件
[root@wangqs ~]# ansible all -m file -a "name=/tmp/test1 state=absent " absent缺席删除
[root@wangqs ~]# ansible all -m file -a "name=/tmp/dir1 state=directory" directory创建目录
[root@wangqs ~]# ansible all -m file -a "src=/etc/fstab dest=/tmp/fstab.link state=link" link创建软连接
[root@wangqs ~]# ansible all -m file -a "name=/tmp/fstab.link state=absent" 删除软连接
file模块中,path的alias=dest和name
[root@wangqs ~]# ansible-doc -l/-s file 查看模块帮助
[root@node2 ~]# timedatectl 查询当前时区
[root@node2 ~]# timedatectl set-timezone Asia/Shanghai 设定时区
[root@wangqs ~]# ansible all -m hostname -a "name=node2.example.com"
[root@wangqs ~]# ansible all -m cron -a 'minute=*/1 job="/usr/bin/wall Lookup UFO" name=test2'
[root@wangqs ~]# ansible all -m cron -a 'minute=*/1 dow=1,3,5 job="/usr/bin/wall Lookup UFO" name=test'
[root@wangqs ~]# ansible all -m cron -a 'disabled=yes minute=*/1 job="/usr/bin/wall lookufo" name=test'
[root@wangqs ~]# ansible all -m cron -a 'disabled=true minute=*/1 job="/usr/bin/wall lookufo" name=test' #yes no / true false
[root@wangqs ~]# ansible all -m cron -a 'name=test state=absent'
[root@wangqs ~]# ansible all -m shell -a 'crontab -l'
################
ansible all -m yum -a 'name=httpd state=latest' #present默认
ansible all -m yum -a 'name=httpd state=absent'
ansible all -m yum -a 'name=httpd update_cache=yes'
###################
ansible all -m service -a 'name=httpd state=stoped'
ansible all -m service -a 'name=httpd state=started'
ansible all -m service -a 'name=httpd state=reloaded'
ansible all -m service -a 'name=httpd state=restarted'
###################
ansible all -m user -a 'name=user1 comment="test user" uid=2048 home=/app/user1 group=root'
ansible all -m user -a 'name=sysuser1 system=yes home=/app/sysuser1 shell=/sbin/nologin'
ansible all -m user -a 'name=user1 state=absent remove=yes' #删除用户及家目录
ansible all -m group -a 'name=testgroup system=yes'
ansible all -m group -a 'name=testgroup state=absent'
###################
ansible-galaxy
连接https://galaxy.ansible.com下载相应的roles
ansible-galaxy list 列出所有已安装的galaxy
安装galaxy
ansible-galaxy install geerlingguy.redis #cp复制角色可成为自己用的新角色
删除galaxy
ansible-galaxy remove geerlinggry.redis
#########ansible-valut playbook加密解密用法
[root@wangqs ansible]#ansible-vault encrypt hello.yml
[root@wangqs ansible]#ansible-vault dencrypt hello.yml
[root@wangqs ansible]#ansible-vault view hello.yml
[root@wangqs ansible]#ansible-vault edit hello.yml
[root@wangqs ansible]#ansible-vault rekey hello.yml
[root@wangqs ansible]#ansible-vault create hello2.yml
#####ansible-console用法######
2.0+新增,可交互执行命令,支持tab。
[root@wangqs ~]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.
root@all (3)[f:5]$ cd master ###root代表哪个用户 all代表主机清单 3代表主机清单里多少台主机,f:5并发。
root@master (1)[f:5]$ command hostname
192.168.3.111 | CHANGED | rc=0 >>
master.example.com
root@master (1)[f:5]$ list 列出主机列表
root@master (1)[f:5]$ cd websrvs 切换主机组
root@master (1)[f:5]$ yum name=httpd state=present
root@master (1)[f:5]$ service name=httpd state=started
?或者help
设置并发数:forks number # forks 10

浙公网安备 33010602011771号