ansible-学习总结(Inventory实践、Ad-Hoc实践)

命令执行流程

Created with Raphaël 2.2.0 ansible命令执行 读取ansible.cfg配置文件 通过规则过滤invtentory中定义的主机列表 加载task对应模块文件 通过ansible core将模块或者命令打包成python脚本文件 将临时文件发送的目标主机 对应执行用户的夹目录中.ansible/tmp/xxx/xxx.py文件 给文件加执行权 执行py文件并返回结果 删除文件退出

实践

实验环境

主机IP作用
node1192.168.27.11控制机,安装有ansible 2.9.3
node2192.168.27.12被控制节点
node3192.168.27.13被控制节点

实践目标

Inventory实践

  1. 配置node2有node3主机管理清单
  2. 设置独立管理用户
  3. 使用ping模块进行测试

Ad-hoc实践

  1. 使用copy模块复制文件
  2. 使用command模块操作
  3. 使用yum模块安装、卸载httpd服务
  4. 使用shell模块检查软件安装
  5. 使用user模块床创建用户
  6. 使用service模块操作httpd服务开启关闭
  7. 使用setup模块收集节点主机信息
  8. 安装mariadb数据库,并开启服务,进行测试

实践过程

  • Inventory
####################### 设置独立用户ans,node1,node2,node3均需设置 #######################
[root@node1 ~]# useradd ans
[root@node1 ~]# echo ans | passwd --stdin ans
[root@node2 ~]# useradd ans
[root@node2 ~]# echo ans | passwd --stdin ans
[root@node3 ~]# useradd ans
[root@node3 ~]# echo ans | passwd --stdin ans

####################### 设置节点主机管理 ####################### 
[root@node1 ansible]# su - ans
[ans@node1 ~]$ mkdir ansible
[ans@node1 ~]$ cd ansible/
#建立操作目录,方便管理
[ans@node1 ansible]$ vim ansible.cfg
[defaults]
inventory= ./inventory
#增加配置文件,设置inventory主机列表文件路径
[ans@node1 ansible]$ vim inventory
[web]
node2															#已在/etc/hosts文件中做过解析
[db]
node3															#已在/etc/hosts文件中做过解析
[app:children]
web
db
#设置主机列表
[ans@node1 ansible]$ ansible app --list-hosts
  hosts (2):
    node2
    node3
[ans@node1 ansible]$ ansible web --list-hosts
  hosts (1):
    node2
[ans@node1 ansible]$ ansible db --list-hosts
  hosts (1):
    node3
#主机管理列表已设置完成
[ans@node1 ansible]$ ansible app -m ping -u ans -k				#ping模块,使用ans用户,设置询问密码
SSH password: 													#输入设置用户密码
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
node3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
#测试主机管理节点可以使用

####################### 设置ssh免密登录,方便操作 ####################### 
[ans@node1 ansible]$ ssh-keygen 
[ans@node1 ansible]$ ssh-copy-id ans@node2
[ans@node1 ansible]$ ssh-copy-id ans@node3
[ans@node1 ansible]$ ansible app -m ping
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
node3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
  • Ad-Hoc
####################### copy模块 ####################### 
[ans@node1 ansible]$ touch test.file
[ans@node1 ansible]$ ansible app -m copy -a "src=/home/ans/ansible/test.file dest=/tmp/test.file"
#-m调用模块,-a添加模块使用参数
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "e7509a8c032f3bc2a8df1df476f8ef03436185fa", 
    "dest": "/tmp/test.file", 
    "gid": 1000, 
    "group": "ans", 
    "md5sum": "d73b04b0e696b0945283defa3eee4538", 
    "mode": "0664", 
    "owner": "ans", 
    "size": 11, 
    "src": "/home/ans/.ansible/tmp/ansible-tmp-1583587416.74-58502808521992/source", 
    "state": "file", 
    "uid": 1000
}
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "e7509a8c032f3bc2a8df1df476f8ef03436185fa", 
    "dest": "/tmp/test.file", 
    "gid": 1000, 
    "group": "ans", 
    "md5sum": "d73b04b0e696b0945283defa3eee4538", 
    "mode": "0664", 
    "owner": "ans", 
    "size": 11, 
    "src": "/home/ans/.ansible/tmp/ansible-tmp-1583587416.73-216420757286424/source", 
    "state": "file", 
    "uid": 1000
}
#复制到节点服务器成功
####################### command模块 ####################### 
[ans@node1 ansible]$ ansible app -a "ls -l /tmp"
#应为command是默认模块,所以不用-m调用模块
node3 | CHANGED | rc=0 >>
total 4
drwx------ 2 ans  ans  41 Mar  7 07:52 ansible_command_payload_utUHaX
-rw-rw-r-- 1 ans  ans  11 Mar  7 07:51 test.file

