【ansible】ansble的安装、配置修改、变量定义和使用

一、安装ansible


方法一

#  yum  -y install   epel-release 

# yum -y install  ansible 

# ansible --version

# ansible  --help

# rpm -ql ansible

# ll /etc/ansible/


方法二


# yum -y install python-setuptools

# easy_install pip 

# pip install ansible 


二、常见问题 

# ansible -i hosts test01 -m shell -a 'date'

Ansible默认安装好后有一个配置文件/etc/ansible/ansible.cfg,该配置文件中定义了ansible的主机的默认配置部分,如默认是否需要输入密码、是否开启sudo认证、action_plugins插件的位置、hosts主机组的位置、是否开启log功能、默认端口、key文件位置等等。

从上面的输出提示上基本可以了解到由于在本机的~/.ssh/known_hosts文件中并有fingerprint key串,ssh第一次连接的时候一般会提示输入yes 进行确认为将key字符串加入到  ~/.ssh/known_hosts 文件中。

了解到问题原因为,我们了解到进行ssh连接时,可以使用-o参数将StrictHostKeyChecking设置为no,使用ssh连接时避免首次连接时让输入yes/no部分的提示。


解决方法1:

在hosts中添加额外参数   ansible_ssh_extra_args='-o StrictHostKeyChecking=no' 

解决方法2:

通过查看ansible.cfg配置文件,发现如下行,可以使用-o参数将StrictHostKeyChecking设置为no,使用ssh连接时避免首次连接时让输入yes/no部分的提示。

我们将注释放开,添加一个选项



三、一个项目中的ansible playbook最佳实践



hosts 文件中,定义各个主机,将各个组件根据角色分组

group_vars下根据组名,定义组变量,文件名为  组名.yml ,其中all.yml 定义全局变量,对所有主机生效

host_vars下定义主机变量,文件名为 主机别名.yml,如test01.yml,变量只针对特定主机生效



# cat install_frontend.yml

#########################

---

- hosts: frontend

  gather_facts: no

  roles:

    - frontend

############################


# cat uninstall_frontend.yml

###################################

---

- hosts: 127.0.0.1

  gather_facts: yes

  tasks:

    - name: "echo test var"

      shell: "echo {{ test_var }}"

      register: result

    - name: "print result"

      debug: var=result.stdout_lines

    - name: "print result"

      debug:

        var: result.stdout_lines

    - name: "print result"

      debug:

        msg: "{{ result.stdout_lines }}"

###################################



四、参考


How to set host_key_checking=false in ansible inventory file?

https://stackoverflow.com/questions/23074412/how-to-set-host-key-checking-false-in-ansible-inventory-file/23094433


Ansible Variable

http://www.imooc.com/article/255006

https://ansible-tran.readthedocs.io/en/latest/docs/playbooks_variables.html

https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts


Ansible: Get all the IP addresses of a group

https://stackoverflow.com/questions/36328907/ansible-get-all-the-ip-addresses-of-a-group

https://coderwall.com/p/jerwwg/in-a-template-get-all-the-ips-of-all-machines-in-a-group

https://serverfault.com/questions/615132/accessing-hostvars-for-a-host-group-in-ansible


Ansible  magic variables

https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html


ansible总结

https://www.jianshu.com/p/7c4c47aa6b5a


Ansible 专题文章总览

https://www.jianshu.com/p/c56a88b103f8


'is defined' in Ansible

https://medium.com/opsops/is-defined-in-ansible-d490945611ae

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1)安装2)常用模块3)inventory4)playbook(role\tag\template)5) yaml...
    秦记阅读 9,819评论 2 5
  • 简介 架构 原理 组成 ANSIBLE PLAYBOOKS:任务剧本(任务集),编排定义Ansible任务集的配置...
    毛利卷卷发阅读 4,733评论 0 2
  • 一、初识ansible 1、ansible是新出现的自动化运维工具 ansible是一个配置管理和应用部署工具,基...
    清风徐来_简阅读 7,184评论 0 15
  • 一.responder对象 在iOS系统中,能够响应并处理事件的对象称之为responder object,UIR...
    大饼炒鸡蛋阅读 4,365评论 0 2
  • 1)打开文件: 在python中用open()这个函数来打开文件并返回文件对象,open()函数有很多参数,其中第...
    MikeCorleonefan阅读 1,228评论 0 0