Fork me on GitHub

ansible 的安装及常见模块使用

ansible 基础keys的ssh协议配置的

 特性:幂等性:一个任务执行1遍和执行n遍效果一样。

ansible是个管理软件不是服务,不需要长期运行

 一、通过epel源安装ansible,

1、下载阿里云base源和epel源

1 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
2 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

二、主机清单(将需要管理的机器放到这个文件里)(在最后一行添加即可)

/etc/ansible/hosts

三、以组的方式管理主机

[webserver]
172.16.1.100
172.16.1.101

#webserver是组名,下面是要管理的机器的IP地址

[webserver]
172.16.1.100
172.16.1.101

[appserver]
172.16.1.10[1:2]

#按组分,172.16.1.10[1:2] 表示 172.16.1.101和172.16.1.102 

四、比如想了解hostname相关的模块信息

ansible-doc hostname

-s 以简单方式查看的选项

[10:35:56 root@ansible ~]$ansible-doc -s hostname
- name: Manage hostname
  hostname:
      name:                  # (required) Name of the host
      use:                   # Which strategy to use to update the hostname. If not set
                               we try to autodetect, but
                               this can be problematic,
                               specially with containers
                               as they can present
                               misleading information.

五、ansible单条命令执行

1、列出当前管理的主机清单

ansible all --list-host
  hosts (3):
    172.16.1.101
    172.16.1.102
    172.16.1.100

2、列出webserver下的主机

ansible webserver --list-hosts
  hosts (2):
    172.16.1.100

六、

1、首次远程控制会问你yes or no 去掉配置文件中的host_key_checking = False注释即可

vim /etc/ansible/ansible.cfg 

host_key_checking = False

2、在配置中开启日志

log_path = /var/log/ansible.log

七、两种连接方式

1、手工输入密码 ansible 172.16.1.101 -m ping -k  ,-k输入对方主机的root密码即可

ansible 172.16.1.101 -m ping -k

172.16.1.101 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

 2、基于key验证(不需要每次都输入密码)

1)、ssh-keygen 

ssh-keygen 
Generating public
/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:d01XYNc5GBGsLKkTACOsJC5kGKb/QD/gigfJ0J5zZMA root@ansible The key's randomart image is: +---[RSA 2048]----+ |+=.o. .+B.=| |=*E... + +o| |X + o . o . . o| |=B * . o o o . | |+.B + S o . . | |.o = . o . . | |o . . . | | . | | | +----[SHA256]-----+

2、拷贝刚刚生成的key到需要远程的主机(只需要ansible主机到其他机器,不需要其他机器到ansible主机)(输入密码即可)

[16:42:43 root@ansible ~]$ssh-copy-id 172.16.1.100
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.1.100's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.1.100'"
and check to make sure that only the key(s) you wanted were added.

八、ansible 的使用

ansible "webser:&appser" -m ping -C
#逻辑与webser组和appser组中都有的主机,执行ping模块

ansible 'webser:!appser' -m ping -C
#逻辑非,在webser组合但不在appser组中

 九、ansible常用模块使用步骤(模块名-m,模块参数-a)

1、command 默认模块(可以不加command)

ansible-doc -s command

1)、在所有主机上执行ls /data

ansible all -m command -a "ls /data"

2)、creates判断文件是否存在,如果存在就不执行后面的ls /data 

ansible all -a "creates=/etc/fstab ls /data"

 #command模块不支持 $VARNAME < > | ;&这些

2、shell 模块  和command相似,用shell命令执行

1)、在所有主机上创建用户

ansible all -m shell -a 'useradd alex'

 2)对所有主机,重置alex用户密码为China123

ansible all -m shell -a 'echo China123 |passwd --stdin alex'

 3)、对webser这个组里的所有主机显示$hostname

ansible webser -m shell -a 'echo $HOSTNAME'

 4)、将默认模块由command修改为shell模块(shell模块可以替代command)

module_name = command
修改为
module_name = shell

 

5)、通过ansible批量修改所有主机的selinux为disabled(当然他有专门的模块做,这里只是演式)

ansible all -a "sed -i 's@SELINUX=enforcing@SELINUX=Disabled@' /etc/selinux/config"

 

3、script 模块在远程节点上运行本地脚本(最好绝对路径)

ansible all -m script -a /root/test1.sh 

 

4、copy 模块、从当前主机拷贝文件到远程主机

1)、将本机的/etc/fstab 拷贝到/data/下

ansible all -m copy -a 'src=/etc/fstab dest=/data/'