node2 | CHANGED | rc=0 >>
total 4
drwx------ 2 ans  ans  41 Mar  7 07:52 ansible_command_payload_znrWdZ
-rw-rw-r-- 1 ans  ans  11 Mar  7 07:51 test.file
#查询
[ans@node1 ansible]$ ansible app -a "rm  /tmp/test.file"
node3 | CHANGED | rc=0 >>
node2 | CHANGED | rc=0 >>
#删除

####################### sudo设置 ####################### 
#通常使用-b可以切换用户身份,但是每次使用命令切换太麻烦,可以直接在配置文件中设置
[ans@node1 ansible]$ vim ansible.cfg 
……
[privilege_escalation]
become=True							#使用切换模式
become_method=sudo					#模式为sudo
become_user=root					#身份切换为root
become_ask_pass=False				#询问密码关闭
#添加[privilege_escalation]模块
[root@node2 ~]# vim /etc/sudoers
……
ans     ALL=(ALL)       NOPASSWD: ALL
……
#仅在node2中设置sudo,查看测试结果
[ans@node1 ansible]$  ansible app  -m copy  -a "src=/home/ans/ansible/test.file dest=/mnt/test.file"
node3 | FAILED! => {
    "msg": "Missing sudo password"
}
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "e7509a8c032f3bc2a8df1df476f8ef03436185fa", 
    "dest": "/mnt/test.file", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "d73b04b0e696b0945283defa3eee4538", 
    "mode": "0644", 
    "owner": "root", 
    "size": 11, 
    "src": "/home/ans/.ansible/tmp/ansible-tmp-1583590928.25-164215043325998/source", 
    "state": "file", 
    "uid": 0
}
#仅在设置了sudo的node2中成功,node3未设置便没有成功

####################### yum模块、shell模块 #######################
[ans@node1 ansible]$ ansible web -m yum -a "name=httpd state=present" -u root -k
SSH password: 
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "httpd"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: product-id, search-disabled-repos, subscription-manager\nThis system is not registered with an entitlement server. You can use subscription-manager to register.\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-80.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch            Version                  Repository        Size\n================================================================================\nInstalling:\n httpd          x86_64          2.4.6-80.el7             rhel7.5          1.2 M\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 1.2 M\nInstalled size: 3.7 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : httpd-2.4.6-80.el7.x86_64                                    1/1 \n  Verifying  : httpd-2.4.6-80.el7.x86_64                                    1/1 \n\nInstalled:\n  httpd.x86_64 0:2.4.6-80.el7                                                   \n\nComplete!\n"
    ]
}
#给web组的节点主机安装httpd服务
[ans@node1 ansible]$ ansible web -a "rpm -q httpd"
node2 | CHANGED | rc=0 >>
httpd-2.4.6-80.el7.x86_64
#查看安装软件
[ans@node1 ansible]$ ansible web -a "rpm -q httpd"
node2 | CHANGED | rc=0 >>
httpd-2.4.6-80.el7.x86_64
#另一种方式
[ans@node1 ansible]$ ansible web -m yum -a "name=httpd state=absent" -u root -k
SSH password: 
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "removed": [
            "httpd"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: product-id, search-disabled-repos, subscription-manager\nThis system is not registered with an entitlement server. You can use subscription-manager to register.\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-80.el7 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch            Version                 Repository         Size\n================================================================================\nRemoving:\n httpd          x86_64          2.4.6-80.el7            @rhel7.5          3.7 M\n\nTransaction Summary\n================================================================================\nRemove  1 Package\n\nInstalled size: 3.7 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Erasing    : httpd-2.4.6-80.el7.x86_64                                    1/1 \n  Verifying  : httpd-2.4.6-80.el7.x86_64                                    1/1 \n\nRemoved:\n  httpd.x86_64 0:2.4.6-80.el7                                                   \n\nComplete!\n"
    ]
}
#卸载httpd软件
####################### service模块 #######################
[ans@node1 ansible]$ ansible web -m service -a "name=httpd state=started" -u root -k
SSH password: 
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "httpd", 
    "state": "started", 
……
#开启httpd服务
[ans@node1 ansible]$ ansible web -m service -a "name=httpd state=stopped" -u root -k
SSH password: 
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "httpd", 
    "state": "stopped", 
……
#关闭服务
####################### user模块 #######################
[ans@node1 ansible]$ ansible app -m user -a "name=test password=test" -u root -k
SSH password: 
[WARNING]: The input password appears not to have been hashed. The 'password' argument must be
encrypted for this module to work properly.

node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1001, 
    "home": "/home/********", 
    "name": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1001
}
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1001, 
    "home": "/home/********", 
    "name": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1001
}
#创建用户
[ans@node1 ansible]$ ansible app -m user -a "name=test state=absent remove=yes" -u root -k
SSH password: 
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "force": false, 
    "name": "test", 
    "remove": true, 
    "state": "absent"
}
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "force": false, 
    "name": "test", 
    "remove": true, 
    "state": "absent"
}
#删除用户,同时家里路一起删除
[ans@node1 ansible]$ ansible web -m user -a "name=test password={{'test'|password_hash('sha512')}}" -u root -k
SSH password: 
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "append": false, 
    "changed": true, 
    "comment": "", 
    "group": 1001, 
    "home": "/home/test", 
    "move_home": false, 
    "name": "test", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 1001
}
#密码不是明文的方式创建用户
####################### setup模块 #######################
[ans@node1 ansible]$ ansible app -m setup|more
node2 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.27.12"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::5054:ff:fee1:b9dd"
        ], 
        "ansible_apparmor": {
            "status": "disabled"
        }, 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "01/01/2011", 
        "ansible_bios_version": "0.5.1", 
        "ansible_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-862.el7.x86_64", 
            "LANG": "en_US.UTF-8", 
            "crashkernel": "auto", 
            "quiet": true, 
            "rd.lvm.lv": "rhel/swap", 
            "rhgb": true, 
            "ro": true, 
            "root": "/dev/mapper/rhel-root"
        }, 
        "ansible_date_time": {
            "date": "2020-03-07", 
            "day": "07", 
            "epoch": "1583590562", 
            "hour": "09", 
            "iso8601": "2020-03-07T14:16:02Z", 
            "iso8601_basic": "20200307T091602482191", 
            "iso8601_basic_short": "20200307T091602", 
            "iso8601_micro": "2020-03-07T14:16:02.482254Z", 
            "minute": "16", 
            "month": "03", 
            "second": "02", 
--More--
#收集远端主机的信息

####################### 数据库安装、开启、测试 #######################
[ans@node1 ansible]$ ansible db -m yum -a "name=mariadb-server state=present" -u root -k
SSH password: 
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "mariadb-server"
……
#安装数据库
[ans@node1 ansible]$ ansible db -m yum -a "name=MySQL-python state=present" -u root -k
SSH password: 
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "MySQL-python"
……
#安装mysql数据库python控制模块
[ans@node1 ansible]$ ansible db -m service -a "name=mariadb state=started" -u root -k
SSH password: 
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "mariadb", 
    "state": "started", 
#开启服务
[ans@node1 ansible]$ ansible db -m mysql_user -a "login_user=root name=ans password=ans priv=*.*:select host='%' state=present"
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "User added", 
    "user": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
}
#注意此次添加用户,使用的ans用户登录系统,但是mysql添加用户需要给数据库root用户权限操作
[ans@node1 ansible]$ ansible db -m mysql_db -a "name=test state=present"
node3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "db": "test", 
    "db_list": [
        "test"
    ]
}
#创建数据库
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值