ansible配置使用

    xiaoxiao2021-03-25  141

    安装软件最简单的方法是apt-get install,但是卸载就不常用了,如何卸载呢? 1.sudo apt-get remove xxxxx   sudo apt-get autoremove 2.对于用命令sudo aptitude install xxx安装的软件 可以用sudo aptitude remove xxx卸载即可

    安装:

    sudo apt-get install software-properties-common $ sudo apt-add-repository ppa:ansible/ansible $ sudo apt-get update $ sudo apt-get install ansible

     

    配置文件:

    (1)ansible 应用程序的 主配置文件:/etc/ansible/ansible.cfg

    (2) Host Inventory 定义管控主机 :/etc/ansible/hosts

    ·  定义Host Inventory  

    · # vim /etc/ansible/hosts  

    ·  [webhosts]  

    · 172.16.10.22 ansible_ssh_user=root ansible_ssh_pass=guoting 

    · 172.16.10.33 ansible_ssh_user=root ansible_ssh_pass=guoting 

    ·  解释  

    · #ansible_ssh_user=root 是ssh登陆用户  

    · #ansible_ssh_pass=guoting 是ssh登陆密码3、测试各个模块  

    ·  # 注意每个模块的用法可以使用 ansible-doc MOD 来查看例如ansible-doc copy  

    ·     

    ·  ansible命令最常用的用法  

    · ansible <Host-partten> -m MOE -a 'MOD_ARV'所支持的模块可以使用ansible-doc -l来查看 

     

    使用Ansible来测试“受控节点”的网络联通情况:

    $ ansible Site01 -u root -k -m ping

    如果你使用密钥方式登录SSH,去掉 -k 参数即可。可以为Ansible设置一个特定的用户,所有操作均以此用户来执行。甚至可以为每个“受控节点”设置各自不同的用户。

    全局用户的设置见配置文件/etc/ansible/ansible.cfg ,修改 [defaults] 段落里的remote_user 的值即可。也可以通过修改 remote_port 来定义Ansible去使用非标准的22/SSH端口来进行连接和管理。在没有给“受控节点”或“受控组”进行特定设置时,Ansible将默认使用全局设置。

    可以通过设置 ansible_ssh_user 来用指定用户在“受控节点”上执行任务,还可以通过设置ansible_ssh_host 来指定不同的主机或域名,SSH对应的端口也可以通过ansible_ssh_port 来修改,同时你还能使用特定的密钥登录。以下示例中,我们在Site01上指定 root 用户运行,而在Site02上指定nobody 用户运行;同时对上述提到的配置也进行了示范:

    [WebServers]

    Site01 ansible_ssh_user=root

    Site02 ansible_ssh_user=nobody

    Site01-dr  ansible_ssh_host=site01-dr.cm

     

    [Production]

    Site01 ansible_ssh_port=7722

    Site02 

    Db01   ansible_ssh_private_key_file=/key/.ssh.id_rsa

    Bastion ansible_ssh_user=www

    在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc  -s  模块名  又可以查看该模块有哪些参数可以使用。

    常用的几个模块:

    copy模块 file模块 cron模块 group模块 user模块 yum模块 service模块 script模块 ping模块 command模块 raw模块 get_url模块 synchronize模块

    1. ansible-doc命令:获取模块列表,及模块使用格式;

    ansible-doc -l :获取列表

    ansible-doc -s  module_name :获取指定模块的使用信息

    2.ansible 命令格式

    ansible  <host-pattern>  [-f forks] [-mmodule_name]  [-a args]

    <host-pattern>

    指明管控主机,以模式形式表示或者直接给定 IP ,必须事先定义在文件中; all 设置所有

    [-f forks]

    指明每批管控多少主机,默认为 5 个主机一批次

    [-m module_name]

    使用何种模块管理操作,所有的操作都需要通过模块来指定

    [-a args]

    指明模块专用参数; args 一般为 key=value 格式

    注意:command模块的参数非为kv格式,而是直接给出要执行的命令即可;

    注意: <host-pattern> 默认读取/etc/ansible/hosts ,也可以指明自定义文件路径

    -iPATH, --inventory=PATH:指明使用的hostinventory文件路径;

    常用模块 (module_name) :

    1) command:默认模块 ,可省略。在远程主机上进行操作命令

    -a  'COMMAND'

    注意: comand 模块的参数非 key=value 格式,直接给出要执行的命令

    [root@localhost ~]# ansible all -m command -a  'ifconfig'

    2)user:

    -a 'name=  state={present ( 创建 ) |absent ( 删除 ) }  force= ( 是否强制操作删除家目录 )   system=  uid=  shell= home='

    [root@localhost ~]# ansible all -m user -a 'name=ansible state=present'

    3)group:

    -a 'name= state={present|absent}  gid=  system= ( 系统组 ) '

    [root@localhost ~]# ansible all -m group -a 'name=mygroupstate=presentsystem=true'

    4)cron:

    -a  'name= state=  minute=  hour= day= month=  weekday= job='

    [root@localhost ~]# ansible all -m cron -a 'name='Time' state=presentminute='*/5'job='/usr/sbin/ntpdate 172.168.0.1 &> /dev/null''

    5)ping:

    无参数

    [root@localhost ~]# ansible all -m ping

    6) file: 文件管理

    -a 'path=  mode= owner= group= state={file|directory|link|hard|touch|absent} src= (link ,链接至何处 ) '

    [root@localhost ~]# ansible all -m file -a'path=/tmp/testdirstate=directory'

    [root@localhost ~]# ansible all -m file -a 'path=/tmp/test.txtstate=touchmod=600 owner=user1'

    7)copy:

    -a 'dest= ( 远程主机上路径 )  src= ( 本地主机路径 )   content= ( 直接指明内容 )  owner=  group=  mode='

    [root@localhosttmp]# ansible web -m copy -a'src=/etc/yum.repos.d/aliyun.repodest=/etc/yum.repos.d/'

    8)template

    -a  'dest= src=\'#\'" content= owner= group=  mode='

    9)yum:

    -a 'name=  conf_file= ( 指明配置文件 ) state={present|latest|absent} enablerepo= disablerepo='        

    [root@localhost ~]# ansible all -m yum 'name=httpd state=present'

    10)service:

    -a 'name= state={started|stopped|restarted} enabled= ( 是否开机自动启动 )   runlevel='

    [root@localhost ~]# ansible all -m service -a 'name=httpd state=started'

    11)shell:

    -a 'COMMAND'    运行 shell 命令

    [root@localhost ~]# ansible all -m shell -a echo "123456789"|passwd --stdin user1'

    12)script:

    -a '/PATH/TO/SCRIPT' 运行脚本

    [root@localhost ~]# ansible all -m script -a '/tmp/a.sh'

     

    安装后首次连接是出现报错

    root@iZ28mgb0fazZ:/etc/ansible# ansibletest -m command -a 'date'

    139.129.201.205 | FAILED | rc=0 >>

    Using a SSH password instead of a key isnot possible because Host Key checking is enabled and sshpass does not supportthis.  Please add this host's fingerprintto your known_hosts file to manage this host.

    解决方法:

    在ansible.cfg配置文件中,也会找到如下部分: # uncomment this to disable SSH key host checking host_key_checking = False   默认host_key_checking部分是注释的,通过找开该行的注释,同样也可以实现跳过 ssh 首次连接提示验证部分。

     

    例子:

    ansible test -m shell  -a '/root/restart_dashboard.sh>/root/restart.log'

    ansible test -m copy -a'src=/etc/ansible/hosts dest=/root/'

    ansible test -m command -a 'date'

    file模块:

    目的:更改指定节点上/tmp/t.sh的权限为755,属主和属组为root

    命令:ansible all -m file -a "dest=/tmp/t.sh mode=755 owner=rootgroup=root"

    service模块:

    目的:启动指定节点上的 puppet 服务,并让其开机自启动

    命令:ansible 10.1.1.113 -m service -a 'name=puppet state=restartedenabled=yes'

    实现ssh秘钥认证。shell、copy模块

    ansible test-m copy -a 'src=/root/.ssh/id_rsa.pub dest=/root'

    ansible test-m shell -a 'cat /root/id_rsa.pub >>/root/.ssh/authorized_keys'

    密钥生成:ssh-keygen -t rsa

     

    转载请注明原文地址: https://ju.6miu.com/read-13546.html

    最新回复(0)