Рубрики
ansible

ansible delegate_to / выполнить где нужно / run_once / shell / copy

playbook.yml delegate_to

---
- name: My playbook create file
  hosts: all
  become: yes

  vars:
   mytext: "Privet ot b14esh"

  tasks:
  - name: Test ping
    ping:

  - name: Create File file1.txt
    copy:
      dest: /home/file1.txt
      content: |
         This is file1.txt
         On ohhh {{ mytext }}
    delegate_to: linux1

  - name: Create file file2.txt
    copy:
     dest: /home/file2.txt
     content: |
        This is File2
        Mytext {{ mytext }}
        lol lol lol lol lol

...



playbook.yml delegate_to на ansible master 127.0.0.1

---
- name: My playbook create file
  hosts: all
  become: yes

  vars:
   mytext: "Privet ot b14esh"

  tasks:
  - name: Test ping
    ping:

  - name: Unregister Server from Load Balance
    shell: echo this server {{ inventory_hostname }} was deregisterred from our load balancer, node name is {{ ansible_nodename }} >> /home/log.txt
    delegate_to: 127.0.0.1

  - name: Create File file1.txt
    copy:
      dest: /home/file1.txt
      content: |
         This is file1.txt
         On ohhh {{ mytext }}
    delegate_to: linux1

  - name: Create file file2.txt
    copy:
     dest: /home/file2.txt
     content: |
        This is File2
        Mytext {{ mytext }}
        lol lol lol lol lol

...

playbook.yml delegate_to , run_once выполнить один раз , reboot и ждем

---
- name: My playbook create file
  hosts: all
  become: yes

  vars:
   mytext: "Privet ot b14esh"

  tasks:
  - name: Test ping
    ping:

  - name: Unregister Server from Load Balance
    shell: echo this server {{ inventory_hostname }} was deregisterred from our load balancer, node name is {{ ansible_nodename }} >> /home/log.txt
    delegate_to: 127.0.0.1

  - name: Update my database
    shell: echo Update database
    run_once: true
    delegate_to: 127.0.0.1
   
  - name: Create File file1.txt
    copy:
      dest: /home/file1.txt
      content: |
         This is file1.txt
         On ohhh {{ mytext }}
    delegate_to: linux1

  - name: Create file file2.txt
    copy:
     dest: /home/file2.txt
     content: |
        This is File2
        Mytext {{ mytext }}
        lol lol lol lol lol

  - name: reboot my servers
    shell: sleep 4 && reboot now
    async: 1
    poll: 0

  - name: wait my server will come online
    wait_for:
      host: "{{ inventory_hostname }}"
      state: started
      delay: 5
      timeout: 40
    delegate_to: 127.0.0.1
...