ansible常用模块与配置代理

 

1. 配置免密登陆:

   ansible -i costome_hosts all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub')}}' path='/root/.ssh/authorized_keys' manage_dir=no"

   costome_hosts 配置

[default]

1.1.1.1        ansible_ssh_pass='qqqqq'
2.2.2.2        ansible_ssh_pass='ddddd'

[all:vars]
ansible_ssh_user='root'

 2. 替换文件中的一行,类似于sed命令

---
- hosts: all
  remote_user: root
  gather_facts: false
  tasks:
  - name: replace remote line a.root-servers.net.
    lineinfile:
      dest: /etc/hosts   # 要修改的文件
      regexp: "1.1.1.1"  # 匹配要替换的行中含有的字符
      line: "{{item.line}}"  # 来自于下面的with_items
      insertafter: "1.1.1.1" # 如果没匹配到,且backrefs为no,则在后面新加一行
      backrefs: yes
    with_items:
      - { line: "2.2.2.2   aroot-servers.net." } # 要替换的新内容

  - name: replace remote line a.root-servers.net.
    lineinfile:
      dest: /etc/hosts
      regexp: "3.3.3.3"
      line: "{{item.line}}"
      insertafter: "3.3.3.3"
      backrefs: yes
    with_items:
      - { line: "3.3.3.3   a.gtld-servers.net." }

 3. ansible使用代理登录其他机器

     ansible host ---X--> A机器     B机器(3.3.3.3)------> A机器【ansible机器直连A不通,但与机器B是通的,可以使用B机器作为代理,跳到A机器上】

     ansible host -------> B机器(代理)----->A机器

13.134.8.25 
18.56.22.7    

[all:vars]
ansible_ssh_user=root
ansible_python_interpreter=/usr/local/ansible_python/python/bin/python3
ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -p 22 -q root@3.3.3.3"'

https://blog.ruanbekker.com/blog/2020/10/26/use-a-ssh-jump-host-with-ansible/ 

https://www.jeffgeerling.com/blog/2022/using-ansible-playbook-ssh-bastion-jump-host

 

4. ansible正则匹配删除某几行

ansible -i inventories/host 1.2.2.1 -m lineinfile -a "dest=/home/logs/cd.log regexp='(.*)15:24:59(.*)' state=absent"

可以删除[2021-2-3 15:24:59.34243] [info] dsdjhf fjdjfdjfjk类似行

posted @ 2020-12-15 16:42  明天OoO你好  阅读(219)  评论(0编辑  收藏  举报