# src 源目录(支持相对路径和绝对路径),dest 目的目录  (仅支持绝对路径)再次执行相同的文件拷贝命令,源文件有变化内容属性等变化才会重新拷贝一遍,文件一模一样的将不拷贝

2)、将本机的/etc/fstab 拷贝到所有主机下的/data/fstab 下将所有者改为alex,所有组改为bin,文件权限改为777

ansible all -m copy -a 'src=/etc/fstab dest=/data/fstab mode=777 owner=alex group=bin'

 

3)、避免文件存在呗覆盖,加backup,备份

ansible all -m copy -a 'src=/etc/fstab dest=/data/fstab mode=777 owner=alex group=bin backup=yes'

 

4)、content 指定内容,职级生成目标文件

ansible all -m copy -a 'content="line1\nline2\nline3\n" dest=/data/test.txt'

 5、fetch 从远程主机提取文件至ansible主机(不支持目录)(会在/data/messages下生成远程主机IP+/var/log/messages的文件)(用来抓取远程主机的配置、日志等)(执行多个文件的抓取也不会覆盖文件IP文件夹)

ansible all -m fetch -a 'src=/var/log/messages dest=/data/messages/'

 6、file文件模块(都是直接操作目标主机的文件)

1)、path指定要管理的文件,设置所有者所有组权限。

ansible all -m file -a 'path=/data/fstab owner=alex mode=777'

 

2)、创建软链接(hard硬链接,link软链接)

ansible all -m file -a 'src=/data/fstab path=/data/fstab.link state=link'

 

3)、创建空文件state=touch

ansible all -m file -a 'path=/data/f1.txt state=touch'

 

4)、删除文件 state=absent

ansible all -m file -a 'path=/data/fstab.link state=absent'

 

5)、清空目录,/data/是挂载点事删不掉的(最好不用,以防删根)

ansible all -m file -a 'path=/data/ state=absent'

 

7、unarchive:解包解压缩,

1)、在ansible这台主机上的压缩包在本地解压然后拷贝到远程主机上去(默认copy=yes 可以不写)

ansible all -m unarchive -a 'src=/data/sysconfig.tar.gz dest=/data/ owner=alex group=bin mode=777'

 

#将本地的/data/sysconfig.tar.gz 拷贝到所有主机的/data/下,所有者改为alex,所有组该为bin 权限改为777

2)、将远程主机上的压缩包,解压缩到指定目录上去

ansible all -m copy -a 'src=/data/sysconfig.tar.gz dest=/data/ '

ansible all -m unarchive -a 'src=/data/sysconfig.tar.gz dest=/data/ copy=no'

 8、在远程主机上压缩/etc/sysconfig 压缩到/data/sysconfig.tar.bz2 ,format 压缩格式bz2 ,所有者改为alex

ansible all -m archive -a 'path=/etc/sysconfig dest=/data/sysconfig.tar.bz2 format=bz2 owner=alex'

 

 9、修改远程主机的hostname主机名centos

ansible 172.16.1.100 -m hostname -a 'name=centos'

 

 10、cron、计划任务(每隔五分钟执行一下ntpdate 同步下时间,name是注释)

ansible all -m cron -a "minute=*/5 job=' /usr/sbin/ntpdate 10.8.2.103 &>/dev/null' name=Synctime"

 

 确认计划任务的创建

ansible all -a 'crontab -l'

172.16.1.102 | CHANGED | rc=0 >>
#Ansible: Synctime
*/5 * * * *  /usr/sbin/ntpdate 10.8.2.103 &>/dev/null
172.16.1.101 | CHANGED | rc=0 >>
#Ansible: Synctime
*/5 * * * *  /usr/sbin/ntpdate 10.8.2.103 &>/dev/null
172.16.1.100 | CHANGED | rc=0 >>
#Ansible: Synctime
*/5 * * * *  /usr/sbin/ntpdate 10.8.2.103 &>/dev/null

 

删除禁用计划任务(禁用disabled=true ,启用disable=no)(state=absent删除计划任务)

ansible all -m cron -a "minute=*/5 job=' /usr/sbin/ntpdate 10.8.2.103 &>/dev/null' name=Synctime disabled=true"

 

11、yum :管理包 (state=present 安装)(state=absent 删除)

ansible all -m yum -a 'name=httpd state=present'

 

$ansible all -m yum -a 'name=httpd state=absent'

 

posted @ 2022-08-22 22:36  Alex-Lzy  阅读(1132)  评论(0编辑  收藏  举报