Ansible “connection: docker” plugin not working for lineinefile in remote container

So From an ansible with host localhost I am trying to run ansible command directly inside a docker container (command can be anything from lineinfile to copy). The main reason I am trying to do this is so that I cam seamlessly use ansible task on docker (Docker exec is NOT an appropriate solution).
I am currently trying to achieve this using connection: community.docker.docker. I want to this on a task block and not at the playbook level hence why it looks like it does. Ideally I can the params in the template without creating an additional inventory.

Heads up the playbook is truncated but you can assume all missing vars are present. the issue is main. The part where I copying Let’s encrypt is important. The fact that I can start a container and docker_exec works makes me thing the error I get

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>fatal: [localhost -> instance-139.xxx.xxx.xxx]: FAILED! => {
"msg": "docker command not found in PATH"
}
</code>
<code>fatal: [localhost -> instance-139.xxx.xxx.xxx]: FAILED! => { "msg": "docker command not found in PATH" } </code>
fatal: [localhost -> instance-139.xxx.xxx.xxx]: FAILED! => {
    "msg": "docker command not found in PATH"
}

is not supposed to happen.

Here is a truncated version of my ansible playbook

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>- name: Create an API instance
hosts: localhost
gather_facts: no
collections:
- linode.cloud
- community.docker
tasks:
- block:
- name: Linode IPS
debug:
msg: "{{ linode_ips }}"
- name: Update apt and install dependencies
apt:
update_cache: yes
name: "{{ item }}"
force_apt_get: yes
state: present
loop:
- apt-transport-https
- ca-certificates
- curl
- gzip
- software-properties-common
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: deb https://download.docker.com/linux/ubuntu focal stable
state: present
- name: Install Docker
apt:
name: docker-ce
state: present
force_apt_get: yes
update_cache: yes
- name: Start and enable Docker
systemd:
name: docker
state: started
enabled: yes
- debug:
msg: "Registry: docker.registry.aksantinet.com/{{ host_group }}"
- name: Pull the image from the registry
community.docker.docker_image:
name: "docker.registry.aksantinet.com/{{ host_group }}"
tag: latest
source: pull
- name: Run Docker container
community.docker.docker_container:
name: "{{ container_name }}"
image: "docker.registry.aksantinet.com/{{ host_group }}"
network_mode: host
state: started
capabilities:
- NET_ADMIN
devices:
- /dev/net/tun:/dev/net/tun
privileged: yes
become: yes
- name: Copy LetsEncrypt from ansible to Docker
become: yes
block:
- lineinfile:
path: "test"
line: "example"
create: yes
state: present
- ansible.builtin.copy:
src: /etc/letsencrypt/
dest: /etc/letsencrypt/
- shell: nginx
- shell: nginx
vars:
ansible_docker_host: "{{ container_name }}"
ansible_connection: docker
- name: Instance added
debug:
msg: "Addded instance at {{ linode_ip }}"
delegate_to: "instance-{{ linode_ip }}"
</code>
<code>- name: Create an API instance hosts: localhost gather_facts: no collections: - linode.cloud - community.docker tasks: - block: - name: Linode IPS debug: msg: "{{ linode_ips }}" - name: Update apt and install dependencies apt: update_cache: yes name: "{{ item }}" force_apt_get: yes state: present loop: - apt-transport-https - ca-certificates - curl - gzip - software-properties-common - name: Add Docker GPG key apt_key: url: https://download.docker.com/linux/ubuntu/gpg state: present - name: Add Docker repository apt_repository: repo: deb https://download.docker.com/linux/ubuntu focal stable state: present - name: Install Docker apt: name: docker-ce state: present force_apt_get: yes update_cache: yes - name: Start and enable Docker systemd: name: docker state: started enabled: yes - debug: msg: "Registry: docker.registry.aksantinet.com/{{ host_group }}" - name: Pull the image from the registry community.docker.docker_image: name: "docker.registry.aksantinet.com/{{ host_group }}" tag: latest source: pull - name: Run Docker container community.docker.docker_container: name: "{{ container_name }}" image: "docker.registry.aksantinet.com/{{ host_group }}" network_mode: host state: started capabilities: - NET_ADMIN devices: - /dev/net/tun:/dev/net/tun privileged: yes become: yes - name: Copy LetsEncrypt from ansible to Docker become: yes block: - lineinfile: path: "test" line: "example" create: yes state: present - ansible.builtin.copy: src: /etc/letsencrypt/ dest: /etc/letsencrypt/ - shell: nginx - shell: nginx vars: ansible_docker_host: "{{ container_name }}" ansible_connection: docker - name: Instance added debug: msg: "Addded instance at {{ linode_ip }}" delegate_to: "instance-{{ linode_ip }}" </code>
- name: Create an API instance
  hosts: localhost
  gather_facts: no
  collections:
    - linode.cloud
    - community.docker
  tasks:
    - block:
      - name: Linode IPS
        debug:
          msg: "{{ linode_ips }}"
      - name: Update apt and install dependencies
        apt:
          update_cache: yes
          name: "{{ item }}"
          force_apt_get: yes
          state: present
        loop:
          - apt-transport-https
          - ca-certificates
          - curl
          - gzip
          - software-properties-common
          
      - name: Add Docker GPG key
        apt_key:
          url: https://download.docker.com/linux/ubuntu/gpg
          state: present

      - name: Add Docker repository
        apt_repository:
          repo: deb https://download.docker.com/linux/ubuntu focal stable
          state: present

      - name: Install Docker
        apt:
          name: docker-ce
          state: present
          force_apt_get: yes
          update_cache: yes

      - name: Start and enable Docker
        systemd:
          name: docker
          state: started
          enabled: yes

      - debug:
          msg: "Registry: docker.registry.aksantinet.com/{{ host_group }}"

      - name: Pull the image from the registry
        community.docker.docker_image:
          name: "docker.registry.aksantinet.com/{{ host_group }}"
          tag: latest
          source: pull

      - name: Run Docker container
        community.docker.docker_container:
          name: "{{ container_name }}"
          image: "docker.registry.aksantinet.com/{{ host_group }}"
          network_mode: host
          state: started
          capabilities:
            - NET_ADMIN
          devices:
            - /dev/net/tun:/dev/net/tun
          privileged: yes 
        become: yes
        
      - name: Copy LetsEncrypt from ansible to Docker
        become: yes
        block:
        - lineinfile:
          path: "test"
          line: "example"
          create: yes
          state: present
        - ansible.builtin.copy:
            src: /etc/letsencrypt/
            dest: /etc/letsencrypt/
        - shell: nginx
        - shell: nginx
        vars:
          ansible_docker_host: "{{ container_name }}"
          ansible_connection: docker
      - name: Instance added
        debug: 
          msg: "Addded instance at {{ linode_ip }}"
      delegate_to: "instance-{{ linode_ip }}"  

Main points are

  • Want to run tasks inside the container docker
  • I can execute other commands before so I am confused as to why it does work with connection: docker (same ssh: user)
  • The delegate to is for the docker host

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật